Skip to content

Commit

Permalink
Fix struct array fields for nested structs (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Nov 25, 2024
1 parent 657bb08 commit 26ed593
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/shared/log/StructDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,11 @@ export default class StructDecoder {
}
} else {
// Child struct
outputSchemaTypes[valueSchema.name] = valueSchema.type;
let child = this.decode(valueSchema.type, valueArray, valueBitLength);
let isArray = valueSchema.arrayLength !== null;
outputSchemaTypes[valueSchema.name] = valueSchema.type + (isArray ? "[]" : "");
let child = isArray
? this.decodeArray(valueSchema.type, valueArray, valueSchema.arrayLength!)
: this.decode(valueSchema.type, valueArray, valueBitLength);
outputData[valueSchema.name] = child.data;
Object.keys(child.schemaTypes).forEach((field) => {
outputSchemaTypes[valueSchema.name + "/" + field] = child.schemaTypes[field];
Expand All @@ -227,14 +230,18 @@ export default class StructDecoder {
}

/** Converts struct-encoded data with a known array schema to an object. */
decodeArray(name: string, value: Uint8Array): { data: unknown; schemaTypes: { [key: string]: string } } {
decodeArray(
name: string,
value: Uint8Array,
arrayLength?: number
): { data: unknown; schemaTypes: { [key: string]: string } } {
if (!(name in this.schemas)) {
throw new Error("Schema not defined");
}
let outputData: unknown[] = [];
let outputSchemaTypes: { [key: string]: string } = {};
let schemaLength = this.schemas[name].length / 8;
let length = value.length / schemaLength;
let length = arrayLength === undefined ? value.length / schemaLength : arrayLength;
for (let i = 0; i < length; i++) {
let decodedData = this.decode(name, value.slice(i * schemaLength, (i + 1) * schemaLength));
outputData.push(decodedData.data);
Expand Down

0 comments on commit 26ed593

Please sign in to comment.