Skip to content

Commit

Permalink
Merge pull request #15 from NatLabs/dev
Browse files Browse the repository at this point in the history
[Fix]: Return the intended type of a set of arbitrary data
  • Loading branch information
tomijaga authored Sep 12, 2023
2 parents f725a16 + 66e1bcf commit fdb943e
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 41 deletions.
86 changes: 86 additions & 0 deletions benchmarks/serde.bench.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import Prim "mo:prim";
import Cycles "mo:base/ExperimentalCycles";
import IC "mo:base/ExperimentalInternetComputer";
import Debug "mo:base/Debug";
import Iter "mo:base/Iter";
import Serde "../src";

actor {
type Candid = Serde.Candid;
let { Candid } = Serde;

type Permission = {
#read : [Text];
#write : [Text];
#read_all : ();
#write_all : ();
#admin : ();
};

type User = {
name : Text;
age : Nat;
permission : Permission;
};

type Record = {
group : Text;
users : ?[User];
};

public query func cycles() : async Nat { Cycles.balance()};

public query func deserialize(n: Nat) : async (Nat64, Nat, Nat, Nat) {
let init_cycles = Cycles.balance();

let init_heap = Prim.rts_heap_size();
let init_memory = Prim.rts_memory_size();

let calls = IC.countInstructions(
func() {

let admin_record_candid : Candid = #Record([
("group", #Text("admins")),
("users", #Option(#Array([#Record([("age", #Nat(32)), ("name", #Text("John")), ("permission", #Variant("admin", #Null))])]))),
]);

let user_record_candid : Candid = #Record([
("group", #Text("users")),
("users", #Option(#Array([#Record([("age", #Nat(28)), ("name", #Text("Ali")), ("permission", #Variant("read_all", #Null))]), #Record([("age", #Nat(40)), ("name", #Text("James")), ("permission", #Variant("write_all", #Null))])]))),
]);

let empty_record_candid : Candid = #Record([
("group", #Text("empty")),
("users", #Option(#Array([]))),
]);

let null_record_candid : Candid = #Record([
("group", #Text("null")),
("users", #Option(#Null)),
]);

let base_record_candid : Candid = #Record([
("group", #Text("base")),
("users", #Option(#Array([#Record([("age", #Nat(32)), ("name", #Text("Henry")), ("permission", #Variant("read", #Array([#Text("posts"), #Text("comments")])))]), #Record([("age", #Nat(32)), ("name", #Text("Steven")), ("permission", #Variant("write", #Array([#Text("posts"), #Text("comments")])))])]))),
]);

let records : Candid = #Array([
null_record_candid,
empty_record_candid,
admin_record_candid,
user_record_candid,
base_record_candid,
]);

for (_ in Iter.range(1, n)){
let #ok(blob) = Candid.encodeOne(records, null);
let motoko : ?[Record] = from_candid (blob);
Debug.print(debug_show (motoko));
};
}
);

(calls, Prim.rts_heap_size() - init_heap, Prim.rts_memory_size() - init_memory, init_cycles - Cycles.balance())
};

};
6 changes: 6 additions & 0 deletions dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"args": ""
}
},
"canisters": {
"benchmarks": {
"type": "motoko",
"main": "benchmarks/serde.bench.mo"
}
},
"networks": {
"local": {
"bind": "127.0.0.1:8000",
Expand Down
2 changes: 1 addition & 1 deletion mops.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde"
version = "1.1.0"
version = "2.0.0"
description = "A serialisation and deserialisation library for Motoko."
repository = "https://github.com/NatLabs/serde"
keywords = [ "json", "candid", "parser", "urlencoded", "serialization" ]
Expand Down
5 changes: 1 addition & 4 deletions src/Candid/Blob/Decoder.mo
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,7 @@ module {

case (x) {
return #err(
"
Serde Decoding Error from fromArg() fn in Candid/Blob/Decoder.mo
Error Log: Could not match '" # debug_show (x) # "' type to any case
"
"\nSerde Decoding Error from fromArg() fn in Candid/Blob/Decoder.mo\n\tError Log: Could not match '" # debug_show (x) # "' type to any case"
);
};
};
Expand Down
Loading

0 comments on commit fdb943e

Please sign in to comment.