@@ -129,6 +129,11 @@ func encodeMap(m *Map) (any, error) {
129
129
// steal and modify its header, and build our own output from pieces. This avoids
130
130
// needing to support 6 different possible encodings of a map's header byte depending
131
131
// on length
132
+ useIndef := false
133
+ if m .useIndef != nil {
134
+ useIndef = * m .useIndef
135
+ }
136
+ // Build encoded pairs
132
137
tmpPairs := make ([][]byte , 0 , len (m .Pairs ))
133
138
for _ , pair := range m .Pairs {
134
139
key , err := encodeToRaw (pair [0 ])
@@ -152,19 +157,27 @@ func encodeMap(m *Map) (any, error) {
152
157
slices .Concat (keyRaw , valueRaw ),
153
158
)
154
159
}
155
- // Create dummy list with simple (one-byte) values so we can easily extract the header
156
- tmpList := make ([]bool , len (tmpPairs ))
157
- tmpListRaw , err := cborMarshal (tmpList )
158
- if err != nil {
159
- return nil , err
160
- }
161
- tmpListHeader := tmpListRaw [0 : len (tmpListRaw )- len (tmpPairs )]
162
- // Modify header byte to switch type from array to map
163
- tmpListHeader [0 ] |= 0x20
164
160
// Build return value
165
161
ret := bytes .NewBuffer (nil )
166
- _ , _ = ret .Write (tmpListHeader )
162
+ if useIndef {
163
+ ret .WriteByte (CborTypeMap | CborIndefFlag )
164
+ } else {
165
+ // Create dummy list with simple (one-byte) values so we can easily extract the header
166
+ tmpList := make ([]bool , len (tmpPairs ))
167
+ tmpListRaw , err := cborMarshal (tmpList )
168
+ if err != nil {
169
+ return nil , err
170
+ }
171
+ tmpListHeader := tmpListRaw [0 : len (tmpListRaw )- len (tmpPairs )]
172
+ // Modify header byte to switch type from array to map
173
+ tmpListHeader [0 ] |= 0x20
174
+ _ , _ = ret .Write (tmpListHeader )
175
+ }
167
176
_ , _ = ret .Write (slices .Concat (tmpPairs ... ))
177
+ if useIndef {
178
+ // Indef-length "break" byte
179
+ ret .WriteByte (0xff )
180
+ }
168
181
return cbor .RawMessage (ret .Bytes ()), nil
169
182
}
170
183
0 commit comments