Skip to content

Commit

Permalink
Struct decoder fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbonner committed Sep 6, 2023
1 parent 2c9a0d9 commit bfaef65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/shared/log/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ export default class Log {
this.putBoolean(key, timestamp, value, true);
return;
case "number":
if (!allowRootWrite) return;
this.readOnlyFields.add(key);
this.putNumber(key, timestamp, value, true);
return;
Expand Down Expand Up @@ -373,7 +374,7 @@ export default class Log {
let lengthKey = key + "/length";
this.createBlankField(lengthKey, LoggableType.Number);
this.processTimestamp(lengthKey, timestamp);
this.readOnlyFields.add(key);
this.readOnlyFields.add(lengthKey);
this.fields[lengthKey].putNumber(timestamp, value.length);
}
for (let i = 0; i < value.length; i++) {
Expand Down
31 changes: 12 additions & 19 deletions src/shared/log/StructDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,10 @@ export default class StructDecoder {
}

/** Converts struct-encoded data with a known schema to an object. */
decode(
name: string,
value: Uint8Array
): { data: { [key: string]: unknown }; schemaTypes: { [key: string]: string } } {
decode(name: string, value: Uint8Array): { data: unknown; schemaTypes: { [key: string]: string } } {
if (!(name in this.schemas)) {
return {
data: {},
data: null,
schemaTypes: {}
};
}
Expand Down Expand Up @@ -208,15 +205,17 @@ export default class StructDecoder {
)
);
}
outputData[valueSchema.name] = value;
if (type === ValueType.Char) {
outputData[valueSchema.name] = value.join("");
} else {
outputData[valueSchema.name] = value;
}
}
} else {
// Child struct
outputSchemaTypes[valueSchema.name] = valueSchema.type;
let child = this.decode(valueSchema.type, StructDecoder.toUint8Array(valueBoolArray));
Object.keys(child.data).forEach((field) => {
outputData[valueSchema.name + "/" + field] = child.data[field];
});
outputData[valueSchema.name] = child.data;
Object.keys(child.schemaTypes).forEach((field) => {
outputSchemaTypes[valueSchema.name + "/" + field] = child.schemaTypes[field];
});
Expand All @@ -229,26 +228,20 @@ export default class StructDecoder {
}

/** Converts struct-encoded data with a known array schema to an object. */
decodeArray(
name: string,
value: Uint8Array
): { data: { [key: string]: unknown }; schemaTypes: { [key: string]: string } } {
decodeArray(name: string, value: Uint8Array): { data: unknown; schemaTypes: { [key: string]: string } } {
if (!(name in this.schemas)) {
return {
data: {},
data: null,
schemaTypes: {}
};
}
let outputData: { [key: string]: unknown } = {};
let outputData: unknown[] = [];
let outputSchemaTypes: { [key: string]: string } = {};
let schemaLength = this.schemas[name].length / 8;
let length = value.length / schemaLength;
outputData["length"] = length;
for (let i = 0; i < length; i++) {
let decodedData = this.decode(name, value.slice(i * schemaLength, (i + 1) * schemaLength));
Object.entries(decodedData.data).forEach(([itemKey, itemValue]) => {
outputData[i.toString() + "/" + itemKey] = itemValue;
});
outputData.push(decodedData.data);
Object.entries(decodedData.schemaTypes).forEach(([itemKey, itemSchemaType]) => {
outputSchemaTypes[i.toString() + "/" + itemKey] = itemSchemaType;
});
Expand Down

0 comments on commit bfaef65

Please sign in to comment.