@@ -51,15 +51,15 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
51
51
}
52
52
53
53
public decode ( bytes : number [ ] ) {
54
- const { definition } = this ;
54
+ const { definition, hasItems } = this ;
55
55
const items = definition . items || [ definition ] ;
56
56
57
57
const [ values ] = items . reduce < [ CustomValueItems , number ] > (
58
58
( [ values , offset ] , item ) => {
59
59
const itemTypeDefinition = this . resolveTypeDefinitionFromRef ( item ) ;
60
60
61
61
const [ count , chunk ] = bytesToChunk ( bytes . slice ( offset ) , itemTypeDefinition . size , ( ) =>
62
- this . isVariableSizeSubtype ( itemTypeDefinition ) ,
62
+ this . isVariableSizeSubtype ( itemTypeDefinition , hasItems ) ,
63
63
) ;
64
64
65
65
return [
@@ -73,9 +73,7 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
73
73
[ { } , 0 ] ,
74
74
) ;
75
75
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 ) ;
79
77
80
78
return this ;
81
79
}
@@ -92,12 +90,16 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
92
90
return this ;
93
91
}
94
92
93
+ public get hasItems ( ) {
94
+ return ! ! this . definition . items ;
95
+ }
96
+
95
97
public get name ( ) {
96
98
return this . definition . name ;
97
99
}
98
100
99
101
public setValue ( value : unknown ) {
100
- if ( this . definition . items ) {
102
+ if ( this . hasItems ) {
101
103
if ( isObject ( value ) ) {
102
104
this . assignValueToItems ( value ) ;
103
105
} else {
@@ -153,7 +155,7 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
153
155
const { size } = this . resolveTypeDefinitionFromRef ( definition ) ;
154
156
const bytes = value . encode ( ) ;
155
157
156
- return size ? bytes : bytesWithSize ( bytes ) ;
158
+ return size || ! this . hasItems ? bytes : bytesWithSize ( bytes ) ;
157
159
}
158
160
159
161
protected getItemTypeDefinition ( name : string ) {
@@ -173,7 +175,10 @@ export class CustomValue extends Value<CustomValueData, CustomValueSetter> imple
173
175
: type ;
174
176
}
175
177
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
+ ) ;
178
183
}
179
184
}
0 commit comments