Cannot add nested message and a field with same name using `builder`
tanner-bruce opened this issue · 2 comments
For use with the BigQuery Write API they require all message descriptors to be nested within a single descriptor proto. I am building up this descriptor proto, wanting to end up with something like
message A {
message B {
message C {}
C c;
}
B b;
}
Because of the addSymbol
maintaining a map that includes the symbol name for both the nested message descriptor as well as the field, this results in a error from AddField
.
This is not a big deal since we are simply able to append a well known string to the message descriptor name which removes the "duplicate", but it did take a little bit to figure that out since it's not obvious, as this should technically work.
a := builder.NewMessage("a")
builder.AddNestedMessage(a)
builder.AddField(builder.NewField("a"), builder.FieldTypeMessage(a))
Your code example at the bottom does not match the example schema at the top. The reason you are running into errors is because your code example uses lower-case for both -- a message named "a" and a field named "a". But the schema at the top has the message types capitalized -- so a message named "A" and a field named "a". If you change the case of the message, it should work.
-a := builder.NewMessage("a")
+a := builder.NewMessage("A")
builder.AddNestedMessage(a)
builder.AddField(builder.NewField("a"), builder.FieldTypeMessage(a))
It is simply not allowed to have conflicting names in the same scope. So we will not make any changes to try to support both being named "a". You'll find that compilers like protoc
and buf
also complain if you tried to actually write a proto source file like that, because it is not a valid Protobuf schema.
// test.proto
syntax = "proto3";
message Message {
message a {
}
a a = 1;
}
$ buf build
test.proto:6:6:symbol "Message.a" already defined at test.proto:4:12
$ protoc test.proto -o /dev/null
test.proto:4:12: "a" is already defined in "Message".
test.proto:6:4: "a" is not defined.
Well, that'd do it. Appreciate the quick response!