Skip to content

Commit

Permalink
refactor: improve path of read error
Browse files Browse the repository at this point in the history
  • Loading branch information
askuzminov committed Dec 25, 2020
1 parent aa95cf2 commit 6ff5e20
Show file tree
Hide file tree
Showing 6 changed files with 1,684 additions and 1,227 deletions.
18 changes: 13 additions & 5 deletions src/binary/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ export const Fixed32 = 5; // 32-bit: float, fixed32, sfixed32
export const SHIFT_LEFT_32 = (1 << 16) * (1 << 16);
export const SHIFT_RIGHT_32 = 1 / SHIFT_LEFT_32;

export const MapStruct = (item: FieldMap): Field => () => [
[1, 'key', item[1], 1],
[2, 'value', item[2], 1],
];
export function MapStruct(item: FieldMap): Field {
return function MapValue() {
return [
[1, 'key', item[1], 1],
[2, 'value', item[2], 1],
];
};
}

export const WrapperStruct = (value: string): Field => () => [[1, 'value', value, 1]];
export function WrapperStruct(value: string): Field {
return function Wrapper() {
return [[1, 'value', value, 1]];
};
}
24 changes: 16 additions & 8 deletions src/binary/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,19 @@ const GetDefault = (item: FieldType) =>
? getDefault(item[1])
: undefined;

function DecodeHelper<T extends Record<string, any>>(b: BufRead, struct: Field, result = {} as T, end?: number) {
function DecodeHelper<T extends Record<string, any>>(
b: BufRead,
struct: Field,
result = {} as T,
path = '',
end?: number
) {
const { s } = GetStruct(struct);

end = end || b.buf.length;

b.path = path;

while (b.pos < end) {
const val = readVarint(b);
const tag = val >> 3;
Expand All @@ -147,8 +155,8 @@ function DecodeHelper<T extends Record<string, any>>(b: BufRead, struct: Field,
b.type = val & 0x7;

if (s[tag]) {
const [, nameField, item, , oneof] = s[tag];
DecodeRead(b, nameField, result, item);
const [id, nameField, item, , oneof] = s[tag];
DecodeRead(b, nameField, result, `${path ? `${path}.` : ''}${nameField}[${id}]`, item);

if (oneof) {
if (result[nameField]) {
Expand All @@ -167,13 +175,13 @@ function DecodeHelper<T extends Record<string, any>>(b: BufRead, struct: Field,
return DecodeDefault(struct, result);
}

function DecodeRead(b: BufRead, fieldName: string, result: Record<string, unknown>, item: FieldType) {
function DecodeRead(b: BufRead, fieldName: string, result: Record<string, unknown>, path: string, item: FieldType) {
if (isString(item)) {
if (readMap[item as ReadMapKeys]) {
result[fieldName] = getDefault(item, readMap[item as ReadMapKeys](b));
}
} else if (isFunction(item)) {
result[fieldName] = DecodeHelper(b, item, {}, readVarint(b) + b.pos);
result[fieldName] = DecodeHelper(b, item, {}, path, readVarint(b) + b.pos);
} else if (isArray(item)) {
if (item[0] === 'repeated') {
if (isString(item[1]) && PACKED[item[1]]) {
Expand All @@ -183,17 +191,17 @@ function DecodeRead(b: BufRead, fieldName: string, result: Record<string, unknow
const m = isArray(mm) ? mm : [];
result[fieldName] = m;
const o = {} as { out: unknown };
DecodeRead(b, 'out', o, item[1]);
DecodeRead(b, 'out', o, path, item[1]);
m.push(o.out);
}
} else if (item[0] === 'map') {
const mm = result[fieldName];
const m = isObject(mm) ? mm : {};
result[fieldName] = m;
const o = DecodeHelper(b, MapStruct(item), {} as { key: string; value: unknown }, readVarint(b) + b.pos);
const o = DecodeHelper(b, MapStruct(item), {} as { key: string; value: unknown }, path, readVarint(b) + b.pos);
m[o.key] = o.value;
} else if (item[0] === 'wrapper') {
const o = DecodeHelper(b, WrapperStruct(item[1]), {} as { value: unknown }, readVarint(b) + b.pos);
const o = DecodeHelper(b, WrapperStruct(item[1]), {} as { value: unknown }, path, readVarint(b) + b.pos);
result[fieldName] = o.value;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/binary/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { readUtf8 } from './utf8';
export class BufRead {
pos = 0;
type = 0;
path = '';

constructor(public buf: Uint8Array) {}
}
Expand All @@ -20,7 +21,7 @@ export const skip = (b: BufRead, val: number) => {
} else if (type === Fixed64) {
b.pos += 8;
} else {
throw new Error(`Unimplemented type: ${type}`);
throw new Error(`Unimplemented type: ${type} in path "${b.path}"`);
}
};

Expand Down Expand Up @@ -79,7 +80,7 @@ function readVarintRemainder(low: number, b: BufRead) {
return [low, high];
}

throw new Error('Expected varint not more than 10 bytes');
throw new Error(`Expected varint not more than 10 bytes in path "${b.path}"`);
}

const toNum = ([low, high]: number[], s: boolean) => {
Expand Down
4 changes: 2 additions & 2 deletions src/generator/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ function message(pack: string, out: MakeOuts, item: Message, list: List[], enums
}

out.dts.push(`export const ${baseName}: Field<${baseName}>;`);
out.js.push(`export const ${baseName} = () => [`);
out.js.push(`export function ${baseName} () { return [`);
for (const field of runtime) {
out.js.push(field);
}
out.js.push('];');
out.js.push(']; }');
}

0 comments on commit 6ff5e20

Please sign in to comment.