-
Notifications
You must be signed in to change notification settings - Fork 636
/
Copy pathexample-formats-08.kt
36 lines (30 loc) · 1.26 KB
/
example-formats-08.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// This file was automatically generated from formats.md by Knit tool. Do not edit.
package example.exampleFormats08
import kotlinx.serialization.*
import kotlinx.serialization.protobuf.*
// The outer class
@OptIn(ExperimentalSerializationApi::class)
@Serializable
data class Data(
@ProtoNumber(1) val name: String,
@ProtoOneOf val phone: IPhoneType?,
)
// The oneof interface
@Serializable sealed interface IPhoneType
// Message holder for home_phone
@OptIn(ExperimentalSerializationApi::class)
@Serializable @JvmInline value class HomePhone(@ProtoNumber(2) val number: String): IPhoneType
// Message holder for work_phone. Can also be a value class, but we leave it as `data` to demonstrate that both variants can be used.
@OptIn(ExperimentalSerializationApi::class)
@Serializable data class WorkPhone(@ProtoNumber(3) val number: String): IPhoneType
@OptIn(ExperimentalSerializationApi::class)
fun main() {
val dataTom = Data("Tom", HomePhone("123"))
val stringTom = ProtoBuf.encodeToHexString(dataTom)
val dataJerry = Data("Jerry", WorkPhone("789"))
val stringJerry = ProtoBuf.encodeToHexString(dataJerry)
println(stringTom)
println(stringJerry)
println(ProtoBuf.decodeFromHexString<Data>(stringTom))
println(ProtoBuf.decodeFromHexString<Data>(stringJerry))
}