@dao-xyz/borsh
Advanced tools
Comparing version 2.1.3 to 2.1.4
@@ -432,2 +432,65 @@ "use strict"; | ||
}); | ||
test("extended enum inheritance variants, serialization target does matter for fields", () => { | ||
let Super = class Super { | ||
}; | ||
Super = __decorate([ | ||
(0, index_1.variant)(0) | ||
], Super); | ||
let ClazzA = class ClazzA extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
ClazzA = __decorate([ | ||
(0, index_1.variant)(0) | ||
], ClazzA); | ||
let ClazzB = class ClazzB extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
ClazzB = __decorate([ | ||
(0, index_1.variant)(1) | ||
], ClazzB); | ||
class Struct { | ||
constructor() { } | ||
} | ||
__decorate([ | ||
(0, index_1.field)({ type: ClazzA }) | ||
], Struct.prototype, "property", void 0); | ||
const s = new Struct(); | ||
s.property = new ClazzB(); | ||
expect(() => (0, index_1.serialize)(s)).toThrowError(); | ||
}); | ||
test("extended enum inheritance variants, deserialization target does matter for fields", () => { | ||
let Super = class Super { | ||
}; | ||
Super = __decorate([ | ||
(0, index_1.variant)(0) | ||
], Super); | ||
let ClazzA = class ClazzA extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
ClazzA = __decorate([ | ||
(0, index_1.variant)(0) | ||
], ClazzA); | ||
let ClazzB = class ClazzB extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
ClazzB = __decorate([ | ||
(0, index_1.variant)(1) | ||
], ClazzB); | ||
class Struct { | ||
constructor() { } | ||
} | ||
__decorate([ | ||
(0, index_1.field)({ type: ClazzB }) | ||
], Struct.prototype, "property", void 0); | ||
// we try to deserializ [0,0] into Struct, which shouldnot be possible since property is instance of ClazzB | ||
expect(() => (0, index_1.deserialize)(Buffer.from(Uint8Array.from([0, 0])), Struct)).toThrowError(); | ||
}); | ||
test("extended enum inheritance and field value conflict is resolved", () => { | ||
@@ -434,0 +497,0 @@ let Super = class Super { |
@@ -175,8 +175,8 @@ "use strict"; | ||
} | ||
function deserializeStruct(clazz, reader) { | ||
if (typeof clazz.borshDeserialize === "function") { | ||
return clazz.borshDeserialize(reader); | ||
function deserializeStruct(targetClazz, reader) { | ||
if (typeof targetClazz.borshDeserialize === "function") { | ||
return targetClazz.borshDeserialize(reader); | ||
} | ||
const result = {}; | ||
clazz = getSuperMostClass(clazz); | ||
const clazz = getSuperMostClass(targetClazz); | ||
// assume clazz is super class | ||
@@ -261,3 +261,3 @@ if ((0, exports.getVariantIndex)(clazz) !== undefined) { | ||
const classes = [...dependencies.values()].map((f) => f.name).join(', '); | ||
throw new error_1.BorshError(`Multiple ambigious deserialization paths from ${currClazz.name} found: ${classes}. This is not allowed, and would not be performant if allowed`); | ||
throw new error_1.BorshError(`Multiple deserialization paths from ${currClazz.name} found: ${classes} but no matches the variant read from the buffer.`); | ||
} | ||
@@ -273,2 +273,5 @@ } | ||
} | ||
if (!checkClazzesCompatible(currClazz, targetClazz)) { | ||
throw new error_1.BorshError(`Deserialization of ${targetClazz} yielded another Class: ${clazz} which are not compatible`); | ||
} | ||
return Object.assign(new currClazz(), result); | ||
@@ -275,0 +278,0 @@ } |
{ | ||
"name": "@dao-xyz/borsh", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"readme": "README.md", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README", |
@@ -475,2 +475,59 @@ import BN from "bn.js"; | ||
test("extended enum inheritance variants, serialization target does matter for fields", () => { | ||
@variant(0) | ||
class Super {} | ||
@variant(0) | ||
class ClazzA extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
@variant(1) | ||
class ClazzB extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
class Struct { | ||
@field({ type: ClazzA }) | ||
property: ClazzA; | ||
constructor() {} | ||
} | ||
const s = new Struct(); | ||
s.property = new ClazzB(); | ||
expect(() => serialize(s)).toThrowError(); | ||
}); | ||
test("extended enum inheritance variants, deserialization target does matter for fields", () => { | ||
@variant(0) | ||
class Super {} | ||
@variant(0) | ||
class ClazzA extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
@variant(1) | ||
class ClazzB extends Super { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
class Struct { | ||
@field({ type: ClazzB }) | ||
property: ClazzB; | ||
constructor() {} | ||
} | ||
// we try to deserializ [0,0] into Struct, which shouldnot be possible since property is instance of ClazzB | ||
expect(() => | ||
deserialize(Buffer.from(Uint8Array.from([0, 0])), Struct) | ||
).toThrowError(); | ||
}); | ||
test("extended enum inheritance and field value conflict is resolved", () => { | ||
@@ -477,0 +534,0 @@ @variant(1) |
@@ -194,5 +194,5 @@ import bs58 from "bs58"; | ||
function deserializeStruct(clazz: any, reader: BinaryReader) { | ||
if (typeof clazz.borshDeserialize === "function") { | ||
return clazz.borshDeserialize(reader); | ||
function deserializeStruct(targetClazz: any, reader: BinaryReader) { | ||
if (typeof targetClazz.borshDeserialize === "function") { | ||
return targetClazz.borshDeserialize(reader); | ||
} | ||
@@ -202,3 +202,3 @@ | ||
clazz = getSuperMostClass(clazz); | ||
const clazz = getSuperMostClass(targetClazz); | ||
@@ -300,3 +300,3 @@ // assume clazz is super class | ||
const classes = [...dependencies.values()].map((f) => f.name).join(', ') | ||
throw new BorshError(`Multiple ambigious deserialization paths from ${currClazz.name} found: ${classes}. This is not allowed, and would not be performant if allowed`) | ||
throw new BorshError(`Multiple deserialization paths from ${currClazz.name} found: ${classes} but no matches the variant read from the buffer.`) | ||
} | ||
@@ -313,2 +313,6 @@ } | ||
} | ||
if (!checkClazzesCompatible(currClazz, targetClazz)) { | ||
throw new BorshError(`Deserialization of ${targetClazz} yielded another Class: ${clazz} which are not compatible`); | ||
} | ||
return Object.assign(new currClazz(), result); | ||
@@ -315,0 +319,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
Sorry, the diff of this file is not supported yet
341170
6169