Skip to content

Commit feff288

Browse files
committed
fix: fix handle normal case with nested Class and add testcase
1 parent 8e67878 commit feff288

File tree

4 files changed

+45
-18
lines changed

4 files changed

+45
-18
lines changed

examples/__tests__/test-status-deserialize-class.ava.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,18 @@ test("Ali sets then gets status", async (t) => {
4141
);
4242
});
4343

44-
test("Ali set_car_info and get_car_info", async (t) => {
44+
test.only("Ali set_truck_info and get_truck_info", async (t) => {
4545
const { ali, statusMessage } = t.context.accounts;
4646
let carName = "Mercedes-Benz";
4747
let speed = 240;
48-
await ali.call(statusMessage, "set_car_info", { name: carName, speed: speed });
48+
await ali.call(statusMessage, "set_truck_info", { name: carName, speed: speed });
49+
50+
await ali.call(statusMessage, "add_truck_load", { name: "alice", load: "a box" });
51+
await ali.call(statusMessage, "add_truck_load", { name: "bob", load: "a packet" });
4952

5053
t.is(
51-
await statusMessage.view("get_car_info", { }),
52-
carName + " run with speed " + speed
54+
await statusMessage.view("get_truck_info", { }),
55+
carName + " run with speed " + speed + " with loads length: 2"
5356
);
5457

5558
t.is(

examples/src/status-deserialize-class.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,29 @@ class Car {
2424
}
2525
}
2626

27+
class Truck {
28+
static schema = {
29+
name: "string",
30+
speed: "number",
31+
loads: {unordered_map: {value: 'string'}}
32+
};
33+
constructor() {
34+
this.name = "";
35+
this.speed = 0;
36+
this.loads = new UnorderedMap("tra");
37+
}
38+
39+
info() {
40+
return this.name + " run with speed " + this.speed.toString() + " with loads length: " + this.loads.toArray().length;
41+
}
42+
}
43+
2744
@NearBindgen({})
2845
export class StatusDeserializeClass {
2946
static schema = {
3047
is_inited: "boolean",
3148
records: {map: { key: 'string', value: 'string' }},
32-
car: Car,
49+
truck: Truck,
3350
messages: {array: {value: 'string'}},
3451
efficient_recordes: {unordered_map: {value: 'string'}},
3552
nested_efficient_recordes: {unordered_map: {value: { unordered_map: {value: 'string'}}}},
@@ -44,7 +61,7 @@ export class StatusDeserializeClass {
4461
constructor() {
4562
this.is_inited = false;
4663
this.records = {};
47-
this.car = new Car();
64+
this.truck = new Truck();
4865
this.messages = [];
4966
// account_id -> message
5067
this.efficient_recordes = new UnorderedMap("a");
@@ -92,20 +109,31 @@ export class StatusDeserializeClass {
92109

93110

94111
@call({})
95-
set_car_info({ name, speed }) {
112+
set_truck_info({ name, speed }) {
96113
let account_id = near.signerAccountId();
97-
near.log(`${account_id} set_car_info name ${name}, speed ${speed}`);
114+
near.log(`${account_id} set_truck_info name ${name}, speed ${speed}`);
115+
let truck = new Truck();
116+
truck.name = name;
117+
truck.speed = speed;
118+
truck.loads = this.truck.loads;
119+
this.truck = truck;
98120
let car = new Car();
99121
car.name = name;
100122
car.speed = speed;
101-
this.car = car;
102123
this.user_car_map.set(account_id, car);
103124
}
104125

126+
@call({})
127+
add_truck_load({ name, load }) {
128+
let account_id = near.signerAccountId();
129+
near.log(`${account_id} add_truck_load name ${name}, load ${load}`);
130+
this.truck.loads.set(name, load);
131+
}
132+
105133
@view({})
106-
get_car_info({ }) {
107-
near.log(`get_car_info`);
108-
return this.car.info();
134+
get_truck_info({ }) {
135+
near.log(`get_truck_info`);
136+
return this.truck.info();
109137
}
110138

111139
@view({})

packages/near-sdk-js/lib/utils.js

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/near-sdk-js/src/utils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,7 @@ export function decodeObj2class(class_instance, obj) {
275275
return subtype_value;
276276
};
277277
} else {
278-
// normal class
279-
class_instance[key].constructor.schema =
280-
class_instance.constructor.schema[key];
278+
// normal case with nested Class, such as field is truck: Truck,
281279
class_instance[key] = decodeObj2class(class_instance[key], obj[key]);
282280
}
283281
} else {

0 commit comments

Comments
 (0)