Skip to content

Commit

Permalink
fix: fix handle normal case with nested Class and add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
fospring committed Dec 21, 2023
1 parent 8e67878 commit feff288
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
11 changes: 7 additions & 4 deletions examples/__tests__/test-status-deserialize-class.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
44 changes: 36 additions & 8 deletions examples/src/status-deserialize-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}}},
Expand All @@ -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");
Expand Down Expand Up @@ -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({})
Expand Down
4 changes: 1 addition & 3 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.

4 changes: 1 addition & 3 deletions packages/near-sdk-js/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit feff288

Please sign in to comment.