Skip to content

Commit

Permalink
Add value class support to the ProtoBufSchemaGenerator (Kotlin#2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotilho committed Jan 12, 2024
1 parent c10428e commit b3f6e0f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -216,29 +216,34 @@ public object ProtoBufSchemaGenerator {
val messageDescriptor = messageType.descriptor

val fieldDescriptor = messageDescriptor.getElementDescriptor(index)
var unwrappedFieldDescriptor = fieldDescriptor
while (unwrappedFieldDescriptor.isInline) {
unwrappedFieldDescriptor = unwrappedFieldDescriptor.getElementDescriptor(0)
}

val nestedTypes: List<TypeDefinition>
val typeName: String = when {
messageDescriptor.isSealedPolymorphic && index == 1 -> {
appendLine(" // decoded as message with one of these types:")
nestedTypes = fieldDescriptor.elementDescriptors.map { TypeDefinition(it) }.toList()
nestedTypes = unwrappedFieldDescriptor.elementDescriptors.map { TypeDefinition(it) }.toList()
nestedTypes.forEachIndexed { _, childType ->
append(" // message ").append(childType.descriptor.messageOrEnumName).append(", serial name '")
.append(removeLineBreaks(childType.descriptor.serialName)).appendLine('\'')
}
fieldDescriptor.scalarTypeName()
unwrappedFieldDescriptor.scalarTypeName()
}
fieldDescriptor.isProtobufScalar -> {
unwrappedFieldDescriptor.isProtobufScalar -> {
nestedTypes = emptyList()
fieldDescriptor.scalarTypeName(messageDescriptor.getElementAnnotations(index))
unwrappedFieldDescriptor.scalarTypeName(messageDescriptor.getElementAnnotations(index))
}
fieldDescriptor.isOpenPolymorphic -> {
unwrappedFieldDescriptor.isOpenPolymorphic -> {
nestedTypes = listOf(SyntheticPolymorphicType)
SyntheticPolymorphicType.descriptor.serialName
}
else -> {
// enum or regular message
nestedTypes = listOf(TypeDefinition(fieldDescriptor))
fieldDescriptor.messageOrEnumName
nestedTypes = listOf(TypeDefinition(unwrappedFieldDescriptor))
unwrappedFieldDescriptor.messageOrEnumName
}
}

Expand Down
18 changes: 15 additions & 3 deletions formats/protobuf/jvmTest/resources/OptionalClass.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ package kotlinx.serialization.protobuf.schema.generator;
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.OptionalClass'
message OptionalClass {
required int32 requiredInt = 1;
required int32 requiredUInt = 2;
required int32 requiredWrappedUInt = 3;
// WARNING: a default value decoded when value is missing
optional int32 optionalInt = 2;
optional int32 nullableInt = 3;
optional int32 optionalInt = 4;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalInt = 4;
optional int32 optionalUInt = 5;
// WARNING: a default value decoded when value is missing
optional int32 optionalWrappedUInt = 6;
optional int32 nullableInt = 7;
optional int32 nullableUInt = 8;
optional int32 nullableWrappedUInt = 9;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalInt = 10;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalUInt = 11;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalWrappedUInt = 12;
}
18 changes: 15 additions & 3 deletions formats/protobuf/jvmTest/resources/common/schema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,23 @@ message MapClass {
// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.OptionalClass'
message OptionalClass {
required int32 requiredInt = 1;
required int32 requiredUInt = 2;
required int32 requiredWrappedUInt = 3;
// WARNING: a default value decoded when value is missing
optional int32 optionalInt = 2;
optional int32 nullableInt = 3;
optional int32 optionalInt = 4;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalInt = 4;
optional int32 optionalUInt = 5;
// WARNING: a default value decoded when value is missing
optional int32 optionalWrappedUInt = 6;
optional int32 nullableInt = 7;
optional int32 nullableUInt = 8;
optional int32 nullableWrappedUInt = 9;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalInt = 10;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalUInt = 11;
// WARNING: a default value decoded when value is missing
optional int32 nullableOptionalWrappedUInt = 12;
}

// serial name 'kotlinx.serialization.protobuf.schema.GenerationTest.ContextualHolder'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class GenerationTest {
@ProtoNumber(5)
val b: Int,
@ProtoNumber(3)
val c: Int
val c: UInt,
)

@Serializable
Expand All @@ -84,6 +84,10 @@ class GenerationTest {
@Serializable
data class OptionsClass(val i: Int)

@JvmInline
@Serializable
value class WrappedUInt(val i : UInt)

@Serializable
class ListClass(
val intList: List<Int>,
Expand Down Expand Up @@ -113,9 +117,17 @@ class GenerationTest {
@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 nullableOptionalInt: Int? = 10
val nullableUInt: UInt?,
val nullableWrappedUInt: WrappedUInt?,
val nullableOptionalInt: Int? = 10,
val nullableOptionalUInt: UInt? = 10U,
val nullableOptionalWrappedUInt: WrappedUInt? = WrappedUInt(10U),
)

@Serializable
Expand Down

0 comments on commit b3f6e0f

Please sign in to comment.