@@ -186,13 +186,14 @@ internal class IndefiniteLengthCborWriter(cbor: Cbor, private val output: ByteAr
186
186
}
187
187
188
188
// optimized indefinite length encoder
189
- internal class StructuredCborWriter (cbor : Cbor ) : CborWriter(
190
- cbor
191
- ) {
189
+ internal class StructuredCborWriter (cbor : Cbor ) : CborWriter(cbor) {
192
190
193
- sealed class CborContainer (tags : ULongArray , elements : MutableList <CborElement >) {
194
- var elements = elements
195
- private set
191
+ /* *
192
+ * Tags and values are "written", i.e. recorded/prepared for encoding separately. Hence, we need a helper that allows
193
+ * for setting tags and values independently, and then merging them into the final [CborElement] at the end.
194
+ */
195
+ internal sealed class CborContainer (tags : ULongArray , elements : MutableList <CborElement >) {
196
+ protected val elements = elements
196
197
197
198
var tags = tags
198
199
internal set
@@ -207,7 +208,7 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
207
208
208
209
class Primitive (tags : ULongArray ) : CborContainer(tags, elements = mutableListOf()) {
209
210
override fun add (element : CborElement ): Boolean {
210
- require(elements.isEmpty()) {" Implementation error. Please report a bug." }
211
+ require(elements.isEmpty()) { " Implementation error. Please report a bug." }
211
212
return elements.add(element)
212
213
}
213
214
}
@@ -226,6 +227,11 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
226
227
}
227
228
}
228
229
230
+ private operator fun CborContainer?.plusAssign (element : CborElement ) {
231
+ this !! .add(element)
232
+ }
233
+
234
+
229
235
private val stack = ArrayDeque <CborContainer >()
230
236
private var currentElement: CborContainer ? = null
231
237
@@ -252,14 +258,14 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
252
258
val finalized = currentElement!! .finalize()
253
259
if (stack.isNotEmpty()) {
254
260
currentElement = stack.removeLast()
255
- currentElement!! .add( finalized)
261
+ currentElement + = finalized
256
262
}
257
263
}
258
264
259
265
override fun getDestination () = throw IllegalStateException (" There is not byteArrayOutput" )
260
266
261
-
262
- override fun incrementChildren () { /* NOOP*/
267
+ override fun incrementChildren () {
268
+ /* NOOP*/
263
269
}
264
270
265
271
@@ -276,11 +282,9 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
276
282
// indices are put into the name field. we don't want to write those, as it would result in double writes
277
283
val cborLabel = descriptor.getCborLabel(index)
278
284
if (cbor.configuration.preferCborLabelsOverNames && cborLabel != null ) {
279
- currentElement!! .add(
280
- CborInt .invoke(value = cborLabel, tags = keyTags ? : ulongArrayOf())
281
- )
285
+ currentElement + = CborInt (value = cborLabel, tags = keyTags ? : ulongArrayOf())
282
286
} else {
283
- currentElement!! .add( CborString (name, keyTags ? : ulongArrayOf() ))
287
+ currentElement + = CborString (name, keyTags ? : ulongArrayOf())
284
288
}
285
289
}
286
290
}
@@ -295,51 +299,51 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
295
299
296
300
297
301
override fun encodeBoolean (value : Boolean ) {
298
- currentElement!! .add( CborBoolean (value) )
302
+ currentElement + = CborBoolean (value)
299
303
}
300
304
301
305
override fun encodeByte (value : Byte ) {
302
- currentElement!! .add( CborInt (value.toLong() ))
306
+ currentElement + = CborInt (value.toLong())
303
307
}
304
308
305
309
override fun encodeChar (value : Char ) {
306
- currentElement!! .add( CborInt (value.code.toLong() ))
310
+ currentElement + = CborInt (value.code.toLong())
307
311
}
308
312
309
313
override fun encodeDouble (value : Double ) {
310
- currentElement!! .add( CborDouble (value) )
314
+ currentElement + = CborDouble (value)
311
315
}
312
316
313
317
override fun encodeFloat (value : Float ) {
314
- currentElement!! .add( CborDouble (value.toDouble() ))
318
+ currentElement + = CborDouble (value.toDouble())
315
319
}
316
320
317
321
override fun encodeInt (value : Int ) {
318
- currentElement!! .add( CborInt (value.toLong() ))
322
+ currentElement + = CborInt (value.toLong())
319
323
}
320
324
321
325
override fun encodeLong (value : Long ) {
322
- currentElement!! .add( CborInt (value) )
326
+ currentElement + = CborInt (value)
323
327
}
324
328
325
329
override fun encodeShort (value : Short ) {
326
- currentElement!! .add( CborInt (value.toLong() ))
330
+ currentElement + = CborInt (value.toLong())
327
331
}
328
332
329
333
override fun encodeString (value : String ) {
330
- currentElement!! .add( CborString (value) )
334
+ currentElement + = CborString (value)
331
335
}
332
336
333
337
override fun encodeByteString (byteArray : ByteArray ) {
334
- currentElement!! .add( CborByteString (byteArray) )
338
+ currentElement + = CborByteString (byteArray)
335
339
}
336
340
337
341
override fun encodeNull () {
338
- currentElement!! .add( CborNull () )
342
+ currentElement + = CborNull ()
339
343
}
340
344
341
345
override fun encodeEnum (enumDescriptor : SerialDescriptor , index : Int ) {
342
- currentElement!! .add( CborString (enumDescriptor.getElementName(index) ))
346
+ currentElement + = CborString (enumDescriptor.getElementName(index))
343
347
}
344
348
345
349
}
0 commit comments