Skip to content

Commit

Permalink
fix: fix convert subtype collection class failed
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Jan 26, 2024
1 parent aad39e9 commit c0ab23d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 62 deletions.
18 changes: 0 additions & 18 deletions examples/__tests__/test-status-deserialize-class.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,21 +149,3 @@ test("Ali set_extra_record without schema defined then gets", async (t) => {
const recordWithoutSchemaDefined = await statusMessage.view("get_extra_record", { account_id: ali.accountId });
t.is(recordWithoutSchemaDefined, "Hello world!");
});

test("View get_subtype_of_efficient_recordes", async (t) => {
const { statusMessage } = t.context.accounts;

t.is(
await statusMessage.view("get_subtype_of_efficient_recordes", { }),
'string'
);
});

test("View get_subtype_of_nested_efficient_recordes", async (t) => {
const { statusMessage } = t.context.accounts;

t.is(
JSON.stringify(await statusMessage.view("get_subtype_of_nested_efficient_recordes", { })),
'{"collection":{"value":"string"}}'
);
});
12 changes: 5 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.

30 changes: 17 additions & 13 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.

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

export function decodeObj2class(class_instance, obj) {
if (
typeof obj != "object" || typeof obj === "bigint" || obj instanceof Date ||
typeof obj != "object" ||
typeof obj === "bigint" ||
obj instanceof Date ||
class_instance.constructor.schema === undefined
) {
return obj;
Expand Down Expand Up @@ -247,15 +249,17 @@ export function decodeObj2class(class_instance, obj) {
}
// eslint-disable-next-line no-prototype-builtins
} else if (ty !== undefined && ty.hasOwnProperty("class")) {
// nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
// {collection: {reconstructor:
// => nested_lookup_recordes: {class: UnorderedMap, value: {class: LookupMap }},
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;
};
// eslint-disable-next-line no-prototype-builtins
if (ty.hasOwnProperty("value")) {
const subtype_value = ty["value"];
class_instance[key].subtype = function () {
// example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
// example: UnorderedMap or {class: UnorderedMap, value: 'string'}
return subtype_value;
};
}
} else if (ty !== undefined && typeof ty.reconstruct === "function") {
class_instance[key] = ty.reconstruct(obj[key]);
} else {
Expand Down

0 comments on commit c0ab23d

Please sign in to comment.