Skip to content

Commit

Permalink
feat(bigquery/storage/managedwriter/adapt): handle oneof normalization (
Browse files Browse the repository at this point in the history
#5670)

* feat(bigquery/storage/managedwriter/adapt): handle oneof normalization

This updates NormalizeDescriptor to remove oneof indices from the
output DescriptorProto.  BigQuery schemas do not have a compatible
concept regarding schema enforcement.
  • Loading branch information
shollyman committed Feb 17, 2022
1 parent 876964f commit c7f54d8
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 14 deletions.
6 changes: 5 additions & 1 deletion bigquery/storage/managedwriter/adapt/protoconversion.go
Expand Up @@ -291,7 +291,8 @@ func tableFieldSchemaToFieldDescriptorProto(field *storagepb.TableFieldSchema, i
// and not the namespaces when decoding, this is sufficient for the needs of the API's representation.
//
// In addition to nesting messages, this method also handles some encapsulation of enum types to avoid possible
// conflicts due to ambiguities.
// conflicts due to ambiguities, and clears oneof indices as oneof isn't a concept that maps into BigQuery
// schemas.
func NormalizeDescriptor(in protoreflect.MessageDescriptor) (*descriptorpb.DescriptorProto, error) {
return normalizeDescriptorInternal(in, newStringSet(), newStringSet(), newStringSet(), nil)
}
Expand All @@ -310,6 +311,9 @@ func normalizeDescriptorInternal(in protoreflect.MessageDescriptor, visitedTypes
for i := 0; i < in.Fields().Len(); i++ {
inField := in.Fields().Get(i)
resultFDP := protodesc.ToFieldDescriptorProto(inField)
if resultFDP.OneofIndex != nil {
resultFDP.OneofIndex = nil
}
if inField.Kind() == protoreflect.MessageKind || inField.Kind() == protoreflect.GroupKind {
// Handle fields that reference messages.
// Groups are a proto2-ism which predated nested messages.
Expand Down
30 changes: 30 additions & 0 deletions bigquery/storage/managedwriter/adapt/protoconversion_test.go
Expand Up @@ -591,6 +591,36 @@ func TestNormalizeDescriptor(t *testing.T) {
},
},
},
{
description: "WithOneOf",
in: (&testdata.WithOneOf{}).ProtoReflect().Descriptor(),
want: &descriptorpb.DescriptorProto{
Name: proto.String("testdata_WithOneOf"),
Field: []*descriptorpb.FieldDescriptorProto{
{
Name: proto.String("int32_value"),
JsonName: proto.String("int32Value"),
Number: proto.Int32(1),
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
},
{
Name: proto.String("string_value"),
JsonName: proto.String("stringValue"),
Number: proto.Int32(2),
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
},
{
Name: proto.String("double_value"),
JsonName: proto.String("doubleValue"),
Number: proto.Int32(3),
Type: descriptorpb.FieldDescriptorProto_TYPE_DOUBLE.Enum(),
Label: descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
},
},
},
},
}

for _, tc := range testCases {
Expand Down
140 changes: 127 additions & 13 deletions bigquery/storage/managedwriter/testdata/testing.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 bigquery/storage/managedwriter/testdata/testing.proto
Expand Up @@ -65,4 +65,12 @@ message RecursiveType {

message RecursiveTypeTopMessage {
optional RecursiveTypeTopMessage field = 2;
}

message WithOneOf {
optional int32 int32_value = 1;
oneof oneof_value {
string string_value = 2;
double double_value = 3;
}
}

0 comments on commit c7f54d8

Please sign in to comment.