Skip to content

Commit

Permalink
feat: auto reconstruct for LookupMap
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Dec 3, 2023
1 parent 7c2cfe6 commit 707054e
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 9 deletions.
10 changes: 10 additions & 0 deletions examples/__tests__/test-status-deserialize-class.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ test("Ali set_nested_efficient_recordes then get_nested_efficient_recordes text"
await statusMessage.view("get_nested_efficient_recordes", { id: "2", account_id: bob.accountId }),
"world"
);

t.is(
await statusMessage.view("get_nested_lookup_recordes", { id: "1", account_id: bob.accountId }),
"hello"
);

t.is(
await statusMessage.view("get_nested_lookup_recordes", { id: "2", account_id: bob.accountId }),
"world"
);
});

test("View get_subtype_of_efficient_recordes", async (t) => {
Expand Down
48 changes: 45 additions & 3 deletions examples/src/status-deserialize-class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import {NearBindgen, call, view, near, UnorderedMap, serialize, decode, deserialize, encode} from "near-sdk-js";
import {
NearBindgen,
call,
view,
near,
UnorderedMap,
serialize,
decode,
deserialize,
encode,
LookupMap
} from "near-sdk-js";
import lodash from "lodash-es";

function decode_obj2class(class_instance, obj) {
Expand Down Expand Up @@ -41,7 +52,11 @@ function decode_obj2class(class_instance, obj) {
} else if (ty !== undefined && ty.hasOwnProperty("unorder_set")) {
// todo: imple
} else if (ty !== undefined && ty.hasOwnProperty("lookup_map")) {
// todo: impl
class_instance[key].constructor.schema = ty;
let subtype_value = ty["lookup_map"]["value"];
class_instance[key].subtype = function () {
return subtype_value;
}
} else if (ty !== undefined && ty.hasOwnProperty("lookup_set")) {
// todo: impl
} else {
Expand Down Expand Up @@ -84,7 +99,8 @@ class InnerStatusDeserializeClass {
car: Car,
messages: {array: {value: 'string'}},
efficient_recordes: {unorder_map: {value: 'string'}},
nested_efficient_recordes: {unorder_map: {value: { unorder_map: {value: 'string'}}}}
nested_efficient_recordes: {unorder_map: {value: { unorder_map: {value: 'string'}}}},
nested_lookup_recordes: {unorder_map: {value: { lookup_map: {value: 'string'}}}},
};
constructor() {
this.records = {};
Expand All @@ -94,6 +110,8 @@ class InnerStatusDeserializeClass {
this.efficient_recordes = new UnorderedMap("a");
// id -> account_id -> message
this.nested_efficient_recordes = new UnorderedMap("b");
// id -> account_id -> message
this.nested_lookup_recordes = new UnorderedMap("c");
}
}

Expand Down Expand Up @@ -194,6 +212,12 @@ export class StatusDeserializeClass {
nestedMap.set(near.signerAccountId(), message);
inst.nested_efficient_recordes.set(id, nestedMap);

const nestedLookup = inst.nested_lookup_recordes.get(id, {
defaultValue: new LookupMap("li_" + id + "_"),
});
nestedLookup.set(near.signerAccountId(), message);
inst.nested_lookup_recordes.set(id, nestedLookup);

let data = serialize(inst)
this.messages = decode(data);
}
Expand All @@ -216,6 +240,16 @@ export class StatusDeserializeClass {
}).get(account_id);
}

@view({})
get_nested_lookup_recordes({ account_id, id }) {
near.log(`get_nested_lookup_recordes for account_id ${account_id}, id ${id}`);
let obj = deserialize(encode(this.messages));
let inst = decode_obj2class(new InnerStatusDeserializeClass(), obj);
return inst.nested_lookup_recordes.get(id, {
defaultValue: new LookupMap("li_" + id + "_"),
}).get(account_id);
}

@view({})
get_subtype_of_efficient_recordes({ }) {
near.log(`get_subtype_of_efficient_recordes`);
Expand All @@ -231,4 +265,12 @@ export class StatusDeserializeClass {
let inst = decode_obj2class(new InnerStatusDeserializeClass(), obj);
return inst.nested_efficient_recordes.subtype();
}

@view({})
get_subtype_of_nested_lookup_recordes({ }) {
near.log(`get_subtype_of_nested_lookup_recordes`);
let obj = deserialize(encode(this.messages));
let inst = decode_obj2class(new InnerStatusDeserializeClass(), obj);
return inst.nested_lookup_recordes.subtype();
}
}
1 change: 1 addition & 0 deletions packages/near-sdk-js/lib/collections/lookup-map.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions packages/near-sdk-js/lib/collections/lookup-map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions packages/near-sdk-js/lib/collections/unordered-map.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/near-sdk-js/src/collections/lookup-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export class LookupMap<DataType> {
return near.storageHasKey(storageKey);
}

/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-empty-function */
subtype(): any {

}

/**
* Get the data stored at the provided key.
*
Expand All @@ -37,6 +43,14 @@ export class LookupMap<DataType> {
): DataType | null {
const storageKey = this.keyPrefix + key;
const value = near.storageReadRaw(encode(storageKey));
if (options == undefined || (options.reconstructor == undefined)) {
// eslint-disable-next-line no-prototype-builtins
if (this.subtype() != undefined && this.subtype().hasOwnProperty("lookup_map")) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = LookupMap.reconstruct;
}
}

return getValueWithOptions(value, options);
}
Expand Down
13 changes: 10 additions & 3 deletions packages/near-sdk-js/src/collections/unordered-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ export class UnorderedMap<DataType> {
return this._keys.isEmpty();
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-empty-function */
subtype(): any {

}
Expand All @@ -62,11 +63,17 @@ export class UnorderedMap<DataType> {
if (valueAndIndex === null) {
return options?.defaultValue ?? null;
}
if (options == undefined || (options.reconstructor == undefined)) {
if ((options == undefined || (options.reconstructor == undefined)) && this.subtype() != undefined) {
// eslint-disable-next-line no-prototype-builtins
if (this.subtype() != undefined && this.subtype().hasOwnProperty("unorder_map")) {
if (this.subtype().hasOwnProperty("unorder_map")) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = UnorderedMap.reconstruct;
// eslint-disable-next-line no-prototype-builtins
} else if (this.subtype().hasOwnProperty("lookup_map")) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = LookupMap.reconstruct;
}
}

Expand Down

0 comments on commit 707054e

Please sign in to comment.