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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
package kotlinx.serialization.protobuf.schema
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.protobuf.ProtoIntegerType
import kotlinx.serialization.protobuf.*
import kotlin.reflect.KClass
import kotlin.test.Test
import kotlin.test.assertEquals
internal const val TARGET_PACKAGE = "kotlinx.serialization.protobuf.schema.generator"
internal const val COMMON_SCHEMA_FILE_NAME = "common/schema.proto"
internal val commonClasses = listOf(
GenerationTest.ScalarHolder::class,
GenerationTest.FieldNumberClass::class,
GenerationTest.SerialNameClass::class,
GenerationTest.ListClass::class,
GenerationTest.PackedListClass::class,
GenerationTest.MapClass::class,
GenerationTest.OptionalClass::class,
GenerationTest.ContextualHolder::class,
GenerationTest.AbstractHolder::class,
GenerationTest.SealedHolder::class,
GenerationTest.NestedCollections::class,
GenerationTest.LegacyMapHolder::class,
GenerationTest.NullableNestedCollections::class,
GenerationTest.OptionalCollections::class,
GenerationTest.EnumWithProtoNumber::class,
)
class GenerationTest {
@Serializable
class ScalarHolder(
val int: Int,
@ProtoType(ProtoIntegerType.SIGNED)
val intSigned: Int,
@ProtoType(ProtoIntegerType.FIXED)
val intFixed: Int,
@ProtoType(ProtoIntegerType.DEFAULT)
val intDefault: Int,
val long: Long,
@ProtoType(ProtoIntegerType.SIGNED)
val longSigned: Long,
@ProtoType(ProtoIntegerType.FIXED)
val longFixed: Long,
@ProtoType(ProtoIntegerType.DEFAULT)
val longDefault: Int,
val flag: Boolean,
val byteArray: ByteArray,
val boxedByteArray: Array<Byte?>,
val text: String,
val float: Float,
val double: Double
)
@Serializable
class FieldNumberClass(
val a: Int,
@ProtoNumber(5)
val b: Int,
@ProtoNumber(3)
val c: UInt,
)
@Serializable
@SerialName("OverriddenClassName")
class SerialNameClass(
val original: Int,
@SerialName("OverriddenFieldName")
val b: SerialNameEnum
)
@Serializable
@SerialName("OverriddenEnumName")
enum class SerialNameEnum {
FIRST,
@SerialName("OverriddenElementName")
SECOND
}
@Serializable
data class OptionsClass(val i: Int)
@JvmInline
@Serializable
value class WrappedUInt(val i : UInt)
@Serializable
class ListClass(
val intList: List<Int>,
val intArray: IntArray,
val boxedIntArray: Array<Int?>,
val messageList: List<OptionsClass>,
val enumList: List<SerialNameEnum>
)
@Serializable
class PackedListClass(
@ProtoPacked val intList: List<Int>,
@ProtoPacked val intArray: IntArray,
@ProtoPacked val boxedIntArray: Array<Int?>,
val messageList: List<OptionsClass>,
val enumList: List<SerialNameEnum>
)
@Serializable
class MapClass(
val scalarMap: Map<Int, Float>,
val bytesMap: Map<Int, List<Byte>>,
val messageMap: Map<String, OptionsClass>,
val enumMap: Map<Boolean, SerialNameEnum>
)
@Serializable
data class OptionalClass(
val requiredInt: Int,
val requiredUInt: UInt,
val requiredWrappedUInt: WrappedUInt,
val optionalInt: Int = 5,
val optionalUInt: UInt = 5U,
val optionalWrappedUInt: WrappedUInt = WrappedUInt(5U),
val nullableInt: Int?,
val nullableUInt: UInt?,
val nullableWrappedUInt: WrappedUInt?,
val nullableOptionalInt: Int? = 10,
val nullableOptionalUInt: UInt? = 10U,
val nullableOptionalWrappedUInt: WrappedUInt? = WrappedUInt(10U),
)
@Serializable
data class OptionalCollections(
val requiredList: List<Int>,
val optionalList: List<Int> = listOf(42),
val nullableList: List<Int>?,
val nullableOptionalList: List<Int>? = listOf(42),
val requiredMap: Map<Int, Int>,
val optionalMap: Map<Int, Int> = mapOf(42 to 42),
val nullableMap: Map<Int, Int>?,
val nullableOptionalMap: Map<Int, Int>? = mapOf(42 to 42)
)
@Serializable
data class ContextualHolder(
@Contextual val value: Int
)
@Serializable
abstract class AbstractClass(val int: Int)
@Serializable
data class AbstractHolder(@Polymorphic val abs: AbstractClass)
@Serializable
sealed class SealedClass {
@Serializable
data class Impl1(val int: Int) : SealedClass()
@Serializable
data class Impl2(val long: Long) : SealedClass()
}
@Serializable
data class SealedHolder(val sealed: SealedClass)
@Serializable
class NestedCollections(
val intList: List<List<Int>>,
val messageList: List<List<OptionsClass>>,
val mapInList: List<Map<String, OptionsClass>>,
val listInMap: Map<String, List<Int>>
)
@Serializable
class LegacyMapHolder(
val keyAsMessage: Map<OptionsClass, Int>,
val keyAsEnum: Map<SerialNameEnum, OptionsClass>,
val keyAsBytes: Map<List<Byte>, List<Byte>>,
val keyAsList: Map<List<Int>, List<Byte>>,
val keyAsDeepList: Map<List<List<Int>>, List<Byte>>,
val nullableKeyAndValue: Map<OptionsClass?, OptionsClass?>
)
@Serializable
class NullableNestedCollections(
val nullableIntList: List<List<Int>?>,
val nullableIntMap: Map<String, List<Int>?>,
val intMap: Map<String, List<Int?>>,
val intList: List<List<Int?>>,
val legacyMap: Map<List<Int>?, List<Int>?>
)
@Serializable
enum class EnumWithProtoNumber {
ZERO,
@ProtoNumber(3)
THREE,
TWO,
@ProtoNumber(5)
FIVE,
}
@Test
fun testIndividuals() {
assertSchemaForClass(OptionsClass::class, mapOf("java_package" to "api.proto", "java_outer_classname" to "Outer"))
commonClasses.forEach {
assertSchemaForClass(it)
}
}
@Test
fun testCommon() {
assertSchema(COMMON_SCHEMA_FILE_NAME, commonClasses.map { it.serializer().descriptor }.toList())
}
private fun assertSchemaForClass(
clazz: KClass<*>,
options: Map<String, String> = emptyMap()
) {
assertSchema("${clazz.simpleName}.proto", listOf(clazz.serializer().descriptor), options)
}
private fun assertSchema(
fileName: String,
descriptors: List<SerialDescriptor>,
options: Map<String, String> = emptyMap()
) {
val schema = this::class.java.getResourceAsStream("/$fileName")
.readBytes().toString(Charsets.UTF_8)
.replace("\r\n", "\n") // fixme when compiled on windows, the file contains line \r\n breaks
assertEquals(schema, ProtoBufSchemaGenerator.generateSchemaText(descriptors, TARGET_PACKAGE, options))
}
}
|