Skip to content

Commit

Permalink
Add option support to deserialize (#16)
Browse files Browse the repository at this point in the history
* feat: add option support to deserialize
  • Loading branch information
bartosz-lipinski authored May 5, 2021
1 parent 4f000b1 commit 8f732a8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Install dependencies:
yarn install
```

Continuesly build with:
Continuously build with:
```bash
yarn dev
```
Expand Down
16 changes: 15 additions & 1 deletion borsh-ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function serializeField(schema: Schema, fieldName: string, value: any, fieldType
} else if (fieldType.kind !== undefined) {
switch (fieldType.kind) {
case 'option': {
if (value === null) {
if (value === null || value === undefined) {
writer.writeU8(0);
} else {
writer.writeU8(1);
Expand Down Expand Up @@ -319,6 +319,20 @@ function deserializeField(schema: Schema, fieldName: string, fieldType: any, rea
return reader.readArray(() => deserializeField(schema, fieldName, fieldType[0], reader));
}

if (fieldType.kind === 'option') {
const option = reader.readU8();
if (option) {
return deserializeField(
schema,
fieldName,
fieldType.type,
reader
);
}

return undefined;
}

return deserializeStruct(schema, fieldType, reader);
} catch (error) {
if (error instanceof BorshError) {
Expand Down
12 changes: 12 additions & 0 deletions borsh-ts/test/serialize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ test('serialize object', async () => {
expect(newValue.q).toEqual(new Uint8Array([1, 2, 3]));
});

test('serialize optional field', async () => {
const schema = new Map([[Test, { kind: 'struct', fields: [['x', { kind: 'option', type: 'string' }]]}]]);

let buf = borsh.serialize(schema, new Test({ x: '123', }));
let newValue = borsh.deserialize(schema, Test, buf);
expect(newValue.x).toEqual('123');

buf = borsh.serialize(schema, new Test({ }));
newValue = borsh.deserialize(schema, Test, buf);
expect(newValue.x).toEqual(undefined);
});

test('serialize max uint', async () => {
const u64MaxHex = 'ffffffffffffffff';
const value = new Test({
Expand Down
9 changes: 8 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function serializeField(schema, fieldName, value, fieldType, writer) {
else if (fieldType.kind !== undefined) {
switch (fieldType.kind) {
case 'option': {
if (value === null) {
if (value === null || value === undefined) {
writer.writeU8(0);
}
else {
Expand Down Expand Up @@ -334,6 +334,13 @@ function deserializeField(schema, fieldName, fieldType, reader) {
}
return reader.readArray(() => deserializeField(schema, fieldName, fieldType[0], reader));
}
if (fieldType.kind === 'option') {
const option = reader.readU8();
if (option) {
return deserializeField(schema, fieldName, fieldType.type, reader);
}
return undefined;
}
return deserializeStruct(schema, fieldType, reader);
}
catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"serializer",
"deserializer",
"consistency",
"determenistic"
"deterministic"
],
"author": "Near Inc",
"license": "Apache-2.0",
Expand Down

0 comments on commit 8f732a8

Please sign in to comment.