Skip to content
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

Report an error is multiple fields for the same oneof are set #43

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ func (u *unmarshaler) findField(key string, msgDesc protoreflect.MessageDescript

// Unmarshal a field, handling isList/isMap.
func (u *unmarshaler) unmarshalField(node *yaml.Node, field protoreflect.FieldDescriptor, message proto.Message) {
if oneofDesc := field.ContainingOneof(); oneofDesc != nil {
// Check if another field in the oneof is already set.
if whichOne := message.ProtoReflect().WhichOneof(oneofDesc); whichOne != nil {
u.addErrorf(node, fmt.Sprintf("field %v is already set for oneof %v", whichOne.Name(), oneofDesc.Name()))
return
}
}

switch {
case field.IsList():
u.unmarshalList(node, field, message.ProtoReflect().Mutable(field).List())
Expand Down
144 changes: 132 additions & 12 deletions internal/gen/proto/buf/protoyaml/test/v1/pb2.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/proto/buf/protoyaml/test/v1/pb2.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ syntax = "proto2";
package buf.protoyaml.test.v1;

message Proto2Test {
repeated Proto2TestValue values = 1;
extensions 100 to max;
}

message Proto2TestValue {
oneof oneof_value {
string oneof_string_value = 1;
int32 oneof_int32_value = 2;
}
}

extend Proto2Test {
optional string p2t_string_ext = 100;
repeated string p2t_repeated_string_ext = 101;
Expand Down
6 changes: 5 additions & 1 deletion internal/testdata/ext.proto2test.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
internal/testdata/ext.proto2test.yaml:1:1 unknown field "[undefined]", expected one of []
internal/testdata/ext.proto2test.yaml:1:1 unknown field "[undefined]", expected one of [values]
1 | '[undefined]': hi
1 | ^

internal/testdata/ext.proto2test.yaml:4:1 expected scalar, got sequence
4 | [buf.protoyaml.test.v1.p2t_repeated_string_ext, hi]: [hi]
4 | ^

internal/testdata/ext.proto2test.yaml:9:24 field oneof_string_value is already set for oneof oneof_value
9 | oneof_int32_value: 1
9 | .......................^
5 changes: 5 additions & 0 deletions internal/testdata/ext.proto2test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
'[buf.protoyaml.test.v1.p2t_string_ext]': hi
[buf.protoyaml.test.v1.p2t_repeated_string_ext]: [hi]
[buf.protoyaml.test.v1.p2t_repeated_string_ext, hi]: [hi]
values:
- oneof_string_value: hi
- oneof_int32_value: 1
- oneof_string_value: hi
oneof_int32_value: 1