Skip to content

Commit

Permalink
Error out on wrong fixed size array
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Nov 15, 2019
1 parent c0b0f67 commit 52aa7c1
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src.ts/utils/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ export class BinaryReader {
}

function serializeField(schema: Schema, value: any, fieldType: any, writer: any) {
// TODO: Handle missing values properly (make sure they never result in just skipped write)
if (typeof fieldType === 'string') {
writer[`write_${fieldType}`](value);
} else if (fieldType instanceof Array) {
if (typeof fieldType[0] === 'number') {
if (value.length != fieldType[0]) {
throw new Error(`Expecting byte array of length ${fieldType[0]}, but got ${value.length} bytes`);
}
writer.write_fixed_array(value);
} else {
writer.write_array(value, (item: any) => { serializeField(schema, item, fieldType[0], writer); });
Expand Down

0 comments on commit 52aa7c1

Please sign in to comment.