From feff2889b9ca992c22c5cada0e9517e6b874ba34 Mon Sep 17 00:00:00 2001 From: Spring Chiu Date: Fri, 22 Dec 2023 00:44:57 +0800 Subject: [PATCH] fix: fix handle normal case with nested Class and add testcase --- .../test-status-deserialize-class.ava.js | 11 +++-- examples/src/status-deserialize-class.js | 44 +++++++++++++++---- packages/near-sdk-js/lib/utils.js | 4 +- packages/near-sdk-js/src/utils.ts | 4 +- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/examples/__tests__/test-status-deserialize-class.ava.js b/examples/__tests__/test-status-deserialize-class.ava.js index cfac7167a..a7b291ed3 100644 --- a/examples/__tests__/test-status-deserialize-class.ava.js +++ b/examples/__tests__/test-status-deserialize-class.ava.js @@ -41,15 +41,18 @@ test("Ali sets then gets status", async (t) => { ); }); -test("Ali set_car_info and get_car_info", async (t) => { +test.only("Ali set_truck_info and get_truck_info", async (t) => { const { ali, statusMessage } = t.context.accounts; let carName = "Mercedes-Benz"; let speed = 240; - await ali.call(statusMessage, "set_car_info", { name: carName, speed: speed }); + await ali.call(statusMessage, "set_truck_info", { name: carName, speed: speed }); + + await ali.call(statusMessage, "add_truck_load", { name: "alice", load: "a box" }); + await ali.call(statusMessage, "add_truck_load", { name: "bob", load: "a packet" }); t.is( - await statusMessage.view("get_car_info", { }), - carName + " run with speed " + speed + await statusMessage.view("get_truck_info", { }), + carName + " run with speed " + speed + " with loads length: 2" ); t.is( diff --git a/examples/src/status-deserialize-class.js b/examples/src/status-deserialize-class.js index 93ca1dff1..d6e248aa2 100644 --- a/examples/src/status-deserialize-class.js +++ b/examples/src/status-deserialize-class.js @@ -24,12 +24,29 @@ class Car { } } +class Truck { + static schema = { + name: "string", + speed: "number", + loads: {unordered_map: {value: 'string'}} + }; + constructor() { + this.name = ""; + this.speed = 0; + this.loads = new UnorderedMap("tra"); + } + + info() { + return this.name + " run with speed " + this.speed.toString() + " with loads length: " + this.loads.toArray().length; + } +} + @NearBindgen({}) export class StatusDeserializeClass { static schema = { is_inited: "boolean", records: {map: { key: 'string', value: 'string' }}, - car: Car, + truck: Truck, messages: {array: {value: 'string'}}, efficient_recordes: {unordered_map: {value: 'string'}}, nested_efficient_recordes: {unordered_map: {value: { unordered_map: {value: 'string'}}}}, @@ -44,7 +61,7 @@ export class StatusDeserializeClass { constructor() { this.is_inited = false; this.records = {}; - this.car = new Car(); + this.truck = new Truck(); this.messages = []; // account_id -> message this.efficient_recordes = new UnorderedMap("a"); @@ -92,20 +109,31 @@ export class StatusDeserializeClass { @call({}) - set_car_info({ name, speed }) { + set_truck_info({ name, speed }) { let account_id = near.signerAccountId(); - near.log(`${account_id} set_car_info name ${name}, speed ${speed}`); + near.log(`${account_id} set_truck_info name ${name}, speed ${speed}`); + let truck = new Truck(); + truck.name = name; + truck.speed = speed; + truck.loads = this.truck.loads; + this.truck = truck; let car = new Car(); car.name = name; car.speed = speed; - this.car = car; this.user_car_map.set(account_id, car); } + @call({}) + add_truck_load({ name, load }) { + let account_id = near.signerAccountId(); + near.log(`${account_id} add_truck_load name ${name}, load ${load}`); + this.truck.loads.set(name, load); + } + @view({}) - get_car_info({ }) { - near.log(`get_car_info`); - return this.car.info(); + get_truck_info({ }) { + near.log(`get_truck_info`); + return this.truck.info(); } @view({}) diff --git a/packages/near-sdk-js/lib/utils.js b/packages/near-sdk-js/lib/utils.js index 29d1141a9..64a988a43 100644 --- a/packages/near-sdk-js/lib/utils.js +++ b/packages/near-sdk-js/lib/utils.js @@ -208,9 +208,7 @@ export function decodeObj2class(class_instance, obj) { }; } else { - // normal class - class_instance[key].constructor.schema = - class_instance.constructor.schema[key]; + // normal case with nested Class, such as field is truck: Truck, class_instance[key] = decodeObj2class(class_instance[key], obj[key]); } } diff --git a/packages/near-sdk-js/src/utils.ts b/packages/near-sdk-js/src/utils.ts index 732145244..67ba35afa 100644 --- a/packages/near-sdk-js/src/utils.ts +++ b/packages/near-sdk-js/src/utils.ts @@ -275,9 +275,7 @@ export function decodeObj2class(class_instance, obj) { return subtype_value; }; } else { - // normal class - class_instance[key].constructor.schema = - class_instance.constructor.schema[key]; + // normal case with nested Class, such as field is truck: Truck, class_instance[key] = decodeObj2class(class_instance[key], obj[key]); } } else {