-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compile error when proto contains a oneof field with same name as the enclosing message #47
Comments
@garyp - Any update ? :) |
@sujithkris Haven't had a chance to fix this yet. Should be pretty straightforward, if you happen to be interested in looking into it. I'm guessing the fix would be to change the code in pbandk's |
Hi, went overt he code real quick but could you perhaps point out to a test that could be used to validate the fix? |
I would suggest to generate something like syntax = "proto3";
package example;
message Person {
oneof person {
Foo foo = 1;
}
}
message Foo {
} into package tutorial
data class Person(
val person: Person<*>? = null,
override val unknownFields: Map<Int, pbandk.UnknownField> = emptyMap()
) : pbandk.Message {
sealed class Person<V>(value: V) : pbandk.Message.OneOf<V>(value) {
class Foo(foo: tutorial.Foo) : Person<tutorial.Foo>(foo)
}
val foo: tutorial.Foo?
get() = (person as? Person.Foo)?.value
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<tutorial.Person> {
val defaultInstance by lazy { Person() }
override fun decodeWith(u: pbandk.MessageDecoder) = decodeWithImpl(u)
override val descriptor: pbandk.MessageDescriptor<tutorial.Person> by lazy {
pbandk.MessageDescriptor(
messageClass = tutorial.Person::class,
messageCompanion = this,
fields = listOf(
pbandk.FieldDescriptor(
messageDescriptor = this::descriptor,
name = "foo",
number = 99,
type = pbandk.FieldDescriptor.Type.Message(messageCompanion = tutorial.Foo.Companion),
oneofMember = true,
jsonName = "foo",
value = tutorial.Person::foo
)
)
)
}
}
}
this is the diff: 20c20
< companion object : pbandk.Message.Companion<tutorial.Person> {
---
> companion object : pbandk.Message.Companion<Person> {
22c22
< override fun decodeWith(u: pbandk.MessageDecoder) = decodeWithImpl(u)
---
> override fun decodeWith(u: pbandk.MessageDecoder) = Person.decodeWithImpl(u)
24c24
< override val descriptor: pbandk.MessageDescriptor<tutorial.Person> by lazy {
---
> override val descriptor: pbandk.MessageDescriptor<Person> by lazy {
26c26
< messageClass = tutorial.Person::class,
---
> messageClass = Person::class,
36c36
< value = tutorial.Person::foo
---
> value = Person::foo
|
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> { ... } ... } ```
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> { ... } ... } ```
From #18 (comment):
A proto definition such as:
Generates code like:
Which fails to compile because there are two types named
Value
and the type ofvalue
inval value: Value<*>? = null
can't be resolved correctly.The text was updated successfully, but these errors were encountered: