From 819157ecec9dee98316a06c5cf135c9560862d29 Mon Sep 17 00:00:00 2001 From: Jusabe Guedes Date: Tue, 18 Jul 2023 10:57:42 +0200 Subject: [PATCH] fix: stop encoding synthetic fields --- encoding/protobq/schema.go | 7 +- encoding/protobq/schema_test.go | 14 ++ .../example/v1/example_optional.proto | 7 + .../example/v1/example_optional.pb.go | 165 ++++++++++++++++++ ...e_bigquery_example_v1_exampleoptional.json | 7 + 5 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 internal/examples/proto/einride/bigquery/example/v1/example_optional.proto create mode 100644 internal/examples/proto/gen/go/einride/bigquery/example/v1/example_optional.pb.go create mode 100644 internal/examples/proto/gen/json/einride_bigquery_example_v1_exampleoptional.json diff --git a/encoding/protobq/schema.go b/encoding/protobq/schema.go index 3c37ad0..5661a59 100644 --- a/encoding/protobq/schema.go +++ b/encoding/protobq/schema.go @@ -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)) } } @@ -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 } diff --git a/encoding/protobq/schema_test.go b/encoding/protobq/schema_test.go index 451954c..af754c7 100644 --- a/encoding/protobq/schema_test.go +++ b/encoding/protobq/schema_test.go @@ -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) { diff --git a/internal/examples/proto/einride/bigquery/example/v1/example_optional.proto b/internal/examples/proto/einride/bigquery/example/v1/example_optional.proto new file mode 100644 index 0000000..2a27178 --- /dev/null +++ b/internal/examples/proto/einride/bigquery/example/v1/example_optional.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +package einride.bigquery.example.v1; + +message ExampleOptional { + optional double opt = 1; +} diff --git a/internal/examples/proto/gen/go/einride/bigquery/example/v1/example_optional.pb.go b/internal/examples/proto/gen/go/einride/bigquery/example/v1/example_optional.pb.go new file mode 100644 index 0000000..bbab860 --- /dev/null +++ b/internal/examples/proto/gen/go/einride/bigquery/example/v1/example_optional.pb.go @@ -0,0 +1,165 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc (unknown) +// source: einride/bigquery/example/v1/example_optional.proto + +package examplev1 + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type ExampleOptional struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Opt *float64 `protobuf:"fixed64,1,opt,name=opt,proto3,oneof" json:"opt,omitempty"` +} + +func (x *ExampleOptional) Reset() { + *x = ExampleOptional{} + if protoimpl.UnsafeEnabled { + mi := &file_einride_bigquery_example_v1_example_optional_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ExampleOptional) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ExampleOptional) ProtoMessage() {} + +func (x *ExampleOptional) ProtoReflect() protoreflect.Message { + mi := &file_einride_bigquery_example_v1_example_optional_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ExampleOptional.ProtoReflect.Descriptor instead. +func (*ExampleOptional) Descriptor() ([]byte, []int) { + return file_einride_bigquery_example_v1_example_optional_proto_rawDescGZIP(), []int{0} +} + +func (x *ExampleOptional) GetOpt() float64 { + if x != nil && x.Opt != nil { + return *x.Opt + } + return 0 +} + +var File_einride_bigquery_example_v1_example_optional_proto protoreflect.FileDescriptor + +var file_einride_bigquery_example_v1_example_optional_proto_rawDesc = []byte{ + 0x0a, 0x32, 0x65, 0x69, 0x6e, 0x72, 0x69, 0x64, 0x65, 0x2f, 0x62, 0x69, 0x67, 0x71, 0x75, 0x65, + 0x72, 0x79, 0x2f, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x76, 0x31, 0x2f, 0x65, 0x78, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x5f, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x1b, 0x65, 0x69, 0x6e, 0x72, 0x69, 0x64, 0x65, 0x2e, 0x62, 0x69, + 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, + 0x31, 0x22, 0x30, 0x0a, 0x0f, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x4f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x61, 0x6c, 0x12, 0x15, 0x0a, 0x03, 0x6f, 0x70, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x01, 0x48, 0x00, 0x52, 0x03, 0x6f, 0x70, 0x74, 0x88, 0x01, 0x01, 0x42, 0x06, 0x0a, 0x04, 0x5f, + 0x6f, 0x70, 0x74, 0x42, 0xab, 0x02, 0x0a, 0x1f, 0x63, 0x6f, 0x6d, 0x2e, 0x65, 0x69, 0x6e, 0x72, + 0x69, 0x64, 0x65, 0x2e, 0x62, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x2e, 0x65, 0x78, 0x61, + 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x76, 0x31, 0x42, 0x14, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, + 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, + 0x63, 0x67, 0x6f, 0x2e, 0x65, 0x69, 0x6e, 0x72, 0x69, 0x64, 0x65, 0x2e, 0x74, 0x65, 0x63, 0x68, + 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2d, 0x62, 0x69, 0x67, 0x71, 0x75, 0x65, + 0x72, 0x79, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x65, 0x78, 0x61, 0x6d, + 0x70, 0x6c, 0x65, 0x73, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x65, + 0x69, 0x6e, 0x72, 0x69, 0x64, 0x65, 0x2f, 0x62, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x2f, + 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x76, 0x31, 0x3b, 0x65, 0x78, 0x61, 0x6d, 0x70, + 0x6c, 0x65, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x45, 0x42, 0x45, 0xaa, 0x02, 0x1b, 0x45, 0x69, 0x6e, + 0x72, 0x69, 0x64, 0x65, 0x2e, 0x42, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x2e, 0x45, 0x78, + 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x1b, 0x45, 0x69, 0x6e, 0x72, 0x69, + 0x64, 0x65, 0x5c, 0x42, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5c, 0x45, 0x78, 0x61, 0x6d, + 0x70, 0x6c, 0x65, 0x5c, 0x56, 0x31, 0xe2, 0x02, 0x27, 0x45, 0x69, 0x6e, 0x72, 0x69, 0x64, 0x65, + 0x5c, 0x42, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x5c, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, + 0x65, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, + 0xea, 0x02, 0x1e, 0x45, 0x69, 0x6e, 0x72, 0x69, 0x64, 0x65, 0x3a, 0x3a, 0x42, 0x69, 0x67, 0x71, + 0x75, 0x65, 0x72, 0x79, 0x3a, 0x3a, 0x45, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3a, 0x3a, 0x56, + 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_einride_bigquery_example_v1_example_optional_proto_rawDescOnce sync.Once + file_einride_bigquery_example_v1_example_optional_proto_rawDescData = file_einride_bigquery_example_v1_example_optional_proto_rawDesc +) + +func file_einride_bigquery_example_v1_example_optional_proto_rawDescGZIP() []byte { + file_einride_bigquery_example_v1_example_optional_proto_rawDescOnce.Do(func() { + file_einride_bigquery_example_v1_example_optional_proto_rawDescData = protoimpl.X.CompressGZIP(file_einride_bigquery_example_v1_example_optional_proto_rawDescData) + }) + return file_einride_bigquery_example_v1_example_optional_proto_rawDescData +} + +var file_einride_bigquery_example_v1_example_optional_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_einride_bigquery_example_v1_example_optional_proto_goTypes = []interface{}{ + (*ExampleOptional)(nil), // 0: einride.bigquery.example.v1.ExampleOptional +} +var file_einride_bigquery_example_v1_example_optional_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_einride_bigquery_example_v1_example_optional_proto_init() } +func file_einride_bigquery_example_v1_example_optional_proto_init() { + if File_einride_bigquery_example_v1_example_optional_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_einride_bigquery_example_v1_example_optional_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ExampleOptional); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + file_einride_bigquery_example_v1_example_optional_proto_msgTypes[0].OneofWrappers = []interface{}{} + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_einride_bigquery_example_v1_example_optional_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_einride_bigquery_example_v1_example_optional_proto_goTypes, + DependencyIndexes: file_einride_bigquery_example_v1_example_optional_proto_depIdxs, + MessageInfos: file_einride_bigquery_example_v1_example_optional_proto_msgTypes, + }.Build() + File_einride_bigquery_example_v1_example_optional_proto = out.File + file_einride_bigquery_example_v1_example_optional_proto_rawDesc = nil + file_einride_bigquery_example_v1_example_optional_proto_goTypes = nil + file_einride_bigquery_example_v1_example_optional_proto_depIdxs = nil +} diff --git a/internal/examples/proto/gen/json/einride_bigquery_example_v1_exampleoptional.json b/internal/examples/proto/gen/json/einride_bigquery_example_v1_exampleoptional.json new file mode 100644 index 0000000..f27507e --- /dev/null +++ b/internal/examples/proto/gen/json/einride_bigquery_example_v1_exampleoptional.json @@ -0,0 +1,7 @@ +[ + { + "name": "opt", + "type": "FLOAT", + "mode": "NULLABLE" + } +]