Skip to content

Commit

Permalink
feat: update check subtype logic
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Dec 26, 2023
1 parent f4b8967 commit 884cc82
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 98 deletions.
2 changes: 1 addition & 1 deletion examples/__tests__/test-status-deserialize-class.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test("Ali push_message and get_messages", async (t) => {
);
});

test("Ali set_nested_efficient_recordes then get_nested_efficient_recordes text", async (t) => {
test.only("Ali set_nested_efficient_recordes then get_nested_efficient_recordes text", async (t) => {
const { ali, bob, statusMessage } = t.context.accounts;
await ali.call(statusMessage, "set_nested_efficient_recordes", { id: "1", message: "hello" }, { gas: 35_000_000_000_000n });
await bob.call(statusMessage, "set_nested_efficient_recordes", { id: "1", message: "hello" }, { gas: 35_000_000_000_000n });
Expand Down
42 changes: 6 additions & 36 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.

2 changes: 1 addition & 1 deletion packages/near-sdk-js/lib/utils.d.ts

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

44 changes: 30 additions & 14 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.

42 changes: 10 additions & 32 deletions packages/near-sdk-js/src/collections/subtype.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { GetOptions } from "../types/collections";
import { LookupMap } from "./lookup-map";
import { LookupSet } from "./lookup-set";
import { UnorderedSet } from "./unordered-set";
import { Vector } from "./vector";
import { UnorderedMap } from "./unordered-map";

export const LOOKUP_MAP_SCHE = "lookup_map";
export const LOOKUP_SET_SCHE = "lookup_set";
Expand All @@ -22,33 +17,16 @@ export abstract class SubType<DataType> {
if (options == undefined) {
options = {};
}
if (options.reconstructor == undefined && this.subtype() != undefined) {
// eslint-disable-next-line no-prototype-builtins
if (this.subtype().hasOwnProperty(UNORDERED_MAP_SCHE)) {
// 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_SCHE)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = LookupMap.reconstruct;
// eslint-disable-next-line no-prototype-builtins
} else if (this.subtype().hasOwnProperty(LOOKUP_SET_SCHE)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = LookupSet.reconstruct;
// eslint-disable-next-line no-prototype-builtins
} else if (this.subtype().hasOwnProperty(UNORDERED_SET_SCHE)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = UnorderedSet.reconstruct;
// eslint-disable-next-line no-prototype-builtins
} else if (this.subtype().hasOwnProperty(VECTOR_SCHE)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = Vector.reconstruct;
}
// eslint-disable-next-line no-prototype-builtins
if (
options.reconstructor == undefined &&
this.subtype() != undefined &&
this.subtype().hasOwnProperty("collection")
) {
// { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = this.subtype()["reconstructor"];
}
return options;
}
Expand Down
48 changes: 34 additions & 14 deletions packages/near-sdk-js/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GetOptions } from "./types/collections";
import { cloneDeep } from "lodash-es";
import * as near from "./api";

export interface Env {
uint8array_to_latin1_string(a: Uint8Array): string;
Expand Down Expand Up @@ -71,7 +72,7 @@ export function assert(
export type Mutable<T> = { -readonly [P in keyof T]: T[P] };

export function getValueWithOptions<DataType>(
datatype: unknown,
subDatatype: unknown,
value: Uint8Array | null,
options: Omit<GetOptions<DataType>, "serializer"> = {
deserializer: deserialize,
Expand All @@ -89,39 +90,56 @@ export function getValueWithOptions<DataType>(
}

if (options?.reconstructor) {
return options.reconstructor(deserialized);
near.log(deserialized);
// example: // { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
const collection = options.reconstructor(deserialized);
// eslint-disable-next-line no-prototype-builtins
if (
subDatatype !== undefined &&
subDatatype.hasOwnProperty("collection") &&
subDatatype["collection"].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"];
};
}
return collection;
}

if (datatype !== undefined) {
// subtype info is a class constructor
if (typeof datatype === "function") {
// example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
if (subDatatype !== undefined) {
// subtype info is a class constructor, Such as Car
if (typeof subDatatype === "function") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
deserialized = decodeObj2class(new datatype(), deserialized);
} else if (typeof datatype === "object") {
deserialized = decodeObj2class(new subDatatype(), deserialized);
} else if (typeof subDatatype === "object") {
// normal collections of array, map; subtype will be:
// {map: { key: 'string', value: 'string' }} or {array: {value: 'string'}} ..
// eslint-disable-next-line no-prototype-builtins
if (datatype.hasOwnProperty("map")) {
if (subDatatype.hasOwnProperty("map")) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
for (const mkey in deserialized) {
if (datatype["map"]["value"] !== "string") {
if (subDatatype["map"]["value"] !== "string") {
deserialized[mkey] = decodeObj2class(
new datatype["map"]["value"](),
new subDatatype["map"]["value"](),
value[mkey]
);
}
}
// eslint-disable-next-line no-prototype-builtins
} else if (datatype.hasOwnProperty("array")) {
} else if (subDatatype.hasOwnProperty("array")) {
const new_vec = [];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
for (const k in deserialized) {
if (datatype["array"]["value"] !== "string") {
if (subDatatype["array"]["value"] !== "string") {
new_vec.push(
decodeObj2class(new datatype["array"]["value"](), value[k])
decodeObj2class(new subDatatype["array"]["value"](), value[k])
);
}
}
Expand Down Expand Up @@ -230,10 +248,12 @@ export function decodeObj2class(class_instance, obj) {
}
// eslint-disable-next-line no-prototype-builtins
} else if (ty !== undefined && ty.hasOwnProperty("collection")) {
// nested_lookup_recordes: {collection: {reconstructor: UnorderedMap.reconstruct, value: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}}},
// {collection: {reconstructor:
class_instance[key] = ty["collection"]["reconstructor"](obj[key]);
class_instance[key].constructor.schema = ty;
const subtype_value = ty["collection"]["value"];
class_instance[key].subtype = function () {
// example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
return subtype_value;
};
// eslint-disable-next-line no-prototype-builtins
Expand Down

0 comments on commit 884cc82

Please sign in to comment.