Skip to content

Commit

Permalink
test: add test of nested class of UnorderedMap
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Dec 3, 2023
1 parent 7ca9994 commit 0451a4e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 6 deletions.
5 changes: 5 additions & 0 deletions examples/__tests__/test-status-deserialize-class.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ test("Ali set_car_info and get_car_info", async (t) => {
await statusMessage.view("get_car_info", { }),
carName + " run with speed " + speed
);

t.is(
await statusMessage.view("get_user_car_info", { account_id: ali.accountId }),
carName + " run with speed " + speed
);
});

test("Ali push_message and get_messages", async (t) => {
Expand Down
21 changes: 19 additions & 2 deletions examples/src/status-deserialize-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class InnerStatusDeserializeClass {
vector_nested_group: {vector: {value: { lookup_map: {value: 'string'}}}},
lookup_nest_vec: {lookup_map: {value: { vector: { value: 'string' }}}},
unordered_set: {unordered_set: {value: 'string'}},
user_car_map: {unordered_map: {value: Car }},
};
constructor() {
this.records = {};
Expand All @@ -56,6 +57,7 @@ class InnerStatusDeserializeClass {
// account_id -> index -> message
this.lookup_nest_vec = new LookupMap("e");
this.unordered_set = new UnorderedSet("f");
this.user_car_map = new UnorderedMap("g");
}
}

Expand Down Expand Up @@ -110,8 +112,11 @@ export class StatusDeserializeClass {
near.log(`${account_id} set_car_info name ${name}, speed ${speed}`);
let obj = deserialize(encode(this.messages));
let inst = decodeObj2class(new InnerStatusDeserializeClass(), obj);
inst.car.name = name;
inst.car.speed = speed;
let car = new Car();
car.name = name;
car.speed = speed;
inst.car = car;
inst.user_car_map.set(account_id, car);
let data = serialize(inst)
this.messages = decode(data);
}
Expand All @@ -124,6 +129,18 @@ export class StatusDeserializeClass {
return inst.car.info();
}

@view({})
get_user_car_info({ account_id }) {
near.log(`get_user_car_info for account_id ${account_id}`);
let obj = deserialize(encode(this.messages));
let inst = decodeObj2class(new InnerStatusDeserializeClass(), obj);
let car = inst.user_car_map.get(account_id);
if (car == null) {
return null;
}
return inst.user_car_map.get(account_id).info();
}

@call({})
push_message({ message }) {
let account_id = near.signerAccountId();
Expand Down
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.

8 changes: 7 additions & 1 deletion packages/near-sdk-js/lib/utils.js

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

10 changes: 8 additions & 2 deletions packages/near-sdk-js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function assert(
export type Mutable<T> = { -readonly [P in keyof T]: T[P] };

export function getValueWithOptions<DataType>(
datatype: any,
datatype: unknown,
value: Uint8Array | null,
options: Omit<GetOptions<DataType>, "serializer"> = {
deserializer: deserialize,
Expand All @@ -97,12 +97,15 @@ export function getValueWithOptions<DataType>(
if (datatype !== undefined) {
// subtype info is a class constructor
if (typeof datatype === "function") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
deserialized = decodeObj2class(new datatype(), deserialized);
} else if (typeof datatype === "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")) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
for (const mkey in deserialized) {
if (datatype["map"]["value"] !=='string') {
Expand All @@ -112,6 +115,7 @@ export function getValueWithOptions<DataType>(
// eslint-disable-next-line no-prototype-builtins
} else if (datatype.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') {
Expand Down Expand Up @@ -188,9 +192,11 @@ export function decodeObj2class(class_instance, obj) {
}
let key;
for (key in obj) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
let value = obj[key];
const value = obj[key];
if (typeof value == 'object') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const ty = class_instance.constructor.schema[key];
// eslint-disable-next-line no-prototype-builtins
Expand Down

0 comments on commit 0451a4e

Please sign in to comment.