Skip to content

Commit 8661255

Browse files
committed
encoding/protobuf: fix trailing comment in oneof bug
Fixes #586 Change-Id: I8281e90946e6de92bbf2f5819abcb48259128f48 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7661 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 7463d11 commit 8661255

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

encoding/protobuf/parse.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,21 +676,30 @@ func (p *protoConverter) oneOf(x *proto.Oneof) {
676676

677677
p.addDecl(embed)
678678

679-
for _, v := range x.Elements {
680-
s := &ast.StructLit{
679+
newStruct := func() {
680+
s = &ast.StructLit{
681681
// TODO: make this the default in the formatter.
682682
Rbrace: token.Newline.Pos(),
683683
}
684+
embed.Expr = ast.NewBinExpr(token.OR, embed.Expr, s)
685+
}
686+
for _, v := range x.Elements {
684687
switch x := v.(type) {
685688
case *proto.OneOfField:
689+
newStruct()
686690
oneOf := p.parseField(s, 0, x.Field)
687691
oneOf.Optional = token.NoPos
688692

693+
case *proto.Comment:
694+
cg := comment(x, false)
695+
ast.SetRelPos(cg, token.NewSection)
696+
s.Elts = append(s.Elts, cg)
697+
689698
default:
699+
newStruct()
690700
p.messageField(s, 1, v)
691701
}
692702

693-
embed.Expr = ast.NewBinExpr(token.OR, embed.Expr, s)
694703
}
695704
}
696705

encoding/protobuf/protobuf_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestExtractDefinitions(t *testing.T) {
3838
"networking/v1alpha3/gateway.proto",
3939
"mixer/v1/attributes.proto",
4040
"mixer/v1/config/client/client_config.proto",
41+
"other/trailcomment.proto",
4142
}
4243
for _, file := range testCases {
4344
t.Run(file, func(t *testing.T) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Issue #586
2+
package foo
3+
4+
message Bar {
5+
oneof Foo {
6+
string a = 1;
7+
// hello world
8+
9+
string b = 2;
10+
// hello world
11+
}
12+
13+
int32 c = 3;
14+
// hello world
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Issue #586
2+
package foo
3+
4+
#Bar: {
5+
{} | {
6+
a: string @protobuf(1)
7+
8+
// hello world
9+
10+
} | {
11+
b: string @protobuf(2)
12+
13+
// hello world
14+
15+
}
16+
c?: int32 @protobuf(3)
17+
18+
// hello world
19+
20+
}

0 commit comments

Comments
 (0)