@@ -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