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

fix #368: respect synthetic oneof in marshaller #369

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 6 additions & 2 deletions encoding/protobq/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ func (o MarshalOptions) marshalMessage(msg protoreflect.Message) (map[string]big
if o.Schema.UseOneofFields {
for i := 0; i < msg.Descriptor().Oneofs().Len(); i++ {
oneofDescriptor := msg.Descriptor().Oneofs().Get(i)
if oneofDescriptor.IsSynthetic() {
continue
}
oneofField := msg.WhichOneof(oneofDescriptor)
if oneofField != nil {
result[string(oneofDescriptor.Name())] = string(oneofField.Name())
if oneofField == nil {
continue
}
result[string(oneofDescriptor.Name())] = string(oneofField.Name())
}
}
return result, nil
Expand Down
34 changes: 34 additions & 0 deletions encoding/protobq/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,40 @@ func TestMarshalOptions_Marshal(t *testing.T) {
opt: MarshalOptions{Schema: SchemaOptions{UseOneofFields: true}},
expected: map[string]bigquery.Value{},
},

{
name: "optional oneof and optional message",
msg: &examplev1.ExampleOptional{
OptMessage_2: &examplev1.ExampleOptMessage{
StringValue: "opt_message_string_value",
},
OptOneofMessage_3: &examplev1.ExampleOptOneof{
OneofFields_1: &examplev1.ExampleOptOneof_OneofMessage_2{
OneofMessage_2: &examplev1.ExampleOptOneof_Message{
StringValue: "opt_one_of_message_string_value",
},
},
},
},
opt: MarshalOptions{Schema: SchemaOptions{UseOneofFields: true}},
expected: map[string]bigquery.Value{
"opt_message_2": map[string]bigquery.Value{"string_value": string("opt_message_string_value")},
"opt_oneof_message_3": map[string]bigquery.Value{
"oneof_fields_1": string("oneof_message_2"),
"oneof_message_2": map[string]bigquery.Value{"string_value": string("opt_one_of_message_string_value")},
},
},
},

{
name: "optional empty oneof and optional empty message",
msg: &examplev1.ExampleOptional{
OptMessage_2: nil,
OptOneofMessage_3: nil,
},
opt: MarshalOptions{Schema: SchemaOptions{UseOneofFields: true}},
expected: map[string]bigquery.Value{},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down
24 changes: 23 additions & 1 deletion encoding/protobq/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,32 @@ func TestSchemaOptions_InferSchema(t *testing.T) {
msg: &examplev1.ExampleOptional{},
expected: bigquery.Schema{
{
Name: "opt",
Name: "opt_double_1",
Type: bigquery.FloatFieldType,
Required: false,
},
{
Name: "opt_message_2",
Type: "RECORD",
Schema: bigquery.Schema{{Name: "string_value", Type: bigquery.StringFieldType}},
},
{
Name: "opt_oneof_message_3",
Type: bigquery.RecordFieldType,
Required: false,
Schema: bigquery.Schema{
{
Name: "oneof_message_2",
Type: bigquery.RecordFieldType,
Schema: bigquery.Schema{{Name: "string_value", Type: bigquery.StringFieldType}},
},
{
Name: "oneof_fields_1",
Description: "One of: oneof_empty_message_1, oneof_message_2.",
Type: bigquery.StringFieldType,
},
},
},
},
},
} {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@ syntax = "proto3";
package einride.bigquery.example.v1;

message ExampleOptional {
optional double opt = 1;
optional double opt_double_1 = 1;
optional ExampleOptMessage opt_message_2 = 2;
optional ExampleOptOneof opt_oneof_message_3 = 3;
}

message ExampleOptMessage {
string string_value = 1;
}

message ExampleOptOneof {
oneof oneof_fields_1 {
EmptyMessage oneof_empty_message_1 = 1;
Message oneof_message_2 = 2;
}
message EmptyMessage {}
message Message {
string string_value = 1;
}
}
Loading