Skip to content

Commit

Permalink
fix: fix reconstructor undefine error
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Dec 27, 2023
1 parent 884cc82 commit e43fe94
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 106 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.only("Ali set_nested_efficient_recordes then get_nested_efficient_recordes text", async (t) => {
test("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
2 changes: 1 addition & 1 deletion examples/src/status-deserialize-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class StatusDeserializeClass {
const lookupNestVec = this.lookup_nest_vec.get(account_id, {
defaultValue: new Vector("ei_" + account_id + "_"),
});
lookupNestVec.push(message);
lookupNestVec.push(message); //
this.lookup_nest_vec.set(account_id, lookupNestVec);

this.unordered_set.set(account_id);
Expand Down
4 changes: 2 additions & 2 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.

45 changes: 4 additions & 41 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.

9 changes: 6 additions & 3 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.

10 changes: 5 additions & 5 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.

51 changes: 6 additions & 45 deletions packages/near-sdk-js/src/collections/lookup-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,18 @@ import {
serializeValueWithOptions,
encode,
} from "../utils";
import { UnorderedMap } from "./unordered-map";
import { LookupSet } from "./lookup-set";
import { UnorderedSet } from "./unordered-set";
import { Vector } from "./vector";
import {
LOOKUP_MAP_SCHE,
LOOKUP_SET_SCHE,
UNORDERED_MAP_SCHE,
UNORDERED_SET_SCHE,
VECTOR_SCHE,
} from "./subtype";
import { SubType } from "./subtype";

/**
* A lookup map that stores data in NEAR storage.
*/
export class LookupMap<DataType> {
export class LookupMap<DataType> extends SubType<DataType> {
/**
* @param keyPrefix - The byte prefix to use when storing elements inside this collection.
*/
constructor(readonly keyPrefix: string) {}
constructor(readonly keyPrefix: string) {
super();
}

/**
* Checks whether the collection contains the value.
Expand All @@ -36,10 +28,6 @@ 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 @@ -55,34 +43,7 @@ export class LookupMap<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;
}
}
options = this.set_reconstructor(options);

return getValueWithOptions(this.subtype(), value, options);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/near-sdk-js/src/collections/subtype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ export abstract class SubType<DataType> {
if (options == undefined) {
options = {};
}
// eslint-disable-next-line no-prototype-builtins
const subtype = this.subtype();
if (
options.reconstructor == undefined &&
this.subtype() != undefined &&
this.subtype().hasOwnProperty("collection")
subtype != undefined &&
// eslint-disable-next-line no-prototype-builtins
subtype.hasOwnProperty("collection") &&
typeof this.subtype().collection.reconstructor === "function"
) {
// { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.reconstructor = this.subtype()["reconstructor"];
options.reconstructor = this.subtype().collection.reconstructor;
}
return options;
}
Expand Down
6 changes: 2 additions & 4 deletions packages/near-sdk-js/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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 @@ -90,13 +89,13 @@ export function getValueWithOptions<DataType>(
}

if (options?.reconstructor) {
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 &&
// eslint-disable-next-line no-prototype-builtins
subDatatype.hasOwnProperty("collection") &&
// eslint-disable-next-line no-prototype-builtins
subDatatype["collection"].hasOwnProperty("value")
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -256,7 +255,6 @@ export function decodeObj2class(class_instance, obj) {
// example: { collection: {reconstructor: LookupMap.reconstruct, value: 'string'}}
return subtype_value;
};
// eslint-disable-next-line no-prototype-builtins
} 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 e43fe94

Please sign in to comment.