Skip to content

Commit fb20ad1

Browse files
committed
Fixes issue where custom values with single toplevel subtype were prefixed with size bytes
1 parent 2246fd6 commit fb20ad1

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@highmobility/auto-api-javascript",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Auto API for JavaScript - the parsing library for the Auto API vehicle data model",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/values/CustomValue.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
5151
}
5252

5353
public decode(bytes: number[]) {
54-
const { definition } = this;
54+
const { definition, hasItems } = this;
5555
const items = definition.items || [definition];
5656

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

6161
const [count, chunk] = bytesToChunk(bytes.slice(offset), itemTypeDefinition.size, () =>
62-
this.isVariableSizeSubtype(itemTypeDefinition),
62+
this.isVariableSizeSubtype(itemTypeDefinition, hasItems),
6363
);
6464

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

76-
this._value = definition.items
77-
? values
78-
: (last(getKeyValuePairFromObject<Value>(values)) as Value);
76+
this._value = hasItems ? values : (last(getKeyValuePairFromObject<Value>(values)) as Value);
7977

8078
return this;
8179
}
@@ -92,12 +90,16 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
9290
return this;
9391
}
9492

93+
public get hasItems() {
94+
return !!this.definition.items;
95+
}
96+
9597
public get name() {
9698
return this.definition.name;
9799
}
98100

99101
public setValue(value: unknown) {
100-
if (this.definition.items) {
102+
if (this.hasItems) {
101103
if (isObject(value)) {
102104
this.assignValueToItems(value);
103105
} else {
@@ -153,7 +155,7 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
153155
const { size } = this.resolveTypeDefinitionFromRef(definition);
154156
const bytes = value.encode();
155157

156-
return size ? bytes : bytesWithSize(bytes);
158+
return size || !this.hasItems ? bytes : bytesWithSize(bytes);
157159
}
158160

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

176-
protected isVariableSizeSubtype({ customType, type }: TypeDefinition) {
177-
return !!customType || CustomValue.VariableSizeSubtypes.includes(type as TypeDefinitionType);
178+
protected isVariableSizeSubtype({ customType, type }: TypeDefinition, hasItems = true) {
179+
return (
180+
hasItems &&
181+
(!!customType || CustomValue.VariableSizeSubtypes.includes(type as TypeDefinitionType))
182+
);
178183
}
179184
}

0 commit comments

Comments
 (0)