Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protobuf packed encoding/decoding (#1830)
* Create an annotation to request packing of collections. * The specification only allows packing for primitive types (wire types 1, 2 or 5) to allow decoders decode either format independently of the proto specification. * Make pushback work in respect to currentType/currentId. This allows it to be used to effectively peek the type without assumptions on state. * Clarify in the documentation that reading will (per the standard) supports inputs in either format, independent of the annotation. The annotation only affects writing. * Support decoding "packed" arrays as toplevels. Add tests for handling of strings and "packed" toplevel arrays. The checking for eof works as bytesize is always >= array length.
- Loading branch information
Showing
11 changed files
with
296 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...ats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/PackedArrayDecoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package kotlinx.serialization.protobuf.internal | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.protobuf.* | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal class PackedArrayDecoder( | ||
proto: ProtoBuf, | ||
reader: ProtobufReader, | ||
descriptor: SerialDescriptor, | ||
) : ProtobufDecoder(proto, reader, descriptor) { | ||
private var nextIndex: Int = 0 | ||
|
||
// Tags are omitted in the packed array format | ||
override fun SerialDescriptor.getTag(index: Int): ProtoDesc = MISSING_TAG | ||
|
||
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder { | ||
throw SerializationException("Packing only supports primitive number types. The input type however was a struct: $descriptor") | ||
} | ||
|
||
override fun decodeElementIndex(descriptor: SerialDescriptor): Int { | ||
// We need eof here as there is no tag to read in packed form. | ||
if (reader.eof) return CompositeDecoder.DECODE_DONE | ||
return nextIndex++ | ||
} | ||
|
||
override fun decodeTaggedString(tag: ProtoDesc): String { | ||
throw SerializationException("Packing only supports primitive number types. The actual reading is for string.") | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/PackedArrayEncoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package kotlinx.serialization.protobuf.internal | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.protobuf.* | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
internal class PackedArrayEncoder( | ||
proto: ProtoBuf, | ||
writer: ProtobufWriter, | ||
curTag: ProtoDesc, | ||
descriptor: SerialDescriptor, | ||
stream: ByteArrayOutput = ByteArrayOutput() | ||
) : NestedRepeatedEncoder(proto, writer, curTag, descriptor, stream) { | ||
|
||
// Triggers not writing header | ||
override fun SerialDescriptor.getTag(index: Int): ProtoDesc = MISSING_TAG | ||
|
||
override fun beginCollection(descriptor: SerialDescriptor, collectionSize: Int): CompositeEncoder { | ||
throw SerializationException("Packing only supports primitive number types") | ||
} | ||
|
||
override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { | ||
throw SerializationException("Packing only supports primitive number types") | ||
} | ||
|
||
override fun encodeTaggedString(tag: ProtoDesc, value: String) { | ||
throw SerializationException("Packing only supports primitive number types") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.