Skip to content

Commit

Permalink
Fixes issue where custom values with single toplevel subtype were pre…
Browse files Browse the repository at this point in the history
…fixed with size bytes
  • Loading branch information
unematiii committed Apr 27, 2021
1 parent 2246fd6 commit fb20ad1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@highmobility/auto-api-javascript",
"version": "0.0.8",
"version": "0.0.9",
"description": "Auto API for JavaScript - the parsing library for the Auto API vehicle data model",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
23 changes: 14 additions & 9 deletions src/values/CustomValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
}

public decode(bytes: number[]) {
const { definition } = this;
const { definition, hasItems } = this;
const items = definition.items || [definition];

const [values] = items.reduce<[CustomValueItems, number]>(
([values, offset], item) => {
const itemTypeDefinition = this.resolveTypeDefinitionFromRef(item);

const [count, chunk] = bytesToChunk(bytes.slice(offset), itemTypeDefinition.size, () =>
this.isVariableSizeSubtype(itemTypeDefinition),
this.isVariableSizeSubtype(itemTypeDefinition, hasItems),
);

return [
Expand All @@ -73,9 +73,7 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
[{}, 0],
);

this._value = definition.items
? values
: (last(getKeyValuePairFromObject<Value>(values)) as Value);
this._value = hasItems ? values : (last(getKeyValuePairFromObject<Value>(values)) as Value);

return this;
}
Expand All @@ -92,12 +90,16 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
return this;
}

public get hasItems() {
return !!this.definition.items;
}

public get name() {
return this.definition.name;
}

public setValue(value: unknown) {
if (this.definition.items) {
if (this.hasItems) {
if (isObject(value)) {
this.assignValueToItems(value);
} else {
Expand Down Expand Up @@ -153,7 +155,7 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
const { size } = this.resolveTypeDefinitionFromRef(definition);
const bytes = value.encode();

return size ? bytes : bytesWithSize(bytes);
return size || !this.hasItems ? bytes : bytesWithSize(bytes);
}

protected getItemTypeDefinition(name: string) {
Expand All @@ -173,7 +175,10 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
: type;
}

protected isVariableSizeSubtype({ customType, type }: TypeDefinition) {
return !!customType || CustomValue.VariableSizeSubtypes.includes(type as TypeDefinitionType);
protected isVariableSizeSubtype({ customType, type }: TypeDefinition, hasItems = true) {
return (
hasItems &&
(!!customType || CustomValue.VariableSizeSubtypes.includes(type as TypeDefinitionType))
);
}
}

0 comments on commit fb20ad1

Please sign in to comment.