Skip to content

Commit

Permalink
fix: stop encoding synthetic fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Jusabe Guedes authored and dojusa committed Jul 19, 2023
1 parent a339c96 commit 819157e
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
7 changes: 6 additions & 1 deletion encoding/protobq/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (o SchemaOptions) InferMessageSchema(msg protoreflect.MessageDescriptor) bi
if o.UseOneofFields {
for i := 0; i < msg.Oneofs().Len(); i++ {
oneof := msg.Oneofs().Get(i)
// The `optional` keyword creates a synthetic field treated as an oneof
// which creates a new field with _ as prefix missmatching the schema creation
if oneof.IsSynthetic() {
continue
}
schema = append(schema, o.inferOneofFieldSchema(oneof))
}
}
Expand Down Expand Up @@ -220,7 +225,7 @@ func (o SchemaOptions) inferFieldSchemaType(field protoreflect.FieldDescriptor)
return bigquery.RecordFieldType
}

func (o SchemaOptions) inferEnumFieldType(field protoreflect.FieldDescriptor) bigquery.FieldType {
func (o SchemaOptions) inferEnumFieldType(_ protoreflect.FieldDescriptor) bigquery.FieldType {
if o.UseEnumNumbers {
return bigquery.IntegerFieldType
}
Expand Down
14 changes: 14 additions & 0 deletions encoding/protobq/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,20 @@ func TestSchemaOptions_InferSchema(t *testing.T) {
},
},
},
{
name: "examplev1.ExampleOptional (with enable `UseOneofFields`)",
opt: SchemaOptions{
UseOneofFields: true,
},
msg: &examplev1.ExampleOptional{},
expected: bigquery.Schema{
{
Name: "opt",
Type: bigquery.FloatFieldType,
Required: false,
},
},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
syntax = "proto3";

package einride.bigquery.example.v1;

message ExampleOptional {
optional double opt = 1;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name": "opt",
"type": "FLOAT",
"mode": "NULLABLE"
}
]

0 comments on commit 819157e

Please sign in to comment.