Skip to content

Commit

Permalink
use fully qualified name when generating messages (#104)
Browse files Browse the repository at this point in the history
Fixes #47 

For the following protobuf where the `oneof` field had the same name as message, the companion object definition would shadow the `oneof` class instead of the `message` class. This patch fixes it by fully qualifying the `message` class name.

**Input:**
```protobuf
syntax = "proto3";
package foobar;

message Value {
    oneof value {
        int32 int_val = 1;
        string str_val = 2;
    }
}
```

**Before:**

```kotlin
package foobar

data class Value(
        val value: Value<*>? = null,
        override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
       sealed class Value<V>(value: V) : pbandk.Message.OneOf<V>(value) {
              ...
       }
       ...
       companion object : pbandk.Message.Companion<Value> {
              ...
       }
       ...
}
```

**After:**

```kotlin
package foobar

data class Value(
        val value: Value<*>? = null,
        override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
       sealed class Value<V>(value: V) : pbandk.Message.OneOf<V>(value) {
              ...
       }
       ...
       companion object : pbandk.Message.Companion<foobar.Value> {
              ...
       }
       ...
}
```
  • Loading branch information
nbaztec authored Dec 28, 2020
1 parent dc109f4 commit ed017e6
Show file tree
Hide file tree
Showing 23 changed files with 3,727 additions and 2,717 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ data class FailureSet(
override operator fun plus(other: pbandk.Message?) = protoMergeImpl(other)
override val descriptor get() = Companion.descriptor
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<FailureSet> {
val defaultInstance by lazy { FailureSet() }
override fun decodeWith(u: pbandk.MessageDecoder) = FailureSet.decodeWithImpl(u)
companion object : pbandk.Message.Companion<pbandk.conformance.pb.FailureSet> {
val defaultInstance by lazy { pbandk.conformance.pb.FailureSet() }
override fun decodeWith(u: pbandk.MessageDecoder) = pbandk.conformance.pb.FailureSet.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<FailureSet> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<FailureSet, *>>(1).apply {
override val descriptor: pbandk.MessageDescriptor<pbandk.conformance.pb.FailureSet> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<pbandk.conformance.pb.FailureSet, *>>(1).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "failure",
number = 1,
type = pbandk.FieldDescriptor.Type.Repeated<String>(valueType = pbandk.FieldDescriptor.Type.Primitive.String()),
jsonName = "failure",
value = FailureSet::failure
value = pbandk.conformance.pb.FailureSet::failure
)
)
}
pbandk.MessageDescriptor(
messageClass = FailureSet::class,
messageClass = pbandk.conformance.pb.FailureSet::class,
messageCompanion = this,
fields = fieldsList
)
Expand Down Expand Up @@ -102,12 +102,12 @@ data class ConformanceRequest(
override operator fun plus(other: pbandk.Message?) = protoMergeImpl(other)
override val descriptor get() = Companion.descriptor
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<ConformanceRequest> {
val defaultInstance by lazy { ConformanceRequest() }
override fun decodeWith(u: pbandk.MessageDecoder) = ConformanceRequest.decodeWithImpl(u)
companion object : pbandk.Message.Companion<pbandk.conformance.pb.ConformanceRequest> {
val defaultInstance by lazy { pbandk.conformance.pb.ConformanceRequest() }
override fun decodeWith(u: pbandk.MessageDecoder) = pbandk.conformance.pb.ConformanceRequest.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<ConformanceRequest> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<ConformanceRequest, *>>(9).apply {
override val descriptor: pbandk.MessageDescriptor<pbandk.conformance.pb.ConformanceRequest> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<pbandk.conformance.pb.ConformanceRequest, *>>(9).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
Expand All @@ -116,7 +116,7 @@ data class ConformanceRequest(
type = pbandk.FieldDescriptor.Type.Primitive.Bytes(hasPresence = true),
oneofMember = true,
jsonName = "protobufPayload",
value = ConformanceRequest::protobufPayload
value = pbandk.conformance.pb.ConformanceRequest::protobufPayload
)
)
add(
Expand All @@ -127,7 +127,7 @@ data class ConformanceRequest(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "jsonPayload",
value = ConformanceRequest::jsonPayload
value = pbandk.conformance.pb.ConformanceRequest::jsonPayload
)
)
add(
Expand All @@ -137,7 +137,7 @@ data class ConformanceRequest(
number = 3,
type = pbandk.FieldDescriptor.Type.Enum(enumCompanion = pbandk.conformance.pb.WireFormat.Companion),
jsonName = "requestedOutputFormat",
value = ConformanceRequest::requestedOutputFormat
value = pbandk.conformance.pb.ConformanceRequest::requestedOutputFormat
)
)
add(
Expand All @@ -147,7 +147,7 @@ data class ConformanceRequest(
number = 4,
type = pbandk.FieldDescriptor.Type.Primitive.String(),
jsonName = "messageType",
value = ConformanceRequest::messageType
value = pbandk.conformance.pb.ConformanceRequest::messageType
)
)
add(
Expand All @@ -157,7 +157,7 @@ data class ConformanceRequest(
number = 5,
type = pbandk.FieldDescriptor.Type.Enum(enumCompanion = pbandk.conformance.pb.TestCategory.Companion),
jsonName = "testCategory",
value = ConformanceRequest::testCategory
value = pbandk.conformance.pb.ConformanceRequest::testCategory
)
)
add(
Expand All @@ -167,7 +167,7 @@ data class ConformanceRequest(
number = 6,
type = pbandk.FieldDescriptor.Type.Message(messageCompanion = pbandk.conformance.pb.JspbEncodingConfig.Companion),
jsonName = "jspbEncodingOptions",
value = ConformanceRequest::jspbEncodingOptions
value = pbandk.conformance.pb.ConformanceRequest::jspbEncodingOptions
)
)
add(
Expand All @@ -178,7 +178,7 @@ data class ConformanceRequest(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "jspbPayload",
value = ConformanceRequest::jspbPayload
value = pbandk.conformance.pb.ConformanceRequest::jspbPayload
)
)
add(
Expand All @@ -189,7 +189,7 @@ data class ConformanceRequest(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "textPayload",
value = ConformanceRequest::textPayload
value = pbandk.conformance.pb.ConformanceRequest::textPayload
)
)
add(
Expand All @@ -199,12 +199,12 @@ data class ConformanceRequest(
number = 9,
type = pbandk.FieldDescriptor.Type.Primitive.Bool(),
jsonName = "printUnknownFields",
value = ConformanceRequest::printUnknownFields
value = pbandk.conformance.pb.ConformanceRequest::printUnknownFields
)
)
}
pbandk.MessageDescriptor(
messageClass = ConformanceRequest::class,
messageClass = pbandk.conformance.pb.ConformanceRequest::class,
messageCompanion = this,
fields = fieldsList
)
Expand Down Expand Up @@ -247,12 +247,12 @@ data class ConformanceResponse(
override operator fun plus(other: pbandk.Message?) = protoMergeImpl(other)
override val descriptor get() = Companion.descriptor
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<ConformanceResponse> {
val defaultInstance by lazy { ConformanceResponse() }
override fun decodeWith(u: pbandk.MessageDecoder) = ConformanceResponse.decodeWithImpl(u)
companion object : pbandk.Message.Companion<pbandk.conformance.pb.ConformanceResponse> {
val defaultInstance by lazy { pbandk.conformance.pb.ConformanceResponse() }
override fun decodeWith(u: pbandk.MessageDecoder) = pbandk.conformance.pb.ConformanceResponse.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<ConformanceResponse> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<ConformanceResponse, *>>(8).apply {
override val descriptor: pbandk.MessageDescriptor<pbandk.conformance.pb.ConformanceResponse> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<pbandk.conformance.pb.ConformanceResponse, *>>(8).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
Expand All @@ -261,7 +261,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "parseError",
value = ConformanceResponse::parseError
value = pbandk.conformance.pb.ConformanceResponse::parseError
)
)
add(
Expand All @@ -272,7 +272,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "runtimeError",
value = ConformanceResponse::runtimeError
value = pbandk.conformance.pb.ConformanceResponse::runtimeError
)
)
add(
Expand All @@ -283,7 +283,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.Bytes(hasPresence = true),
oneofMember = true,
jsonName = "protobufPayload",
value = ConformanceResponse::protobufPayload
value = pbandk.conformance.pb.ConformanceResponse::protobufPayload
)
)
add(
Expand All @@ -294,7 +294,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "jsonPayload",
value = ConformanceResponse::jsonPayload
value = pbandk.conformance.pb.ConformanceResponse::jsonPayload
)
)
add(
Expand All @@ -305,7 +305,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "skipped",
value = ConformanceResponse::skipped
value = pbandk.conformance.pb.ConformanceResponse::skipped
)
)
add(
Expand All @@ -316,7 +316,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "serializeError",
value = ConformanceResponse::serializeError
value = pbandk.conformance.pb.ConformanceResponse::serializeError
)
)
add(
Expand All @@ -327,7 +327,7 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "jspbPayload",
value = ConformanceResponse::jspbPayload
value = pbandk.conformance.pb.ConformanceResponse::jspbPayload
)
)
add(
Expand All @@ -338,12 +338,12 @@ data class ConformanceResponse(
type = pbandk.FieldDescriptor.Type.Primitive.String(hasPresence = true),
oneofMember = true,
jsonName = "textPayload",
value = ConformanceResponse::textPayload
value = pbandk.conformance.pb.ConformanceResponse::textPayload
)
)
}
pbandk.MessageDescriptor(
messageClass = ConformanceResponse::class,
messageClass = pbandk.conformance.pb.ConformanceResponse::class,
messageCompanion = this,
fields = fieldsList
)
Expand All @@ -358,25 +358,25 @@ data class JspbEncodingConfig(
override operator fun plus(other: pbandk.Message?) = protoMergeImpl(other)
override val descriptor get() = Companion.descriptor
override val protoSize by lazy { super.protoSize }
companion object : pbandk.Message.Companion<JspbEncodingConfig> {
val defaultInstance by lazy { JspbEncodingConfig() }
override fun decodeWith(u: pbandk.MessageDecoder) = JspbEncodingConfig.decodeWithImpl(u)
companion object : pbandk.Message.Companion<pbandk.conformance.pb.JspbEncodingConfig> {
val defaultInstance by lazy { pbandk.conformance.pb.JspbEncodingConfig() }
override fun decodeWith(u: pbandk.MessageDecoder) = pbandk.conformance.pb.JspbEncodingConfig.decodeWithImpl(u)

override val descriptor: pbandk.MessageDescriptor<JspbEncodingConfig> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<JspbEncodingConfig, *>>(1).apply {
override val descriptor: pbandk.MessageDescriptor<pbandk.conformance.pb.JspbEncodingConfig> by lazy {
val fieldsList = ArrayList<pbandk.FieldDescriptor<pbandk.conformance.pb.JspbEncodingConfig, *>>(1).apply {
add(
pbandk.FieldDescriptor(
messageDescriptor = this@Companion::descriptor,
name = "use_jspb_array_any_format",
number = 1,
type = pbandk.FieldDescriptor.Type.Primitive.Bool(),
jsonName = "useJspbArrayAnyFormat",
value = JspbEncodingConfig::useJspbArrayAnyFormat
value = pbandk.conformance.pb.JspbEncodingConfig::useJspbArrayAnyFormat
)
)
}
pbandk.MessageDescriptor(
messageClass = JspbEncodingConfig::class,
messageClass = pbandk.conformance.pb.JspbEncodingConfig::class,
messageCompanion = this,
fields = fieldsList
)
Expand Down
Loading

0 comments on commit ed017e6

Please sign in to comment.