@dao-xyz/borsh
Advanced tools
Comparing version 2.1.5 to 2.1.6
@@ -800,2 +800,33 @@ "use strict"; | ||
}); | ||
test("nested override", () => { | ||
/** | ||
* Serialize field with custom serializer and deserializer | ||
*/ | ||
class TestStruct { | ||
constructor(number) { | ||
this.number = number; | ||
} | ||
} | ||
__decorate([ | ||
(0, index_1.field)({ | ||
type: (0, index_1.option)({ | ||
serialize: (value, writer) => { | ||
writer.writeU8(value); | ||
}, | ||
deserialize: (reader) => { | ||
return reader.readU8(); | ||
}, | ||
}), | ||
}) | ||
], TestStruct.prototype, "number", void 0); | ||
(0, index_1.validate)(TestStruct); | ||
// with value | ||
const serialized = (0, index_1.serialize)(new TestStruct(123)); | ||
expect(serialized).toStrictEqual(Buffer.from([1, 123])); | ||
const deserialied = (0, index_1.deserialize)(Buffer.from(serialized), TestStruct, false, binary_1.BinaryReader); | ||
expect(deserialied.number).toEqual(123); | ||
// without value | ||
const serializedNone = (0, index_1.serialize)(new TestStruct(undefined)); | ||
expect(serializedNone).toStrictEqual(Buffer.from([0])); | ||
}); | ||
}); | ||
@@ -802,0 +833,0 @@ describe("order", () => { |
@@ -11,3 +11,3 @@ import { BinaryReader, BinaryWriter } from "./binary"; | ||
} | ||
export declare type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "String" | Constructor<any> | WrappedType; | ||
export declare type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "String" | Constructor<any> | CustomField<any> | WrappedType; | ||
export declare type SimpleField = { | ||
@@ -38,3 +38,3 @@ type: FieldType; | ||
key: string; | ||
type: FieldType | CustomField<any>; | ||
type: FieldType; | ||
} | ||
@@ -41,0 +41,0 @@ export declare class StructKind { |
@@ -11,3 +11,3 @@ import { BinaryReader, BinaryWriter } from "./binary"; | ||
} | ||
export declare type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "String" | Constructor<any> | WrappedType; | ||
export declare type FieldType = "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "u512" | "f32" | "f64" | "String" | Constructor<any> | CustomField<any> | WrappedType; | ||
export declare type SimpleField = { | ||
@@ -38,3 +38,3 @@ type: FieldType; | ||
key: string; | ||
type: FieldType | CustomField<any>; | ||
type: FieldType; | ||
} | ||
@@ -41,0 +41,0 @@ export declare class StructKind { |
{ | ||
"name": "@dao-xyz/borsh", | ||
"version": "2.1.5", | ||
"version": "2.1.6", | ||
"readme": "README.md", | ||
@@ -60,2 +60,2 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README", | ||
} | ||
} | ||
} |
import BN from "bn.js"; | ||
import { write } from "fs"; | ||
import { BinaryReader } from "../binary"; | ||
@@ -874,2 +873,42 @@ import { BorshError } from "../error"; | ||
}); | ||
test("nested override", () => { | ||
/** | ||
* Serialize field with custom serializer and deserializer | ||
*/ | ||
class TestStruct { | ||
@field({ | ||
type: option({ | ||
serialize: (value: number, writer) => { | ||
writer.writeU8(value); | ||
}, | ||
deserialize: (reader): number => { | ||
return reader.readU8(); | ||
}, | ||
}), | ||
}) | ||
public number?: number; | ||
constructor(number?: number) { | ||
this.number = number; | ||
} | ||
} | ||
validate(TestStruct); | ||
// with value | ||
const serialized = serialize(new TestStruct(123)); | ||
expect(serialized).toStrictEqual(Buffer.from([1, 123])); | ||
const deserialied = deserialize( | ||
Buffer.from(serialized), | ||
TestStruct, | ||
false, | ||
BinaryReader | ||
); | ||
expect(deserialied.number).toEqual(123); | ||
// without value | ||
const serializedNone = serialize(new TestStruct(undefined)); | ||
expect(serializedNone).toStrictEqual(Buffer.from([0])); | ||
}); | ||
}); | ||
@@ -876,0 +915,0 @@ |
@@ -45,2 +45,3 @@ import { BinaryReader, BinaryWriter } from "./binary"; | ||
| Constructor<any> | ||
| CustomField<any> | ||
| WrappedType; | ||
@@ -89,3 +90,3 @@ export type SimpleField = { type: FieldType; index?: number }; | ||
key: string; | ||
type: FieldType | CustomField<any>; | ||
type: FieldType; | ||
} | ||
@@ -92,0 +93,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
351728
6367