Skip to content

Commit

Permalink
Added Lock to Properties, removed Array.append, textToNat, valueToPro…
Browse files Browse the repository at this point in the history
…perties, dfx 0.9.3
  • Loading branch information
skilesare committed May 11, 2022
1 parent 7d11a49 commit 7a02acc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 22 deletions.
1 change: 1 addition & 0 deletions Example_Aramakme_License/example_lib.mo
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ module {

public type UpdateMode = {
#Set : CandyValue;
#Lock : CandyValue;
#Next : [Update];
};

Expand Down
2 changes: 1 addition & 1 deletion dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"packtool": "vessel sources"
}
},
"dfx": "0.9.2",
"dfx": "0.9.3",
"networks": {
"local": {
"bind": "127.0.0.1:8000",
Expand Down
22 changes: 20 additions & 2 deletions src/conversion.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,22 @@ module {
return textToByteBuffer(_text).toArray();
};

public func textToNat(phrase : Text) : ?Nat {
var theSum : Nat = 0;
Iter.iterate(Text.toIter(phrase), func (x : Char, n : Nat){
//todo: check for digits
theSum := theSum + ((Nat32.toNat(Char.toNat32(x)) - 48) * 10 ** (phrase.size()-n-1));
});
return ?theSum;
};

public func valueToProperties(val : CandyValue) : Types.Properties {
switch(val){
case(#Class(val)){ val};
case(_){assert(false);/*unreachable*/[];};
};
};

public func bytesToText(_bytes : [Nat8]) : Text{

var result : Text = "";
Expand Down Expand Up @@ -1182,8 +1198,10 @@ module {
bytes := List.push<Nat8>(a, bytes);
test := b > 0;
};

Array.append<Nat8>([c],List.toArray<Nat8>(bytes));
let result = toBuffer<Nat8>([c]);
result.append(toBuffer<Nat8>(List.toArray<Nat8>(bytes)));
result.toArray();
//Array.append<Nat8>([c],List.toArray<Nat8>(bytes));
};

public func bytesToInt(_bytes : [Nat8]) : Int{
Expand Down
65 changes: 46 additions & 19 deletions src/properties.mo
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,19 @@ module {
};

private func fromPropertyUnstableMap(m : HashMap.HashMap<Text,PropertyUnstable>) : PropertiesUnstable {

var ps : PropertiesUnstable = [];
var ps : Buffer.Buffer<PropertyUnstable> = Buffer.Buffer<PropertyUnstable>(m.size());
for ((_, p) in m.entries()) {
ps := Array.append(ps, [p]);
ps.add(p);
};
ps;
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<PropertiesUnstable, PropertyError> {
let m = toPropertyUnstableMap(properties);
var ps : PropertiesUnstable = [];
var ps : Buffer.Buffer<PropertyUnstable> = Buffer.Buffer<PropertyUnstable>(m.size());
for (q in qs.vals()) {
switch (m.get(q.name)) {
case (null) {
Expand All @@ -63,32 +62,32 @@ module {
case (#Class(c)) {
if (q.next.size() == 0) {
// Return every sub-attribute attribute.
ps := Array.append(ps, [p]);
ps.add(p);
} else {
let sps = switch (getPropertiesUnstable(c, q.next)) {
case (#err(e)) { return #err(e); };
case (#ok(v)) { v; };
};

ps := Array.append(ps, [{
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 := Array.append(ps, [p]);
ps.add(p);
};
}
};
};
};
#ok(ps);
#ok(ps.toArray());
};

// Updates the given properties based on the given update query.
Expand Down Expand Up @@ -119,6 +118,13 @@ module {
immutable = false;
});
};
case (#Lock(v)) {
m.put(u.name, {
name = u.name;
value = v;
immutable = true;
});
};
};
};
case (? p) {
Expand Down Expand Up @@ -155,6 +161,13 @@ module {
immutable = false;
});
};
case (#Lock(v)) {
m.put(u.name, {
name = p.name;
value = v;
immutable = true;
});
};
};
};
};
Expand Down Expand Up @@ -212,18 +225,18 @@ module {

private func fromPropertyMap(m : HashMap.HashMap<Text,Property>) : Properties {

var ps : Properties = [];
var ps : Buffer.Buffer<Property> = Buffer.Buffer(m.size());
for ((_, p) in m.entries()) {
ps := Array.append(ps, [p]);
ps.add(p);
};
ps;
ps.toArray();
};

// Returns a subset of from properties based on the given query.
// NOTE: ignores unknown properties.
public func getProperties(properties : Properties, qs : [Query]) : Result.Result<Properties, PropertyError> {
let m = toPropertyMap(properties);
var ps : Properties = [];
var ps : Buffer.Buffer<Property> = Buffer.Buffer<Property>(m.size());
for (q in qs.vals()) {
switch (m.get(q.name)) {
case (null) {
Expand All @@ -236,32 +249,32 @@ module {
case (#Class(c)) {
if (q.next.size() == 0) {
// Return every sub-attribute attribute.
ps := Array.append(ps, [p]);
ps.add(p);
} else {
let sps = switch (getProperties(c, q.next)) {
case (#err(e)) { return #err(e); };
case (#ok(v)) { v; };
};

ps := Array.append(ps, [{
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 := Array.append(ps, [p]);
ps.add(p);
};
}
};
};
};
#ok(ps);
#ok(ps.toArray());
};

// Updates the given properties based on the given update query.
Expand Down Expand Up @@ -292,6 +305,13 @@ module {
immutable = false;
});
};
case (#Lock(v)) {
m.put(u.name, {
name = u.name;
value = v;
immutable = true;
});
};
};
};
case (? p) {
Expand Down Expand Up @@ -328,6 +348,13 @@ module {
immutable = false;
});
};
case (#Lock(v)) {
m.put(u.name, {
name = p.name;
value = v;
immutable = true;
});
};
};
};
};
Expand Down
2 changes: 2 additions & 0 deletions src/types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module {

public type UpdateModeUnstable = {
#Set : CandyValueUnstable;
#Lock : CandyValueUnstable;
#Next : [UpdateUnstable];
};

Expand Down Expand Up @@ -78,6 +79,7 @@ module {

public type UpdateMode = {
#Set : CandyValue;
#Lock : CandyValue;
#Next : [Update];
};

Expand Down

0 comments on commit 7a02acc

Please sign in to comment.