@@ -186,13 +186,14 @@ internal class IndefiniteLengthCborWriter(cbor: Cbor, private val output: ByteAr
186186}
187187
188188// optimized indefinite length encoder
189- internal class StructuredCborWriter (cbor : Cbor ) : CborWriter(
190- cbor
191- ) {
189+ internal class StructuredCborWriter (cbor : Cbor ) : CborWriter(cbor) {
192190
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
196197
197198 var tags = tags
198199 internal set
@@ -207,7 +208,7 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
207208
208209 class Primitive (tags : ULongArray ) : CborContainer(tags, elements = mutableListOf()) {
209210 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." }
211212 return elements.add(element)
212213 }
213214 }
@@ -226,6 +227,11 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
226227 }
227228 }
228229
230+ private operator fun CborContainer?.plusAssign (element : CborElement ) {
231+ this !! .add(element)
232+ }
233+
234+
229235 private val stack = ArrayDeque <CborContainer >()
230236 private var currentElement: CborContainer ? = null
231237
@@ -252,14 +258,14 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
252258 val finalized = currentElement!! .finalize()
253259 if (stack.isNotEmpty()) {
254260 currentElement = stack.removeLast()
255- currentElement!! .add( finalized)
261+ currentElement + = finalized
256262 }
257263 }
258264
259265 override fun getDestination () = throw IllegalStateException (" There is not byteArrayOutput" )
260266
261-
262- override fun incrementChildren () { /* NOOP*/
267+ override fun incrementChildren () {
268+ /* NOOP*/
263269 }
264270
265271
@@ -276,11 +282,9 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
276282 // indices are put into the name field. we don't want to write those, as it would result in double writes
277283 val cborLabel = descriptor.getCborLabel(index)
278284 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())
282286 } else {
283- currentElement!! .add( CborString (name, keyTags ? : ulongArrayOf() ))
287+ currentElement + = CborString (name, keyTags ? : ulongArrayOf())
284288 }
285289 }
286290 }
@@ -295,51 +299,51 @@ internal class StructuredCborWriter(cbor: Cbor) : CborWriter(
295299
296300
297301 override fun encodeBoolean (value : Boolean ) {
298- currentElement!! .add( CborBoolean (value) )
302+ currentElement + = CborBoolean (value)
299303 }
300304
301305 override fun encodeByte (value : Byte ) {
302- currentElement!! .add( CborInt (value.toLong() ))
306+ currentElement + = CborInt (value.toLong())
303307 }
304308
305309 override fun encodeChar (value : Char ) {
306- currentElement!! .add( CborInt (value.code.toLong() ))
310+ currentElement + = CborInt (value.code.toLong())
307311 }
308312
309313 override fun encodeDouble (value : Double ) {
310- currentElement!! .add( CborDouble (value) )
314+ currentElement + = CborDouble (value)
311315 }
312316
313317 override fun encodeFloat (value : Float ) {
314- currentElement!! .add( CborDouble (value.toDouble() ))
318+ currentElement + = CborDouble (value.toDouble())
315319 }
316320
317321 override fun encodeInt (value : Int ) {
318- currentElement!! .add( CborInt (value.toLong() ))
322+ currentElement + = CborInt (value.toLong())
319323 }
320324
321325 override fun encodeLong (value : Long ) {
322- currentElement!! .add( CborInt (value) )
326+ currentElement + = CborInt (value)
323327 }
324328
325329 override fun encodeShort (value : Short ) {
326- currentElement!! .add( CborInt (value.toLong() ))
330+ currentElement + = CborInt (value.toLong())
327331 }
328332
329333 override fun encodeString (value : String ) {
330- currentElement!! .add( CborString (value) )
334+ currentElement + = CborString (value)
331335 }
332336
333337 override fun encodeByteString (byteArray : ByteArray ) {
334- currentElement!! .add( CborByteString (byteArray) )
338+ currentElement + = CborByteString (byteArray)
335339 }
336340
337341 override fun encodeNull () {
338- currentElement!! .add( CborNull () )
342+ currentElement + = CborNull ()
339343 }
340344
341345 override fun encodeEnum (enumDescriptor : SerialDescriptor , index : Int ) {
342- currentElement!! .add( CborString (enumDescriptor.getElementName(index) ))
346+ currentElement + = CborString (enumDescriptor.getElementName(index))
343347 }
344348
345349}
0 commit comments