@@ -8,63 +8,38 @@ import kotlinx.serialization.protobuf.ProtoIntegerType as KxProtoIntegerType
88import kotlinx.serialization.protobuf.ProtoType as KxProtoType
99
1010/* *
11- * Specifies the integer encoding strategy for `Int` and `Long` fields in [PackedFormat].
11+ * Integer encoding strategy for `Int` and `Long` fields in [PackedFormat].
1212 *
13- * Mirrors [kotlinx.serialization.protobuf.ProtoIntegerType].
13+ * Compatible with [kotlinx.serialization.protobuf.ProtoIntegerType] as a fallback (see [PackedType]) .
1414 */
15- enum class PackedIntegerType {
15+ enum class IntPacking {
1616 /* * Unsigned variable-length (LEB128). Compact for small non-negative values. */
17- DEFAULT ,
17+ VARINT ,
1818 /* * ZigZag variable-length. Compact for small signed values. */
19- SIGNED ,
19+ ZIGZAG ,
2020 /* * Fixed-width. 4 bytes for `Int`, 8 bytes for `Long`. */
2121 FIXED
2222}
2323
2424/* *
25- * Marks an `Int` or `Long` field with an explicit encoding strategy in [PackedFormat].
26- *
27- * Mirrors [kotlinx.serialization.protobuf.ProtoType]:
28- * - [PackedIntegerType.DEFAULT]: unsigned variable-length (LEB128)
29- * - [PackedIntegerType.SIGNED]: ZigZag variable-length
30- * - [PackedIntegerType.FIXED]: fixed-width (4 / 8 bytes)
31- *
32- * Usage:
33- * ```
34- * @Serializable
35- * data class Example(
36- * @PackedType(PackedIntegerType.DEFAULT) val id: Long, // compact for small positive values
37- * @PackedType(PackedIntegerType.SIGNED) val delta: Int, // compact for small signed values
38- * @PackedType(PackedIntegerType.FIXED) val hash: Int // always 4 bytes
39- * )
40- * ```
41- *
42- * Takes precedence over [kotlinx.serialization.protobuf.ProtoType] when both are present.
43- * Has no effect for types outside [PackedFormat].
25+ * Overrides the integer encoding strategy for an `Int` or `Long` field in [PackedFormat].
26+ * Falls back to [kotlinx.serialization.protobuf.ProtoType] if present, then [PackedConfiguration.defaultEncoding].
4427 */
4528@SerialInfo
4629@Target(AnnotationTarget .PROPERTY )
4730@Retention(AnnotationRetention .SOURCE )
48- annotation class PackedType (val type : PackedIntegerType )
49-
50- internal enum class IntEncoding { FIXED , VARINT , ZIGZAG }
51-
52- private fun PackedIntegerType.toIntEncoding (): IntEncoding = when (this ) {
53- PackedIntegerType .DEFAULT -> IntEncoding .VARINT
54- PackedIntegerType .SIGNED -> IntEncoding .ZIGZAG
55- PackedIntegerType .FIXED -> IntEncoding .FIXED
56- }
31+ annotation class PackedType (val type : IntPacking )
5732
58- private fun KxProtoIntegerType.toIntEncoding (): IntEncoding = when (this ) {
59- KxProtoIntegerType .DEFAULT -> IntEncoding .VARINT
60- KxProtoIntegerType .SIGNED -> IntEncoding .ZIGZAG
61- KxProtoIntegerType .FIXED -> IntEncoding .FIXED
33+ private fun KxProtoIntegerType.toIntPacking (): IntPacking = when (this ) {
34+ KxProtoIntegerType .DEFAULT -> IntPacking .VARINT
35+ KxProtoIntegerType .SIGNED -> IntPacking .ZIGZAG
36+ KxProtoIntegerType .FIXED -> IntPacking .FIXED
6237}
6338
64- internal fun resolveIntEncoding (anns : List <Annotation >, config : PackedConfiguration ): IntEncoding {
65- anns.filterIsInstance<PackedType >().firstOrNull()?.let { return it.type.toIntEncoding() }
39+ internal fun resolveIntEncoding (anns : List <Annotation >, config : PackedConfiguration ): IntPacking {
40+ anns.filterIsInstance<PackedType >().firstOrNull()?.let { return it.type }
6641 try {
67- anns.filterIsInstance<KxProtoType >().firstOrNull()?.let { return it.type.toIntEncoding () }
42+ anns.filterIsInstance<KxProtoType >().firstOrNull()?.let { return it.type.toIntPacking () }
6843 } catch (_: Throwable ) { }
69- return config.defaultEncoding.toIntEncoding()
44+ return config.defaultEncoding
7045}
0 commit comments