Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "fcbuffer", | ||
"description": "Serialization library geared towards immutable data storage such as blockchains.", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -91,5 +91,22 @@ const ByteBuffer = require('bytebuffer') | ||
if (!typeatty) return null | ||
const {name, arrayType, optional} = typeatty | ||
const {name, annotation, arrayType, optional} = typeatty | ||
let ret | ||
if (arrayType == null) { | ||
if(annotation) { | ||
// AnyType<FieldName, TypeName> | ||
const type = types[name] | ||
if(type == null) { | ||
errors.push(`Missing ${name} in ${Type}`) | ||
return null | ||
} | ||
const annTypes = [] | ||
for(let annTypeName of annotation) { | ||
const annType = getTypeOrStruct(annTypeName) | ||
if(!annType) { | ||
errors.push(`Missing ${annTypeName} in ${Type}`) | ||
return null | ||
} | ||
annTypes.push(annType) | ||
} | ||
ret = type(annTypes) | ||
} else if (arrayType == null) { | ||
// AnyType | ||
@@ -158,3 +175,12 @@ const fieldStruct = structs[name] | ||
const arrayMatch = name.match(/\[(.*)\]/) // supports nested arrays | ||
const annotationMatch = name.match(/<(.*)>/) | ||
if(annotationMatch) { | ||
const annotation = annotationMatch ? | ||
annotationMatch[1].replace(/ /g, '').split(',') : null | ||
name = name.replace(annotationMatch[0], '').trim() | ||
return {name, annotation} | ||
} | ||
const arrayMatch = name.match(/\[(.*)\]/) | ||
const arrayType = arrayMatch ? arrayMatch[1].trim() : null | ||
@@ -161,0 +187,0 @@ |
@@ -10,2 +10,3 @@ const BN = require('bn.js') | ||
Time: () => [time], | ||
Map: (annotation) => [map, {annotation}], | ||
@@ -78,2 +79,63 @@ FixedString16: () => [string, {maxLen: 16}], | ||
const map = validation => { | ||
const {annotation: [type1, type2]} = validation | ||
if (!isSerializer(type1)) { throw new TypeError(`Map<type1, > unknown`) } | ||
if (!isSerializer(type2)) { throw new TypeError(`Map<, type2> unknown`) } | ||
return { | ||
fromByteBuffer (b) { | ||
const size = b.readVarint32() | ||
const result = {} | ||
for (let i = 0; i < size; i++) { | ||
result[type1.fromByteBuffer(b)] = type2.fromByteBuffer(b) | ||
} | ||
if (validation.debug) { | ||
console.log('0x' + size.toString(16), '(Map.fromByteBuffer length)', result) | ||
} | ||
return result | ||
}, | ||
appendByteBuffer (b, value) { | ||
validate(value, validation) | ||
const keys = Object.keys(value) | ||
b.writeVarint32(keys.length) | ||
if (validation.debug) { | ||
console.log('0x' + keys.length.toString(16), '(Map.appendByteBuffer length)', keys) | ||
} | ||
// if(sorted) { | ||
// value = sortKeys(type1, Object.assign({}, value)) | ||
// } | ||
for (const o of keys) { | ||
const value2 = value[o] | ||
type1.appendByteBuffer(b, o) | ||
type2.appendByteBuffer(b, value2) | ||
} | ||
}, | ||
fromObject (value) { | ||
validate(value, validation) | ||
const result = {} | ||
// if(sorted) { | ||
// value = sortKeys(type1, Object.assign({}, value)) | ||
// } | ||
for (const o in value) { | ||
result[type1.fromObject(o)] = type2.fromObject(value[o]) | ||
} | ||
return result | ||
}, | ||
toObject (value) { | ||
if (validation.defaults && value == null) { | ||
return {[type1.toObject(null)]: type2.toObject(null)} | ||
} | ||
validate(value, validation) | ||
const result = {} | ||
// if(sorted) { | ||
// value = sortKey(type1, Object.assign({}, value)) | ||
// } | ||
for (const o in value) { | ||
result[type1.toObject(o)] = type2.toObject(value[o]) | ||
} | ||
return result | ||
} | ||
} | ||
} | ||
const vector = validation => { | ||
@@ -98,8 +160,8 @@ const {type, sorted} = validation | ||
b.writeVarint32(value.length) | ||
if (validation.debug) { | ||
console.log('0x' + value.length.toString(16), '(Vector.appendByteBuffer length)') | ||
} | ||
if(sorted) { | ||
value = sort(type, Object.assign([], value)) | ||
} | ||
if (validation.debug) { | ||
console.log('0x' + value.length.toString(16), '(Vector.appendByteBuffer length)', value) | ||
} | ||
for (const o of value) { | ||
@@ -106,0 +168,0 @@ type.appendByteBuffer(b, o) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73271
1182