@dao-xyz/borsh
Advanced tools
Comparing version 2.0.5 to 2.0.6
@@ -601,2 +601,50 @@ "use strict"; | ||
}); | ||
test("variant conflict, index", () => { | ||
const classDef = () => { | ||
class TestStruct { | ||
constructor() { } | ||
} | ||
let A = class A extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
A = __decorate([ | ||
(0, index_1.variant)(0) // Same as B | ||
], A); | ||
let B = class B extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
B = __decorate([ | ||
(0, index_1.variant)(0) // Same as A | ||
], B); | ||
}; | ||
expect(() => classDef()).toThrowError(error_1.BorshError); | ||
}); | ||
test("variant conflict, indices", () => { | ||
const classDef = () => { | ||
class TestStruct { | ||
constructor() { } | ||
} | ||
let A = class A extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
A = __decorate([ | ||
(0, index_1.variant)([0, 1, 2]) // Same as B | ||
], A); | ||
let B = class B extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
}; | ||
B = __decorate([ | ||
(0, index_1.variant)([0, 1, 2]) // Same as A | ||
], B); | ||
}; | ||
expect(() => classDef()).toThrowError(error_1.BorshError); | ||
}); | ||
test("undefined throws error", () => { | ||
@@ -603,0 +651,0 @@ class MissingImplementation { |
@@ -161,3 +161,3 @@ "use strict"; | ||
// We know that we should serialize into the variant that accounts to the first byte of the read | ||
for (const actualClazz of getDependencies(clazz)) { | ||
for (const [_key, actualClazz] of getDependencies(clazz)) { | ||
const variantIndex = (0, exports.getVariantIndex)(actualClazz); | ||
@@ -247,13 +247,16 @@ if (variantIndex !== undefined) { | ||
}; | ||
const setDependency = (ctor, depenency) => { | ||
if (!ctor.prototype._borsh_dependency) { | ||
ctor.prototype._borsh_dependency = []; | ||
const setDependency = (ctor, dependency) => { | ||
let dependencies = getDependencies(ctor); | ||
let key = JSON.stringify((0, exports.getVariantIndex)(dependency)); | ||
if (dependencies.has(key)) { | ||
throw new error_1.BorshError(`Conflicting variants: Dependency ${dependencies.get(key).name} and ${dependency.name} share same variant index(es)`); | ||
} | ||
ctor.prototype._borsh_dependency.push(depenency); | ||
dependencies.set(key, dependency); | ||
ctor.prototype._borsh_dependency = dependencies; | ||
}; | ||
const hasDependencies = (ctor, schema) => { | ||
if (!ctor.prototype._borsh_dependency || ctor.prototype._borsh_dependency.length == 0) { | ||
if (!ctor.prototype._borsh_dependency || ctor.prototype._borsh_dependency.size == 0) { | ||
return false; | ||
} | ||
for (const dependency of ctor.prototype._borsh_dependency) { | ||
for (const [_key, dependency] of getDependencies(ctor)) { | ||
if (!schema.has(dependency)) { | ||
@@ -266,3 +269,3 @@ return false; | ||
const getDependencies = (ctor) => { | ||
return ctor.prototype._borsh_dependency ? ctor.prototype._borsh_dependency : []; | ||
return ctor.prototype._borsh_dependency ? ctor.prototype._borsh_dependency : new Map(); | ||
}; | ||
@@ -284,9 +287,2 @@ const setSchema = (ctor, schema) => { | ||
getOrCreateStructMeta(ctor); | ||
// Define Schema for this class, even though it might miss fields since this is a variant | ||
const clazzes = (0, types_1.extendingClasses)(ctor); | ||
let prev = ctor; | ||
for (const clazz of clazzes) { | ||
setDependency(clazz, prev); // Super classes are marked so we know they have some importance/meaningfulness | ||
prev = clazz; | ||
} | ||
// Create a custom serialization, for enum by prepend instruction index | ||
@@ -313,2 +309,9 @@ ctor.prototype.borshSerialize = function (writer) { | ||
}; | ||
// Define Schema for this class, even though it might miss fields since this is a variant | ||
const clazzes = (0, types_1.extendingClasses)(ctor); | ||
let prev = ctor; | ||
for (const clazz of clazzes) { | ||
setDependency(clazz, prev); // Super classes are marked so we know they have some importance/meaningfulness | ||
prev = clazz; | ||
} | ||
}; | ||
@@ -315,0 +318,0 @@ }; |
{ | ||
"name": "@dao-xyz/borsh", | ||
"version": "2.0.5", | ||
"version": "2.0.6", | ||
"readme": "README.md", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/dao-xyz/borsh-ts#README", |
@@ -36,2 +36,3 @@ # Borsh TS | ||
} from "@dao-xyz/borsh"; | ||
import BN from 'bn.js' | ||
@@ -44,3 +45,3 @@ class SomeClass | ||
@field({'u64'}) | ||
y: number | ||
y: BN | ||
@@ -64,3 +65,3 @@ @field({'String'}) | ||
const value = new SomeClass({ x: 255, y: 20, z: 'abc', q: [1, 2, 3] }); | ||
const value = new SomeClass({ x: 255, y: new BN(20), z: 'abc', q: [1, 2, 3] }); | ||
@@ -91,3 +92,3 @@ // Serialize | ||
**Enum, variant at instruction "slot" 1.** | ||
**Enum, with 2 variants** | ||
@@ -94,0 +95,0 @@ ```typescript |
@@ -662,2 +662,46 @@ import BN from "bn.js"; | ||
test("variant conflict, index", () => { | ||
const classDef = () => { | ||
class TestStruct { | ||
constructor() {} | ||
} | ||
@variant(0) // Same as B | ||
class A extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
@variant(0) // Same as A | ||
class B extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
}; | ||
expect(() => classDef()).toThrowError(BorshError); | ||
}); | ||
test("variant conflict, indices", () => { | ||
const classDef = () => { | ||
class TestStruct { | ||
constructor() {} | ||
} | ||
@variant([0, 1, 2]) // Same as B | ||
class A extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
@variant([0, 1, 2]) // Same as A | ||
class B extends TestStruct { | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
}; | ||
expect(() => classDef()).toThrowError(BorshError); | ||
}); | ||
test("undefined throws error", () => { | ||
@@ -664,0 +708,0 @@ class MissingImplementation { |
@@ -180,3 +180,3 @@ import bs58 from "bs58"; | ||
// We know that we should serialize into the variant that accounts to the first byte of the read | ||
for (const actualClazz of getDependencies(clazz)) { | ||
for (const [_key, actualClazz] of getDependencies(clazz)) { | ||
const variantIndex = getVariantIndex(actualClazz); | ||
@@ -287,15 +287,18 @@ if (variantIndex !== undefined) { | ||
const setDependency = (ctor: Function, depenency: Function) => { | ||
if (!ctor.prototype._borsh_dependency) { | ||
ctor.prototype._borsh_dependency = [] | ||
const setDependency = (ctor: Function, dependency: Function) => { | ||
let dependencies = getDependencies(ctor); | ||
let key = JSON.stringify(getVariantIndex(dependency)); | ||
if (dependencies.has(key)) { | ||
throw new BorshError(`Conflicting variants: Dependency ${dependencies.get(key).name} and ${dependency.name} share same variant index(es)`) | ||
} | ||
ctor.prototype._borsh_dependency.push(depenency); | ||
dependencies.set(key, dependency); | ||
ctor.prototype._borsh_dependency = dependencies; | ||
} | ||
const hasDependencies = (ctor: Function, schema: Map<any, StructKind>): boolean => { | ||
if (!ctor.prototype._borsh_dependency || ctor.prototype._borsh_dependency.length == 0) { | ||
if (!ctor.prototype._borsh_dependency || ctor.prototype._borsh_dependency.size == 0) { | ||
return false | ||
} | ||
for (const dependency of ctor.prototype._borsh_dependency) { | ||
for (const [_key, dependency] of getDependencies(ctor)) { | ||
if (!schema.has(dependency)) { | ||
@@ -308,6 +311,8 @@ return false; | ||
const getDependencies = (ctor: Function): Function[] => { | ||
return ctor.prototype._borsh_dependency ? ctor.prototype._borsh_dependency : [] | ||
const getDependencies = (ctor: Function): Map<string, Function> => { | ||
return ctor.prototype._borsh_dependency ? ctor.prototype._borsh_dependency : new Map(); | ||
} | ||
const setSchema = (ctor: Function, schema: StructKind) => { | ||
@@ -330,12 +335,2 @@ ctor.prototype._borsh_schema = schema; | ||
// Define Schema for this class, even though it might miss fields since this is a variant | ||
const clazzes = extendingClasses(ctor); | ||
let prev = ctor; | ||
for (const clazz of clazzes) { | ||
setDependency(clazz, prev); // Super classes are marked so we know they have some importance/meaningfulness | ||
prev = clazz; | ||
} | ||
// Create a custom serialization, for enum by prepend instruction index | ||
@@ -370,2 +365,11 @@ ctor.prototype.borshSerialize = function ( | ||
}; | ||
// Define Schema for this class, even though it might miss fields since this is a variant | ||
const clazzes = extendingClasses(ctor); | ||
let prev = ctor; | ||
for (const clazz of clazzes) { | ||
setDependency(clazz, prev); // Super classes are marked so we know they have some importance/meaningfulness | ||
prev = clazz; | ||
} | ||
}; | ||
@@ -372,0 +376,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
264648
4688
279