From 95945203dba1803e59f92d245ce0f10518c38e44 Mon Sep 17 00:00:00 2001 From: skilesare Date: Fri, 8 Jul 2022 14:01:13 -0700 Subject: [PATCH] refactoring out unstable values --- package-set.dhall | 14 +- src/clone.mo | 49 +++- src/conversion.mo | 545 ++----------------------------------------- src/properties.mo | 154 ------------ src/types.mo | 285 +++------------------- src/workspace.mo | 224 ++++++------------ tests/test_runner.mo | 18 +- vessel.dhall | 2 +- 8 files changed, 188 insertions(+), 1103 deletions(-) diff --git a/package-set.dhall b/package-set.dhall index 072bbde..f2f26f2 100644 --- a/package-set.dhall +++ b/package-set.dhall @@ -3,9 +3,17 @@ let Package = { name : Text, version : Text, repo : Text, dependencies : List Text } let - -- This is where you can add your own packages to the package-set additions = - [] : List Package + [{ name = "stablerbtree" + , repo = "https://github.com/skilesare/StableRBTree" + , version = "v0.6.1" + , dependencies = [ "base"] + }, + { name = "stablebuffer" + , repo = "https://github.com/skilesare/StableBuffer" + , version = "v0.2.0" + , dependencies = [ "base"] + }] : List Package let {- This is where you can override existing packages in the package-set @@ -13,7 +21,7 @@ let For example, if you wanted to use version `v2.0.0` of the foo library: let overrides = [ { name = "foo" - , version = "v2.0.0" + , version = "v2.0" , repo = "https://github.com/bar/foo" , dependencies = [] : List Text } diff --git a/src/clone.mo b/src/clone.mo index 8d7011f..c5bbd4d 100644 --- a/src/clone.mo +++ b/src/clone.mo @@ -13,19 +13,20 @@ import Types "types"; import Array "mo:base/Array"; +import SB "mo:stablebuffer/StableBuffer"; + module { type CandyValue = Types.CandyValue; - type CandyValueUnstable = Types.CandyValueUnstable; - type PropertyUnstable = Types.PropertyUnstable; + type Property = Types.Property; - public func cloneValueUnstable(val : CandyValueUnstable) : CandyValueUnstable{ + public func clone(val : CandyValue) : CandyValue{ switch(val){ case(#Class(val)){ - return #Class(Array.tabulate(val.size(), func(idx){ - {name= val[idx].name; value=cloneValueUnstable(val[idx].value); immutable = val[idx].immutable}; + return #Class(Array.tabulate(val.size(), func(idx){ + {name= val[idx].name; value=clone(val[idx].value); immutable = val[idx].immutable}; })); }; case(#Bytes(val)){ @@ -36,7 +37,43 @@ module { }; case(#thawed(val)){ - #Bytes(#thawed(val.clone())); + #Bytes(#thawed(SB.clone(val))); + }; + } + }; + case(#Nats(val)){ + switch(val){ + case(#frozen(val)){ + + #Nats(#frozen(val)); + }; + case(#thawed(val)){ + + #Nats(#thawed(SB.clone(val))); + }; + } + }; + case(#Floats(val)){ + switch(val){ + case(#frozen(val)){ + + #Floats(#frozen(val)); + }; + case(#thawed(val)){ + + #Floats(#thawed(SB.clone(val))); + }; + } + }; + case(#Array(val)){ + switch(val){ + case(#frozen(val)){ + + #Array(#frozen(val)); + }; + case(#thawed(val)){ + + #Array(#thawed(SB.clone(val))); }; } }; diff --git a/src/conversion.mo b/src/conversion.mo index c8fd432..51408c3 100644 --- a/src/conversion.mo +++ b/src/conversion.mo @@ -10,12 +10,12 @@ /////////////////////////////// import Buffer "mo:base/Buffer"; +import Array "mo:base/Array"; import Blob "mo:base/Blob"; import Char "mo:base/Char"; import Iter "mo:base/Iter"; import Text "mo:base/Text"; import HashMap "mo:base/HashMap"; -import Array "mo:base/Array"; import Nat "mo:base/Nat"; import Nat16 "mo:base/Nat16"; import Nat32 "mo:base/Nat32"; @@ -34,15 +34,17 @@ import List "mo:base/List"; import Types "types"; import Hex "hex"; import Properties "properties"; +import RB "mo:stablerbtree/StableRBTree"; +import SB "mo:stablebuffer/StableBuffer"; module { type CandyValue = Types.CandyValue; - type CandyValueUnstable = Types.CandyValueUnstable; + type DataZone = Types.DataZone; type Property = Types.Property; - type PropertyUnstable = Types.PropertyUnstable; + //todo: generic accesors @@ -345,7 +347,7 @@ module { }; case(#thawed(val)){ var t = "["; - for(thisItem in val.vals()){ + for(thisItem in SB.vals(val)){ t := t # "{" #valueToText(thisItem) # "} "; }; @@ -366,7 +368,7 @@ module { }; case(#thawed(val)){ var t = "["; - for(thisItem in val.vals()){ + for(thisItem in SB.vals(val)){ t := t # Float.format(#exact, thisItem) # " "; }; @@ -383,7 +385,7 @@ module { }; case(#thawed(val)){ - return Hex.encode(val); + return Hex.encode(SB.toArray(val)); }; }; }; @@ -415,7 +417,7 @@ module { switch(val){ case(#frozen(val)){ val;}; - case(#thawed(val)){ val}; + case(#thawed(val)){ SB.toArray(val)}; } )}; @@ -457,7 +459,7 @@ module { case(#Array(val)){ switch(val){ case(#frozen(val)){val}; - case(#thawed(val)){val}; + case(#thawed(val)){SB.toArray(val)}; }; }; //todo: could add all conversions here @@ -465,431 +467,7 @@ module { }; }; - //unstable getters - public func valueUnstableToNat(val : CandyValueUnstable) : Nat { - - switch(val){ - case(#Nat(val)){ val}; - case(#Nat8(val)){ Nat8.toNat(val)}; - case(#Nat16(val)){ Nat16.toNat(val)}; - case(#Nat32(val)){Nat32.toNat(val)}; - case(#Nat64(val)){ Nat64.toNat(val)}; - case(#Float(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat(#Int(Float.toInt(Float.nearest(val))))}; - case(#Int(val)){ - if(val < 0){assert false;};//will throw on negative - - Int.abs(val)}; - case(#Int8(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat(#Int(Int8.toInt(Int8.abs(val))))};//will throw on overflow - case(#Int16(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat(#Int(Int16.toInt(Int16.abs(val))))};//will throw on overflow - case(#Int32(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat(#Int(Int32.toInt(Int32.abs(val))))};//will throw on overflow - case(#Int64(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat(#Int(Int64.toInt(Int64.abs(val))))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToNat8(val : CandyValueUnstable) : Nat8 { - - switch(val){ - case(#Nat8(val)){ val}; - case(#Nat(val)){ Nat8.fromNat(val)};//will throw on overflow - case(#Nat16(val)){ Nat8.fromNat(Nat16.toNat(val))};//will throw on overflow - case(#Nat32(val)){ Nat8.fromNat(Nat32.toNat(val))};//will throw on overflow - case(#Nat64(val)){ Nat8.fromNat(Nat64.toNat(val))};//will throw on overflow - case(#Float(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat8(#Int(Float.toInt(Float.nearest(val))))}; - case(#Int(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat8(#Nat(Int.abs(val)))}; - case(#Int8(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat8(#Int(Int8.toInt(Int8.abs(val))))};//will throw on overflow - case(#Int16(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat8(#Int(Int16.toInt(Int16.abs(val))))};//will throw on overflow - case(#Int32(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat8(#Int(Int32.toInt(Int32.abs(val))))};//will throw on overflow - case(#Int64(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat8(#Int(Int64.toInt(Int64.abs(val))))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToNat16(val : CandyValueUnstable) : Nat16 { - - switch(val){ - case(#Nat16(val)){ val}; - case(#Nat8(val)){ Nat16.fromNat(Nat8.toNat(val))}; - case(#Nat(val)){ Nat16.fromNat(val)};//will throw on overflow - case(#Nat32(val)){ Nat16.fromNat(Nat32.toNat(val))};//will throw on overflow - case(#Nat64(val)){ Nat16.fromNat(Nat64.toNat(val))};//will throw on overflow - case(#Float(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat16(#Int(Float.toInt(Float.nearest(val))))}; - case(#Int(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat16(#Nat(Int.abs(val)))}; - case(#Int8(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat16(#Int(Int8.toInt(Int8.abs(val))))};//will throw on overflow - case(#Int16(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat16(#Int(Int16.toInt(Int16.abs(val))))};//will throw on overflow - case(#Int32(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat16(#Int(Int32.toInt(Int32.abs(val))))};//will throw on overflow - case(#Int64(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat16(#Int(Int64.toInt(Int64.abs(val))))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToNat32(val : CandyValueUnstable) : Nat32 { - - switch(val){ - case(#Nat32(val)){val}; - case(#Nat16(val)){Nat32.fromNat(Nat16.toNat(val))}; - case(#Nat8(val)){ Nat32.fromNat(Nat8.toNat(val))}; - case(#Nat(val)){ Nat32.fromNat(val)};//will throw on overflow - case(#Float(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat32(#Int(Float.toInt(Float.nearest(val))))}; - case(#Int(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat32(#Nat(Int.abs(val)))}; - case(#Int8(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat32(#Int(Int8.toInt(Int8.abs(val))))};//will throw on overflow - case(#Int16(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat32(#Int(Int16.toInt(Int16.abs(val))))};//will throw on overflow - case(#Int32(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat32(#Int(Int32.toInt(Int32.abs(val))))};//will throw on overflow - case(#Int64(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat32(#Int(Int64.toInt(Int64.abs(val))))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToNat64(val : CandyValueUnstable) : Nat64 { - - switch(val){ - case(#Nat64(val)){ val}; - case(#Nat32(val)){ Nat64.fromNat(Nat32.toNat(val))}; - case(#Nat16(val)){ Nat64.fromNat(Nat16.toNat(val))}; - case(#Nat8(val)){ Nat64.fromNat(Nat8.toNat(val))}; - case(#Nat(val)){ Nat64.fromNat(val)}; - case(#Float(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat64(#Int(Float.toInt(Float.nearest(val))))}; - case(#Int(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat64(#Nat(Int.abs(val)))}; - case(#Int8(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat64(#Int(Int8.toInt(Int8.abs(val))))};//will throw on overflow - case(#Int16(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat64(#Int(Int16.toInt(Int16.abs(val))))};//will throw on overflow - case(#Int32(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat64(#Int(Int32.toInt(Int32.abs(val))))};//will throw on overflow - case(#Int64(val)){ - if(val < 0){assert false;};//will throw on negative - valueUnstableToNat64(#Int(Int64.toInt(Int64.abs(val))))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToInt(val : CandyValueUnstable) : Int { - - switch(val){ - case(#Int(val)){val}; - case(#Int8(val)){ Int8.toInt(val)}; - case(#Int16(val)){ Int16.toInt(val)}; - case(#Int32(val)){ Int32.toInt(val)}; - case(#Int64(val)){ Int64.toInt(val)}; - case(#Nat(val)){ val}; - case(#Nat8(val)){Nat8.toNat(val)}; - case(#Nat16(val)){Nat16.toNat(val)}; - case(#Nat32(val)){Nat32.toNat(val)}; - case(#Nat64(val)){Nat64.toNat(val)}; - case(#Float(val)){Float.toInt(Float.nearest(val))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToInt8(val : CandyValueUnstable) : Int8 { - - switch(val){ - case(#Int8(val)){ val}; - case(#Int(val)){ Int8.fromInt(val)};//will throw on overflow - case(#Int16(val)){ Int8.fromInt(Int16.toInt(val))};//will throw on overflow - case(#Int32(val)){ Int8.fromInt(Int32.toInt(val))};//will throw on overflow - case(#Int64(val)){ Int8.fromInt(Int64.toInt(val))};//will throw on overflow - case(#Nat8(val)){ Int8.fromNat8(val)}; - case(#Nat(val)){Int8.fromNat8(valueUnstableToNat8(#Nat(val)))};//will throw on overflow - case(#Nat16(val)){Int8.fromNat8(valueUnstableToNat8(#Nat16(val)))};//will throw on overflow - case(#Nat32(val)){Int8.fromNat8(valueUnstableToNat8(#Nat32(val)))};//will throw on overflow - case(#Nat64(val)){Int8.fromNat8(valueUnstableToNat8(#Nat64(val)))};//will throw on overflow - case(#Float(val)){ Int8.fromInt(Float.toInt(Float.nearest(val)))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToInt16(val : CandyValueUnstable) : Int16 { - - switch(val){ - case(#Int16(val)){ val}; - case(#Int8(val)){ Int16.fromInt(Int8.toInt(val))}; - case(#Int(val)){ Int16.fromInt(val)};//will throw on overflow - case(#Int32(val)){ Int16.fromInt(Int32.toInt(val))};//will throw on overflow - case(#Int64(val)){ Int16.fromInt(Int64.toInt(val))};//will throw on overflow - case(#Nat8(val)){Int16.fromNat16(valueUnstableToNat16(#Nat8(val)))}; - case(#Nat(val)){Int16.fromNat16(valueUnstableToNat16(#Nat(val)))};//will throw on overflow - case(#Nat16(val)){ Int16.fromNat16(val)}; - case(#Nat32(val)){Int16.fromNat16(valueUnstableToNat16(#Nat32(val)))};//will throw on overflow - case(#Nat64(val)){Int16.fromNat16(valueUnstableToNat16(#Nat64(val)))};//will throw on overflow - case(#Float(val)){ Int16.fromInt(Float.toInt(Float.nearest(val)))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToInt32(val : CandyValueUnstable) : Int32 { - switch(val){ - case(#Int32(val)){ val}; - case(#Int16(val)){ Int32.fromInt(Int16.toInt(val))}; - case(#Int8(val)){ Int32.fromInt(Int8.toInt(val))}; - case(#Int(val)){ Int32.fromInt(val)};//will throw on overflow - case(#Nat8(val)){Int32.fromNat32(valueUnstableToNat32(#Nat8(val)))}; - case(#Nat(val)){Int32.fromNat32(valueUnstableToNat32(#Nat(val)))};//will throw on overflow - case(#Nat16(val)){Int32.fromNat32(valueUnstableToNat32(#Nat16(val)))}; - case(#Nat32(val)){ Int32.fromNat32(val)}; - case(#Nat64(val)){Int32.fromNat32(valueUnstableToNat32(#Nat64(val)))};//will throw on overflow - case(#Float(val)){ Int32.fromInt(Float.toInt(Float.nearest(val)))};//will throw on overflow - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToInt64(val : CandyValueUnstable) : Int64 { - switch(val){ - case(#Int64(val)){ val}; - case(#Int32(val)){ Int64.fromInt(Int32.toInt(val))}; - case(#Int16(val)){ Int64.fromInt(Int16.toInt(val))}; - case(#Int8(val)){ Int64.fromInt(Int8.toInt(val))}; - case(#Int(val)){ Int64.fromInt(val)};//will throw on overflow - case(#Nat8(val)){Int64.fromNat64(valueUnstableToNat64(#Nat8(val)))}; - case(#Nat(val)){Int64.fromNat64(valueUnstableToNat64(#Nat(val)))};//will throw on overflow - case(#Nat16(val)){Int64.fromNat64(valueUnstableToNat64(#Nat16(val)))}; - case(#Nat32(val)){Int64.fromNat64(valueUnstableToNat64(#Nat32(val)))};//will throw on overflow - case(#Nat64(val)){ Int64.fromNat64(val)}; - case(#Float(val)){ Float.toInt64(Float.nearest(val))}; - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToFloat(val : CandyValueUnstable) : Float { - switch(val){ - case(#Float(val)){ val}; - case(#Int64(val)){ Float.fromInt64(val)}; - case(#Int32(val)){valueUnstableToFloat(#Int(Int32.toInt(val)))}; - case(#Int16(val)){valueUnstableToFloat(#Int(Int16.toInt(val)))}; - case(#Int8(val)){valueUnstableToFloat(#Int(Int8.toInt(val)))}; - case(#Int(val)){ Float.fromInt(val)}; - case(#Nat8(val)){valueUnstableToFloat(#Int(Nat8.toNat(val)))}; - case(#Nat(val)){valueUnstableToFloat(#Int(val))};//will throw on overflow - case(#Nat16(val)){valueUnstableToFloat(#Int(Nat16.toNat(val)))}; - case(#Nat32(val)){valueUnstableToFloat(#Int(Nat32.toNat(val)))};//will throw on overflow - case(#Nat64(val)){valueUnstableToFloat(#Int(Nat64.toNat(val)))}; - case(_){assert(false);/*unreachable*/0;}; - }; - }; - - public func valueUnstableToText(val : CandyValueUnstable) : Text { - switch(val){ - case(#Text(val)){ val}; - case(#Nat64(val)){ Nat64.toText(val)}; - case(#Nat32(val)){ Nat32.toText(val)}; - case(#Nat16(val)){ Nat16.toText(val)}; - case(#Nat8(val)){ Nat8.toText(val)}; - case(#Nat(val)){ Nat.toText(val)}; - case(#Int64(val)){ Int64.toText(val)}; - case(#Int32(val)){ Int32.toText(val)}; - case(#Int16(val)){ Int16.toText(val)}; - case(#Int8(val)){ Int8.toText(val)}; - case(#Int(val)){ Int.toText(val)}; - case(#Bool(val)){ Bool.toText(val)}; - case(#Float(val)){ Float.format(#exact, val)}; - case(#Option(val)){ - switch(val){ - case(null){ "null"}; - case(?val){valueUnstableToText(val)}; - }; - }; - //blob - case(#Blob(val)){ - valueUnstableToText(#Bytes(#frozen(Blob.toArray(val)))); - }; - //class - case(#Class(val)){ //this is currently not parseable and should probably just be used for debuging. It would be nice to output candid. - - var t = "{"; - for(thisItem in val.vals()){ - t := t # thisItem.name # ":" # (if(thisItem.immutable == false){"var "}else{""}) # valueUnstableToText(thisItem.value) # "; "; - }; - - return Text.trimEnd(t, #text(" ")) # "}"; - - - }; - //principal - case(#Principal(val)){ Principal.toText(val)}; - //array - case(#Array(val)){ - switch(val){ - case(#frozen(val)){ - var t = "["; - for(thisItem in val.vals()){ - t := t # "{" # valueUnstableToText(thisItem) # "} "; - }; - - return Text.trimEnd(t, #text(" ")) # "]"; - }; - case(#thawed(val)){ - var t = "["; - for(thisItem in val.vals()){ - t := t # "{" #valueUnstableToText(thisItem) # "} "; - }; - - return Text.trimEnd(t, #text(" ")) # "]"; - }; - }; - }; - //floats - case(#Floats(val)){ - switch(val){ - case(#frozen(val)){ - var t = "["; - for(thisItem in val.vals()){ - t := t # Float.format(#exact, thisItem) # " "; - }; - - return Text.trimEnd(t, #text(" ")) # "]"; - }; - case(#thawed(val)){ - var t = "["; - for(thisItem in val.vals()){ - t := t # Float.format(#exact, thisItem) # " "; - }; - - return Text.trimEnd(t, #text(" ")) # "]"; - }; - }; - }; - //bytes - case(#Bytes(val)){ - switch(val){ - case(#frozen(val)){ - - return Hex.encode(val); - }; - case(#thawed(val)){ - - return Hex.encode(val.toArray()); - }; - }; - }; - case(_){assert(false);/*unreachable*/"";}; - }; - }; - - public func valueUnstableToPrincipal(val : CandyValueUnstable) : Principal { - switch(val){ - case(#Principal(val)){ val}; - case(_){assert(false);/*unreachable*/Principal.fromText("");}; - }; - }; - - public func valueUnstableToBool(val : CandyValueUnstable) : Bool { - switch(val){ - case(#Bool(val)){ val}; - case(_){assert(false);/*unreachable*/false;}; - }; - }; - - public func valueUnstableToBlob(val : CandyValueUnstable) : Blob { - switch(val){ - - case(#Blob(val)){ val}; - case(#Bytes(val)){ - - Blob.fromArray( - switch(val){ - case(#frozen(val)){val;}; - case(#thawed(val)){val.toArray()}; - - } - )}; - case(#Text(val)){ - Blob.fromArray(textToBytes(val)) - }; - case(#Int(val)){ - Blob.fromArray(intToBytes(val)) - }; - case(#Nat(val)){ - Blob.fromArray(natToBytes(val)) - }; - case(#Nat8(val)){ - - Blob.fromArray([val]) - }; - case(#Nat16(val)){ - Blob.fromArray(nat16ToBytes(val)) - }; - case(#Nat32(val)){ - Blob.fromArray(nat32ToBytes(val)) - }; - case(#Nat64(val)){ - Blob.fromArray(nat64ToBytes(val)) - }; - case(#Principal(val)){ - - Principal.toBlob(val); - }; - //todo: could add all conversions here - case(_){assert(false);/*unreachable*/"\00";}; - }; - }; - - //gets the array of candy values out of an array - public func valueUnstableToValueArray(val : CandyValueUnstable) : [CandyValueUnstable] { - - switch(val){ - case(#Array(val)){ - switch(val){ - case(#frozen(val)){val}; - case(#thawed(val)){val.toArray()}; - }; - }; - //todo: could add all conversions here - case(_){assert(false);/*unreachable*/[];}; - }; - }; + public func valueToBytes(val : CandyValue) : [Nat8]{ switch(val){ @@ -914,48 +492,18 @@ module { case(#Bytes(val)){ switch(val){ case(#frozen(val)){ val}; - case(#thawed(val)){ val}; + case(#thawed(val)){ SB.toArray(val)}; }; }; case(#Floats(val)){Prelude.nyi()}; case(#Nats(val)){Prelude.nyi()}; + case(#Dictionary(val)){Prelude.nyi()}; case(#Empty){ []}; } }; - public func valueUnstableToBytes(val : CandyValueUnstable) : [Nat8]{ - switch(val){ - case(#Int(val)){intToBytes(val)}; - case(#Int8(val)){valueUnstableToBytes(#Int(valueUnstableToInt(#Int8(val))))}; - case(#Int16(val)){valueUnstableToBytes(#Int(valueUnstableToInt(#Int16(val))))}; - case(#Int32(val)){valueUnstableToBytes(#Int(valueUnstableToInt(#Int32(val))))}; - case(#Int64(val)){valueUnstableToBytes(#Int(valueUnstableToInt(#Int64(val))))}; - case(#Nat(val)){ natToBytes(val)}; - case(#Nat8(val)){ [val]}; - case(#Nat16(val)){nat16ToBytes(val)}; - case(#Nat32(val)){nat32ToBytes(val)}; - case(#Nat64(val)){nat64ToBytes(val)}; - case(#Float(val)){Prelude.nyi()}; - case(#Text(val)){textToBytes(val)}; - case(#Bool(val)){boolToBytes(val)}; - case(#Blob(val)){ Blob.toArray(val)}; - case(#Class(val)){Prelude.nyi()}; - case(#Principal(val)){principalToBytes(val)}; - case(#Option(val)){Prelude.nyi()}; - case(#Array(val)){Prelude.nyi()}; - case(#Bytes(val)){ - switch(val){ - case(#frozen(val)){ val}; - case(#thawed(val)){ val.toArray()}; - }; - }; - case(#Floats(val)){Prelude.nyi()}; - case(#Nats(val)){Prelude.nyi()}; - case(#Empty){ []}; - } - }; - public func valueUnstableToBytesBuffer(val : CandyValueUnstable) : Buffer.Buffer{ + public func valueToBytesBuffer(val : CandyValue) : SB.StableBuffer{ switch (val){ case(#Bytes(val)){ switch(val){ @@ -964,44 +512,12 @@ module { return toBuffer(val); }; - case(#thawed(val)){ val}; + case(#thawed(val)){ + return (val)}; }; }; case(_){ - toBuffer(valueUnstableToBytes(val));//may throw for uncovertable types - }; - }; - }; - - public func valueUnstableToFloatsBuffer(val : CandyValueUnstable) : Buffer.Buffer{ - switch (val){ - case(#Floats(val)){ - switch(val){ - case(#frozen(val)){ - - toBuffer(val); - }; - case(#thawed(val)){ val}; - }; - }; - case(_){ - toBuffer([valueUnstableToFloat(val)]); //may throw for unconvertable types - }; - }; - }; - - public func valueUnstableToNatsBuffer(val : CandyValueUnstable) : Buffer.Buffer{ - switch (val){ - case(#Nats(val)){ - switch(val){ - case(#frozen(val)){ - toBuffer(val); - }; - case(#thawed(val)){val}; - }; - }; - case(_){ - toBuffer([valueUnstableToNat(val)]); //may throw for unconvertable types + toBuffer(valueToBytes(val));//may throw for uncovertable types }; }; }; @@ -1013,11 +529,11 @@ module { // The following functions easily creates a buffer from an arry of any type ////////////////////////////////////////////////////////////////////// - public func toBuffer(x :[T]) : Buffer.Buffer{ + public func toBuffer(x :[T]) : SB.StableBuffer{ - let thisBuffer = Buffer.Buffer(x.size()); + let thisBuffer = SB.initPresized(x.size()); for(thisItem in x.vals()){ - thisBuffer.add(thisItem); + SB.add(thisBuffer,thisItem); }; return thisBuffer; }; @@ -1221,8 +737,8 @@ module { test := b > 0; }; let result = toBuffer([c]); - result.append(toBuffer(List.toArray(bytes))); - result.toArray(); + SB.append(result, toBuffer(List.toArray(bytes))); + SB.toArray(result); //Array.append([c],List.toArray(bytes)); }; @@ -1262,21 +778,6 @@ module { }; }; - public func unwrapOptionValueUnstable(val : CandyValueUnstable): CandyValueUnstable{ - - switch(val){ - case(#Option(val)){ - switch(val){ - case(null){ - return #Empty; - }; - case(?val){ - return val; - } - }; - }; - case(_){val}; - }; - }; + } \ No newline at end of file diff --git a/src/properties.mo b/src/properties.mo index 4f067ca..12b91b6 100644 --- a/src/properties.mo +++ b/src/properties.mo @@ -18,167 +18,13 @@ import Types "types"; module { - type PropertiesUnstable = Types.PropertiesUnstable; type Query = Types.Query; type PropertyError = Types.PropertyError; - type UpdateUnstable = Types.UpdateUnstable; type Property = Types.Property; - type PropertyUnstable = Types.PropertyUnstable; type CandyValue = Types.CandyValue; type Properties = Types.Properties; type Update = Types.Update; - private func toPropertyUnstableMap(ps : PropertiesUnstable) : HashMap.HashMap { - - let m = HashMap.HashMap(ps.size(), Text.equal, Text.hash); - for (property in ps.vals()) { - m.put(property.name, property); - }; - m; - }; - - private func fromPropertyUnstableMap(m : HashMap.HashMap) : PropertiesUnstable { - var ps : Buffer.Buffer = Buffer.Buffer(m.size()); - for ((_, p) in m.entries()) { - ps.add(p); - }; - ps.toArray(); - }; - - - // Returns a subset of from properties based on the given query. - // NOTE: ignores unknown properties. - public func getPropertiesUnstable(properties : PropertiesUnstable, qs : [Query]) : Result.Result { - let m = toPropertyUnstableMap(properties); - var ps : Buffer.Buffer = Buffer.Buffer(m.size()); - for (q in qs.vals()) { - switch (m.get(q.name)) { - case (null) { - // Query contained an unknown property. - return #err(#NotFound); - }; - case (? p) { - switch (p.value) { - case (#Class(c)) { - if (q.next.size() == 0) { - // Return every sub-attribute attribute. - ps.add(p); - } else { - let sps = switch (getPropertiesUnstable(c, q.next)) { - case (#err(e)) { return #err(e); }; - case (#ok(v)) { v; }; - }; - - ps.add({ - name = p.name; - value = #Class(sps); - immutable = p.immutable; - }); - }; - }; - case (other) { - // Not possible to get sub-attribute of a non-class property. - if (q.next.size() != 0) { - return #err(#NotFound); - }; - ps.add(p); - }; - } - }; - }; - }; - #ok(ps.toArray()); - }; - - // Updates the given properties based on the given update query. - // NOTE: creates unknown properties. - public func updatePropertiesUnstable(properties : PropertiesUnstable, us : [UpdateUnstable]) : Result.Result { - let m = toPropertyUnstableMap(properties); - for (u in us.vals()) { - switch (m.get(u.name)) { - case (null) { - // Update contained an unknown property, so it gets created. - switch (u.mode) { - case (#Next(sus)) { - let sps = switch(updatePropertiesUnstable([], sus)) { - case (#err(e)) { return #err(e); }; - case (#ok(v)) { v; }; - }; - - m.put(u.name, { - name = u.name; - value = #Class(sps); - immutable = false; - }); - }; - case (#Set(v)) { - m.put(u.name, { - name = u.name; - value = v; - immutable = false; - }); - }; - case (#Lock(v)) { - m.put(u.name, { - name = u.name; - value = v; - immutable = true; - }); - }; - }; - }; - case (? p) { - // Can not update immutable property. - if (p.immutable) { - return #err(#Immutable); - }; - switch (u.mode) { - case (#Next(sus)) { - switch (p.value) { - case (#Class(c)) { - let sps = switch(updatePropertiesUnstable(c, sus)) { - case (#err(e)) { return #err(e); }; - case (#ok(v)) { v; }; - }; - - m.put(u.name, { - name = p.name; - value = #Class(sps); - immutable = false; - }); - }; - case (other) { - // Not possible to update sub-attribute of a non-class property. - return #err(#NotFound); - }; - }; - return #err(#NotFound); - }; - case (#Set(v)) { - m.put(u.name, { - name = p.name; - value = v; - immutable = false; - }); - }; - case (#Lock(v)) { - m.put(u.name, { - name = p.name; - value = v; - immutable = true; - }); - }; - }; - }; - }; - }; - - #ok(fromPropertyUnstableMap(m)); - }; - - - - public func getClassProperty(val: CandyValue, name : Text) : ?Property{ switch(val){ diff --git a/src/types.mo b/src/types.mo index 10beb2a..dae50b2 100644 --- a/src/types.mo +++ b/src/types.mo @@ -9,30 +9,14 @@ //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. /////////////////////////////// -import Buffer "mo:base/Buffer"; -import Array "mo:base/Array"; +import RB "mo:stablerbtree/StableRBTree"; +import SB "mo:stablebuffer/StableBuffer"; -module { - - public type PropertiesUnstable = [PropertyUnstable]; - public type PropertyUnstable = {name : Text; value : CandyValueUnstable; immutable : Bool}; - - public type UpdateRequestUnstable = { - id : Text; - update : [UpdateUnstable]; - }; - public type UpdateUnstable = { - name : Text; - mode : UpdateModeUnstable; - }; +module { - public type UpdateModeUnstable = { - #Set : CandyValueUnstable; - #Lock : CandyValueUnstable; - #Next : [UpdateUnstable]; - }; + //////////////////////////////////// // @@ -44,9 +28,24 @@ module { // The following section is issued under the MIT License Copyright (c) 2021 Departure Labs: /////////////////////////////////// + public type Properties = [Property]; public type Property = {name : Text; value : CandyValue; immutable : Bool}; - public type Properties = [Property]; + public type UpdateRequest = { + id : Text; + update : [Update]; + }; + + public type Update = { + name : Text; + mode : UpdateMode; + }; + + public type UpdateMode = { + #Set : CandyValue; + #Lock : CandyValue; + #Next : [Update]; + }; public type PropertyError = { #Unauthorized; @@ -66,29 +65,10 @@ module { #Some : [Query]; // Returns a select set of properties based on the name. }; - // Specifies the properties that should be updated to a certain value. - public type UpdateRequest = { - id : Text; - update : [Update]; - }; - - public type Update = { - name : Text; - mode : UpdateMode; - }; - - public type UpdateMode = { - #Set : CandyValue; - #Lock : CandyValue; - #Next : [Update]; - }; - /////////////////////////////////// // // End departurelabs' property.mo types // - // Code below resumes the Copyright ARAMAKME - // /////////////////////////////////// //stable @@ -110,245 +90,46 @@ module { #Class : [Property]; #Principal : Principal; #Option : ?CandyValue; + #Dictionary : RB.Tree; #Array : { #frozen: [CandyValue]; - #thawed: [CandyValue]; //need to thaw when going to CandyValueUnstable + #thawed: SB.StableBuffer; }; #Nats: { #frozen: [Nat]; - #thawed: [Nat]; //need to thaw when going to TrixValueUnstable + #thawed: SB.StableBuffer; }; #Floats: { #frozen: [Float]; - #thawed: [Float]; //need to thaw when going to CandyValueUnstable - }; - #Bytes : { - #frozen: [Nat8]; - #thawed: [Nat8]; //need to thaw when going to CandyValueUnstable - }; - #Empty; - }; - - //unstable - public type CandyValueUnstable = { - #Int : Int; - #Int8: Int8; - #Int16: Int16; - #Int32: Int32; - #Int64: Int64; - #Nat : Nat; - #Nat8 : Nat8; - #Nat16 : Nat16; - #Nat32 : Nat32; - #Nat64 : Nat64; - #Float : Float; - #Text : Text; - #Bool : Bool; - #Blob : Blob; - #Class : [PropertyUnstable]; - #Principal : Principal; - #Floats : { - #frozen: [Float]; - #thawed: Buffer.Buffer; - }; - #Nats: { - #frozen: [Nat]; - #thawed: Buffer.Buffer; //need to thaw when going to TrixValueUnstable - }; - #Array : { - #frozen: [CandyValueUnstable]; - #thawed: Buffer.Buffer; //need to thaw when going to CandyValueUnstable + #thawed: SB.StableBuffer; }; - #Option : ?CandyValueUnstable; #Bytes : { #frozen: [Nat8]; - #thawed: Buffer.Buffer; //need to thaw when going to CandyValueUnstable + #thawed: SB.StableBuffer; }; #Empty; }; //a data chunk should be no larger than 2MB so that it can be shipped to other canisters - public type DataChunk = CandyValueUnstable; - public type DataZone = Buffer.Buffer; - public type Workspace = Buffer.Buffer; + public type DataChunk = CandyValue; + public type DataZone = SB.StableBuffer; + public type Workspace = SB.StableBuffer; public type AddressedChunk = (Nat, Nat, CandyValue); public type AddressedChunkArray = [AddressedChunk]; - public type AddressedChunkBuffer = Buffer.Buffer; - - public func stabalizeValue(item : CandyValueUnstable) : CandyValue{ - switch(item){ - case(#Int(val)){ #Int(val)}; - case(#Int8(val)){ #Int8(val)}; - case(#Int16(val)){ #Int16(val)}; - case(#Int32(val)){ #Int32(val)}; - case(#Int64(val)){ #Int64(val)}; - case(#Nat(val)){ #Nat(val)}; - case(#Nat8(val)){ #Nat8(val)}; - case(#Nat16(val)){ #Nat16(val)}; - case(#Nat32(val)){ #Nat32(val)}; - case(#Nat64(val)){ #Nat64(val)}; - case(#Float(val)){ #Float(val)}; - case(#Text(val)){ #Text(val)}; - case(#Bool(val)){ #Bool(val)}; - case(#Blob(val)){ #Blob(val)}; - - case(#Class(val)){ - #Class( - Array.tabulate(val.size(), func(idx){ - stabalizeProperty(val[idx]); - })); - }; - case(#Principal(val)){ #Principal(val)}; - case(#Array(val)){ - switch(val){ - case(#frozen(val)){#Array(#frozen(stabalizeValueArray(val)))}; - case(#thawed(val)){#Array(#thawed(stabalizeValueArray(val.toArray())))}; - }; - }; - case(#Option(val)){ - switch(val){ - case(null){ #Option(null)}; - case(?val){#Option(?stabalizeValue(val))}; - }; - }; - case(#Bytes(val)){ - switch(val){ - case(#frozen(val)){ #Bytes(#frozen(val))}; - case(#thawed(val)){ #Bytes(#thawed(val.toArray()))}; - }; - }; - case(#Floats(val)){ - switch(val){ - case(#frozen(val)){ #Floats(#frozen(val))}; - case(#thawed(val)){ #Floats(#thawed(val.toArray()))}; - }; - }; - case(#Nats(val)){ - switch(val){ - case(#frozen(val)){ #Nats(#frozen(val))}; - case(#thawed(val)){ #Nats(#thawed(val.toArray()))}; - }; - }; - case(#Empty){ #Empty}; - } - }; - - public func destabalizeValue(item : CandyValue) : CandyValueUnstable{ - switch(item){ - case(#Int(val)){ #Int(val)}; - case(#Int8(val)){ #Int8(val)}; - case(#Int16(val)){ #Int16(val)}; - case(#Int32(val)){ #Int32(val)}; - case(#Int64(val)){ #Int64(val)}; - case(#Nat(val)){ #Nat(val)}; - case(#Nat8(val)){ #Nat8(val)}; - case(#Nat16(val)){ #Nat16(val)}; - case(#Nat32(val)){ #Nat32(val)}; - case(#Nat64(val)){ #Nat64(val)}; - case(#Float(val)){ #Float(val)}; - case(#Text(val)){ #Text(val)}; - case(#Bool(val)){ #Bool(val)}; - case(#Blob(val)){ #Blob(val)}; - case(#Class(val)){ - #Class( - Array.tabulate(val.size(), func(idx){ - destabalizeProperty(val[idx]); - })); - }; - case(#Principal(val)){#Principal(val)}; - case(#Array(val)){ - switch(val){ - case(#frozen(val)){#Array(#frozen(destabalizeValueArray(val)))}; - case(#thawed(val)){#Array(#thawed(toBuffer(destabalizeValueArray(val))))}; - }; - }; - case(#Option(val)){ - switch(val){ - case(null){ #Option(null)}; - case(?val){#Option(?destabalizeValue(val))}; - }; - }; - case(#Bytes(val)){ - switch(val){ - case(#frozen(val)){ #Bytes(#frozen(val))}; - case(#thawed(val)){#Bytes(#thawed(toBuffer(val)))}; - }; - }; - case(#Floats(val)){ - switch(val){ - case(#frozen(val)){ #Floats(#frozen(val))}; - case(#thawed(val)){#Floats(#thawed(toBuffer(val)))}; - }; - }; - case(#Nats(val)){ - switch(val){ - case(#frozen(val)){ #Nats(#frozen(val))}; - case(#thawed(val)){#Nats(#thawed(toBuffer(val)))}; - }; - }; - case(#Empty){ #Empty}; - } - }; - - public func stabalizeProperty(item : PropertyUnstable) : Property{ - return { - name = item.name; - value = stabalizeValue(item.value); - immutable = item.immutable; - } - }; - - public func destabalizeProperty(item : Property) : PropertyUnstable{ - return { - name = item.name; - value = destabalizeValue(item.value); - immutable = item.immutable; - } - }; - - public func stabalizeValueArray(items : [CandyValueUnstable]) : [CandyValue]{ - - let finalItems = Buffer.Buffer(items.size()); - for(thisItem in items.vals()){ - finalItems.add(stabalizeValue(thisItem)); - }; - - return finalItems.toArray(); - + public type AddressedChunkBuffer = SB.StableBuffer; - }; - - public func destabalizeValueArray(items : [CandyValue]) : [CandyValueUnstable]{ - - let finalItems = Buffer.Buffer(items.size()); - for(thisItem in items.vals()){ - finalItems.add(destabalizeValue(thisItem)); - }; - - return finalItems.toArray(); - }; - - public func stabalizeValueBuffer(items : DataZone) : [CandyValue]{ - - let finalItems = Buffer.Buffer(items.size()); - for(thisItem in items.vals()){ - finalItems.add(stabalizeValue(thisItem)); - }; - - return finalItems.toArray(); - - }; + ////////////////////////////////////////////////////////////////////// // The following functions easily creates a buffer from an arry of any type ////////////////////////////////////////////////////////////////////// - public func toBuffer(x :[T]) : Buffer.Buffer{ + public func toBuffer(x :[T]) : SB.StableBuffer{ - let thisBuffer = Buffer.Buffer(x.size()); + var thisBuffer = SB.initPresized(x.size()); for(thisItem in x.vals()){ - thisBuffer.add(thisItem); + SB.add(thisBuffer, thisItem); }; return thisBuffer; }; diff --git a/src/workspace.mo b/src/workspace.mo index 0712e6b..d897622 100644 --- a/src/workspace.mo +++ b/src/workspace.mo @@ -19,11 +19,13 @@ import Nat "mo:base/Nat"; import Types "types"; import Conversion "conversion"; import Clone "clone"; +import RB "mo:stablerbtree/StableRBTree"; +import SB "mo:stablebuffer/StableBuffer"; module { type CandyValue = Types.CandyValue; - type CandyValueUnstable = Types.CandyValueUnstable; + type Workspace = Types.Workspace; type DataZone = Types.DataZone; type AddressedChunkArray = Types.AddressedChunkArray; @@ -42,8 +44,8 @@ module { public func countAddressedChunksInWorkspace(x : Workspace) : Nat{ var chunks = 0; - for (thisZone in Iter.range(0, x.size() - 1)){ - chunks += x.get(thisZone).size(); + for (thisZone in Iter.range(0, SB.size(x) - 1)){ + chunks += SB.size(SB.get(x, thisZone)); }; chunks; @@ -51,12 +53,12 @@ module { public func emptyWorkspace() : Workspace { - return Buffer.Buffer(1); + return SB.initPresized(1); }; public func initWorkspace(size : Nat) : Workspace { - return Buffer.Buffer(size); + return SB.initPresized(size); }; //variants take up 2 bytes as long as there are fewer than 32 item in the enum @@ -123,8 +125,9 @@ module { return size; }; case(#thawed(val)){ + //todo: add size from metadata var size = 0; - for(thisItem in val.vals()){ + for(thisItem in SB.vals(val)){ size += 1 + getValueSize(thisItem); }; @@ -135,13 +138,17 @@ module { case(#Bytes(val)){ switch(val){ case(#frozen(val)){val.size() + 2}; - case(#thawed(val)){val.size() + 2}; + case(#thawed(val)){ + //todo: add size from metadata + SB.size(val) + 2}; }; }; case(#Floats(val)){ switch(val){ case(#frozen(val)){(val.size() * 4) + 2}; - case(#thawed(val)){(val.size() * 4) + 2}; + case(#thawed(val)){ + //todo: add size from metadata + (SB.size(val) * 4) + 2}; }; }; case(#Nats(val)){ @@ -155,8 +162,9 @@ module { return size; }; case(#thawed(val)){ + //todo: add size from metadata var size = 0; - for(thisItem in val.vals()){ + for(thisItem in SB.vals(val)){ size += 1 + getValueSize(#Nat(thisItem)); }; @@ -164,122 +172,22 @@ module { }; }; }; - case(#Empty){0}; - }; - - return varSize; - }; - - public func getValueUnstableSize(item : CandyValueUnstable) : Nat{ - - let varSize = switch(item){ - case(#Int(val)){ - var a : Nat = 0; - var b : Nat = Int.abs(val); - var test = true; - while test { - a += 1; - b := b / 256; - test := b > 0; - }; - a + 1;//add the sign - }; - case(#Int8(val)){1}; - case(#Int16(val)){2}; - case(#Int32(val)){3}; - case(#Int64(val)){4}; - case(#Nat(val)){ - var a : Nat = 0; - var b = val; - var test = true; - while test { - a += 1; - b := b / 256; - test := b > 0; - }; - a; - }; - case(#Nat8(val)){1}; - case(#Nat16(val)){2}; - case(#Nat32(val)){3}; - case(#Nat64(val)){4}; - case(#Float(val)){4}; - case(#Text(val)){val.size()*4}; - case(#Bool(val)){1}; - case(#Blob(val)){val.size()}; - case(#Class(val)){ + case(#Dictionary(val)){ + //todo: add size from metadata and pointers var size = 0; - for(thisItem in val.vals()){ - size += 1 + (thisItem.name.size() * 4) + getValueUnstableSize(thisItem.value); + for(thisItem in RB.entries(val)){ + size += getValueSize(thisItem.0) + getValueSize(thisItem.1); }; - return size; }; - case(#Principal(val)){Conversion.principalToBytes(val).size()};//don't like this but need to confirm it is constant - case(#Option(val)){ - switch val{ - case(null){0}; - case(?val){ getValueUnstableSize(val)} - } - }; - case(#Array(val)){ - switch(val){ - case(#frozen(val)){ - var size = 0; - for(thisItem in val.vals()){ - size += 1 + getValueUnstableSize(thisItem); - }; - - return size; - }; - case(#thawed(val)){ - var size = 0; - for(thisItem in val.vals()){ - size += 1 + getValueUnstableSize(thisItem); - }; - - return size; - }; - }; - }; - - case(#Bytes(val)){ - switch(val){ - case(#frozen(val)){val.size() + 2}; - case(#thawed(val)){val.size() + 2}; - }; - }; - case(#Floats(val)){ - switch(val){ - case(#frozen(val)){(val.size() * 4) + 2}; - case(#thawed(val)){(val.size() * 4) + 2}; - }; - }; - case(#Nats(val)){ - switch(val){ - case(#frozen(val)){ - var size = 0; - for(thisItem in val.vals()){ - size += 1 + getValueUnstableSize(#Nat(thisItem)); - }; - - return size; - }; - case(#thawed(val)){ - var size = 0; - for(thisItem in val.vals()){ - size += 1 + getValueUnstableSize(#Nat(thisItem)); - }; - - return size; - }; - }; - }; case(#Empty){0}; }; - return varSize + 2; + + return varSize; }; + + public func workspaceToAddressedChunkArray(x : Workspace) : AddressedChunkArray { @@ -287,8 +195,8 @@ module { var currentZone = 0; var currentChunk = 0; let result = Array.tabulate(countAddressedChunksInWorkspace(x), func(thisChunk){ - let thisChunk = (currentZone, currentChunk, Types.stabalizeValue(x.get(currentZone).get(currentChunk))); - if(currentChunk == Nat.sub(x.get(currentZone).size(),1)){ + let thisChunk = (currentZone, currentChunk, SB.get(SB.get(x, currentZone),currentChunk)); + if(currentChunk == Nat.sub(SB.size(SB.get(x, currentZone)),1)){ currentZone += 1; currentChunk := 0; } else { @@ -304,21 +212,21 @@ module { var currentZone = 0; var currentChunk = 0; - let ws = Buffer.Buffer(x.size()); - for(thisZone in x.vals()){ + let ws = SB.initPresized(SB.size(x)); + for(thisZone in SB.vals(x)){ - let tz = Buffer.Buffer(thisZone.size()); - ws.add(tz); - for(thisDataChunk in thisZone.vals()){ - tz.add(Clone.cloneValueUnstable(thisDataChunk)); + let tz = SB.initPresized(SB.size(thisZone)); + SB.add(ws,tz); + for(thisDataChunk in SB.vals(thisZone)){ + SB.add(tz, Clone.clone(thisDataChunk)); }; }; return ws; }; - public func fromAddressedChunks(x : AddressedChunkArray) : Workspace{ - let result = Buffer.Buffer(x.size()); + public func fromAddressedChunks(x : AddressedChunkArray) : Workspace { + let result = SB.initPresized(x.size()); fileAddressedChunks(result, x); return result; }; @@ -327,7 +235,7 @@ module { for (thisChunk : AddressedChunk in Array.vals(x)){ - let resultSize : Nat = workspace.size(); + let resultSize : Nat = SB.size(workspace); let targetZone = thisChunk.0 + 1; if(targetZone <= resultSize){ @@ -336,29 +244,29 @@ module { //append zone for (thisIndex in Iter.range(resultSize, targetZone-1)){ - workspace.add(Buffer.Buffer(1)); + SB.add(workspace, SB.initPresized(1)); }; }; - let thisZone = workspace.get(thisChunk.0); + let thisZone = SB.get(workspace,thisChunk.0); - if(thisChunk.1 + 1 <= thisZone.size()){ + if(thisChunk.1 + 1 <= SB.size(thisZone)){ //zone exists - thisZone.put(thisChunk.1, Types.destabalizeValue(thisChunk.2)); + SB.put(thisZone, thisChunk.1, thisChunk.2); } else { //append zone - for (newChunk in Iter.range(thisZone.size(), thisChunk.1)){ + for (newChunk in Iter.range(SB.size(thisZone), thisChunk.1)){ let newBuffer = if(thisChunk.1 == newChunk){ //we know the size - Types.destabalizeValue(thisChunk.2); + thisChunk.2; } else { #Empty; }; - thisZone.add(newBuffer); + SB.add(thisZone,newBuffer); }; //return thisZone.get(thisChunk.1); }; @@ -372,8 +280,8 @@ module { public func getDataZoneSize(dz: DataZone) : Nat { var size : Nat = 0; - for(thisChunk in dz.vals()){ - size += getValueUnstableSize(thisChunk); + for(thisChunk in SB.vals(dz)){ + size += getValueSize(thisChunk); }; return size; @@ -392,12 +300,12 @@ module { if(handBrake > 10000){ break chunking;}; var foundBytes = 0; //calc bytes - for(thisZone in Iter.range(zoneTracker, _workspace.size()-1)){ - for(thisChunk in Iter.range(chunkTracker, _workspace.get(thisZone).size()-1)){ + for(thisZone in Iter.range(zoneTracker, SB.size(_workspace)-1)){ + for(thisChunk in Iter.range(chunkTracker, SB.size(SB.get(_workspace, thisZone)) - 1)){ - let thisItem = _workspace.get(thisZone).get(thisChunk); + let thisItem = SB.get(SB.get(_workspace,thisZone), thisChunk); - let newSize = foundBytes + getValueUnstableSize(thisItem); + let newSize = foundBytes + getValueSize(thisItem); if( newSize > _maxChunkSize) { @@ -429,18 +337,18 @@ module { var zoneTracker = 0; var chunkTracker = 0; - let resultBuffer = Buffer.Buffer(1); + let resultBuffer = SB.initPresized(1); label chunking while (1==1){ handBrake += 1; if(handBrake > 10000){ break chunking;}; var foundBytes = 0; //calc bytes - for(thisZone in Iter.range(zoneTracker, _workspace.size()-1)){ - for(thisChunk in Iter.range(chunkTracker, _workspace.get(thisZone).size()-1)){ + for(thisZone in Iter.range(zoneTracker, SB.size(_workspace)-1)){ + for(thisChunk in Iter.range(chunkTracker, SB.size(SB.get(_workspace ,thisZone)) - 1)){ - let thisItem = _workspace.get(thisZone).get(thisChunk); + let thisItem = SB.get(SB.get(_workspace, thisZone), thisChunk); - let newSize = foundBytes + getValueUnstableSize(thisItem); + let newSize = foundBytes + getValueSize(thisItem); if( newSize > _maxChunkSize) { //went over bytes @@ -454,7 +362,7 @@ module { }; if(currentChunk == _chunkID){ //add it to our return - resultBuffer.add((thisZone, thisChunk, Types.stabalizeValue(thisItem))); + SB.add(resultBuffer, (thisZone, thisChunk, thisItem)); }; @@ -491,30 +399,30 @@ module { - public func byteBufferDataZoneToBuffer(dz : DataZone): Buffer.Buffer>{ + public func byteBufferDataZoneToBuffer(dz : DataZone): SB.StableBuffer>{ - let result = Buffer.Buffer>(dz.size()); - for(thisItem in dz.vals()){ - result.add(Conversion.valueUnstableToBytesBuffer(thisItem)); + let result = SB.initPresized>(SB.size(dz)); + for(thisItem in SB.vals(dz)){ + SB.add(result, Conversion.valueToBytesBuffer(thisItem)); }; return result; }; - public func byteBufferChunksToValueUnstableBufferDataZone(buffer : Buffer.Buffer>): DataZone{ + public func byteBufferChunksToValueUnstableBufferDataZone(buffer : SB.StableBuffer>): DataZone{ - let result = Buffer.Buffer(buffer.size()); - for(thisItem in buffer.vals()){ - result.add(#Bytes(#thawed(thisItem))); + let result = SB.initPresized(SB.size(buffer)); + for(thisItem in SB.vals(buffer)){ + SB.add(result, #Bytes(#thawed(thisItem))); }; return result; }; - public func initDataZone(val : CandyValueUnstable) : DataZone{ + public func initDataZone(val : CandyValue) : DataZone{ - let result = Buffer.Buffer(1); - result.add(val); + let result = SB.initPresized(1); + SB.add(result, val); return result; }; diff --git a/tests/test_runner.mo b/tests/test_runner.mo index 4f1ce89..52fcc53 100644 --- a/tests/test_runner.mo +++ b/tests/test_runner.mo @@ -22,9 +22,9 @@ shared (deployer) actor class test_runner() = this { public shared func test() : async {#success; #fail : Text} { - let suite = S.suite("test nft", [ + let suite = S.suite("test candy", [ //test getting witness returns empty if no witness - S.test("testOwner", switch(await testConversions()){case(#success){true};case(_){false};}, M.equals(T.bool(true))), + S.test("testConversions", switch(await testConversions()){case(#success){true};case(_){false};}, M.equals(T.bool(true))), ]); S.run(suite); @@ -32,16 +32,20 @@ shared (deployer) actor class test_runner() = this { return #success; }; - // US.2 - // US.3 public shared func testConversions() : async {#success; #fail : Text} { - Debug.print("running testOwner"); + Debug.print("running testConversions"); let owner = Principal.toText(deployer.caller); let suite = S.suite("test conversion", [ - - S.test("Nat32 is Nat", Conversion.valueUnstableToNat(#Nat32(10)), M.equals(T.nat(10))) + S.test("Nat8 is Nat", Conversion.valueToNat(#Nat8(10)), M.equals(T.nat(10))), + S.test("Nat16 is Nat", Conversion.valueToNat(#Nat16(10)), M.equals(T.nat(10))), + S.test("Nat32 is Nat", Conversion.valueToNat(#Nat32(10)), M.equals(T.nat(10))), + S.test("Nat64 is Nat", Conversion.valueToNat(#Nat64(10)), M.equals(T.nat(10))), + S.test("Nat8 is Text", Conversion.valueToText(#Nat8(10)), M.equals(T.text("10"))), + S.test("Nat16 is Text", Conversion.valueToText(#Nat16(10)), M.equals(T.text("10"))), + S.test("Nat32 is Text", Conversion.valueToText(#Nat32(10)), M.equals(T.text("10"))), + S.test("Nat64 is Text", Conversion.valueToText(#Nat64(10)), M.equals(T.text("10"))) ]); S.run(suite); diff --git a/vessel.dhall b/vessel.dhall index 19be4cd..0c5a35a 100644 --- a/vessel.dhall +++ b/vessel.dhall @@ -1,4 +1,4 @@ { - dependencies = [ "base", "matchers" ], + dependencies = [ "base", "matchers", "stablerbtree", "stablebuffer" ], compiler = None Text }