Skip to content

Commit

Permalink
feat: update reconstruct api
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Jan 15, 2024
1 parent 0827713 commit aad39e9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 31 deletions.
27 changes: 19 additions & 8 deletions examples/src/status-deserialize-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class Truck {
static schema = {
name: "string",
speed: "number",
loads: {collection: {reconstructor: UnorderedMap.reconstruct, value: 'string'}}
// loads: {collection: {reconstructor: UnorderedMap.reconstruct, value: 'string'}}
loads: {class: UnorderedMap }
};
constructor() {
this.name = "";
Expand All @@ -41,20 +42,30 @@ class Truck {
}
}

// sdk should first try if UnorderedMap has a static schema and use it to recursively decode.
// In this case, UnorderedMap doesn't.
// So sdk should next try call UnorderedMap.reconstruct.
@NearBindgen({})
export class StatusDeserializeClass {
static schema = {
is_inited: "boolean",
records: {map: { key: 'string', value: 'string' }},
truck: Truck,
messages: {array: {value: 'string'}},
efficient_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: 'string'}},
nested_efficient_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: UnorderedMap.reconstruct, value: 'string'}}}},
nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
vector_nested_group: {collection: {reconstructor: Vector.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
lookup_nest_vec: {collection: {reconstructor: LookupMap.reconstruct, value: { collection: { reconstructor: Vector.reconstruct, value: 'string' }}}},
unordered_set: {collection: {reconstructor: UnorderedSet.reconstruct, value: 'string'}},
user_car_map: {collection: {reconstructor: UnorderedMap.reconstruct, value: Car }},
// efficient_recordes: {class: {reconstructor: UnorderedMap.reconstruct, value: 'string'}},
efficient_recordes: {class: UnorderedMap},
// nested_efficient_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: UnorderedMap.reconstruct, value: 'string'}}}},
nested_efficient_recordes: {class: UnorderedMap, value: UnorderedMap},
// nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
nested_lookup_recordes: {class: UnorderedMap, value: {class: LookupMap }},
// vector_nested_group: {collection: {reconstructor: Vector.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
vector_nested_group: {class: Vector, value: { class: LookupMap }},
// lookup_nest_vec: {collection: {reconstructor: LookupMap.reconstruct, value: { collection: { reconstructor: Vector.reconstruct, value: 'string' }}}},
lookup_nest_vec: { class: LookupMap, value: Vector },
// unordered_set: {collection: {reconstructor: UnorderedSet.reconstruct, value: 'string'}},
unordered_set: {class: UnorderedSet },
// user_car_map: {collection: {reconstructor: UnorderedMap.reconstruct, value: Car }},
user_car_map: {class: UnorderedMap, value: Car },
big_num: 'bigint',
date: 'date'
};
Expand Down
19 changes: 12 additions & 7 deletions packages/near-sdk-js/lib/collections/subtype.js

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

12 changes: 8 additions & 4 deletions packages/near-sdk-js/lib/utils.js

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

20 changes: 12 additions & 8 deletions packages/near-sdk-js/src/collections/subtype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ export abstract class SubType<DataType> {
const subtype = this.subtype();
if (
options.reconstructor == undefined &&
subtype != undefined &&
// eslint-disable-next-line no-prototype-builtins
subtype.hasOwnProperty("collection") &&
typeof this.subtype().collection.reconstructor === "function"
subtype != undefined
) {
// { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = this.subtype().collection.reconstructor;
if (
// eslint-disable-next-line no-prototype-builtins
subtype.hasOwnProperty("class") &&
typeof subtype.class.reconstructor === "function") {
// { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = subtype.class.reconstructor;
} else if (subtype.reconstructor === "function") {
options.reconstructor = subtype.reconstructor;
}
}
return options;
}
Expand Down
11 changes: 7 additions & 4 deletions packages/near-sdk-js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ export function deserialize(valueToDeserialize: Uint8Array): unknown {

export function decodeObj2class(class_instance, obj) {
if (
typeof obj != "object" ||
typeof obj != "object" || typeof obj === "bigint" || obj instanceof Date ||
class_instance.constructor.schema === undefined
) {
return obj;
Expand Down Expand Up @@ -246,15 +246,18 @@ export function decodeObj2class(class_instance, obj) {
}
}
// eslint-disable-next-line no-prototype-builtins
} else if (ty !== undefined && ty.hasOwnProperty("collection")) {
} else if (ty !== undefined && ty.hasOwnProperty("class")) {
// nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
// {collection: {reconstructor:
class_instance[key] = ty["collection"]["reconstructor"](obj[key]);
const subtype_value = ty["collection"]["value"];
class_instance[key] = ty["class"].reconstruct(obj[key]);
const subtype_value = ty["value"];
class_instance[key].subtype = function () {
// example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
// example: UnorderedMap
return subtype_value;
};
} else if (ty !== undefined && typeof ty.reconstruct === "function") {
class_instance[key] = ty.reconstruct(obj[key]);
} else {
// normal case with nested Class, such as field is truck: Truck,
class_instance[key] = decodeObj2class(class_instance[key], obj[key]);
Expand Down

0 comments on commit aad39e9

Please sign in to comment.