commonMain.pro.felixo.protobuf.serialization.generation.internal.FieldEncodings.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protobuf-kotlin-serialization Show documentation
Show all versions of protobuf-kotlin-serialization Show documentation
Protocol Buffers 3 support for Kotlin Multiplatform
The newest version!
package pro.felixo.protobuf.serialization.generation.internal
import kotlinx.serialization.descriptors.PrimitiveKind
import pro.felixo.protobuf.serialization.IntegerType
import pro.felixo.protobuf.serialization.ProtoIntegerType
import pro.felixo.protobuf.serialization.encoding.FieldEncoding
fun scalar(annotations: List, kind: PrimitiveKind): FieldEncoding =
when (kind) {
PrimitiveKind.BOOLEAN -> FieldEncoding.Bool
PrimitiveKind.BYTE -> int32Type(annotations)
PrimitiveKind.CHAR -> int32Type(annotations)
PrimitiveKind.DOUBLE -> FieldEncoding.Double
PrimitiveKind.FLOAT -> FieldEncoding.Float
PrimitiveKind.INT -> int32Type(annotations)
PrimitiveKind.LONG -> int64Type(annotations)
PrimitiveKind.SHORT -> int32Type(annotations)
PrimitiveKind.STRING -> FieldEncoding.String
}
fun int32Type(annotations: List): FieldEncoding.Integer32 = when (integerType(annotations)) {
IntegerType.Default -> FieldEncoding.Int32
IntegerType.Unsigned -> FieldEncoding.UInt32
IntegerType.Signed -> FieldEncoding.SInt32
IntegerType.Fixed -> FieldEncoding.Fixed32
IntegerType.SignedFixed -> FieldEncoding.SFixed32
}
fun int64Type(annotations: List): FieldEncoding.Integer64 = when (integerType(annotations)) {
IntegerType.Default -> FieldEncoding.Int64
IntegerType.Unsigned -> FieldEncoding.UInt64
IntegerType.Signed -> FieldEncoding.SInt64
IntegerType.Fixed -> FieldEncoding.Fixed64
IntegerType.SignedFixed -> FieldEncoding.SFixed64
}
fun integerType(annotations: List): IntegerType =
annotations.filterIsInstance().firstOrNull()?.type ?: IntegerType.Default