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

refactor: switch from syntax to presence checks #7532

Merged
merged 1 commit into from
Mar 9, 2023
Merged
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
52 changes: 24 additions & 28 deletions bigquery/storage/managedwriter/adapt/protoconversion.go
Original file line number Diff line number Diff line change
@@ -384,34 +384,30 @@ func normalizeDescriptorInternal(in protoreflect.MessageDescriptor, visitedTypes
for i := 0; i < in.Fields().Len(); i++ {
inField := in.Fields().Get(i)
resultFDP := protodesc.ToFieldDescriptorProto(inField)
// For proto3 messages without presence, use proto2 default values to match proto3
// behavior in default values.
if inField.Syntax() == protoreflect.Proto3 && inField.Cardinality() != protoreflect.Repeated {
// Only set default value if there's no field presence.
if resultFDP.Proto3Optional == nil || !resultFDP.GetProto3Optional() {
switch resultFDP.GetType() {
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
resultFDP.DefaultValue = proto.String("false")
case descriptorpb.FieldDescriptorProto_TYPE_BYTES, descriptorpb.FieldDescriptorProto_TYPE_STRING:
resultFDP.DefaultValue = proto.String("")
case descriptorpb.FieldDescriptorProto_TYPE_ENUM:
// Resolve the proto3 default value. The default value should be the value name.
defValue := inField.Enum().Values().ByNumber(inField.Default().Enum())
resultFDP.DefaultValue = proto.String(string(defValue.Name()))
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE,
descriptorpb.FieldDescriptorProto_TYPE_FLOAT,
descriptorpb.FieldDescriptorProto_TYPE_INT64,
descriptorpb.FieldDescriptorProto_TYPE_UINT64,
descriptorpb.FieldDescriptorProto_TYPE_INT32,
descriptorpb.FieldDescriptorProto_TYPE_FIXED64,
descriptorpb.FieldDescriptorProto_TYPE_FIXED32,
descriptorpb.FieldDescriptorProto_TYPE_UINT32,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED32,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED64,
descriptorpb.FieldDescriptorProto_TYPE_SINT32,
descriptorpb.FieldDescriptorProto_TYPE_SINT64:
resultFDP.DefaultValue = proto.String("0")
}
// For messages without explicit presence, use default values to match implicit presence behavior.
if !inField.HasPresence() && inField.Cardinality() != protoreflect.Repeated {
switch resultFDP.GetType() {
case descriptorpb.FieldDescriptorProto_TYPE_BOOL:
resultFDP.DefaultValue = proto.String("false")
case descriptorpb.FieldDescriptorProto_TYPE_BYTES, descriptorpb.FieldDescriptorProto_TYPE_STRING:
resultFDP.DefaultValue = proto.String("")
case descriptorpb.FieldDescriptorProto_TYPE_ENUM:
// Resolve the proto3 default value. The default value should be the value name.
defValue := inField.Enum().Values().ByNumber(inField.Default().Enum())
resultFDP.DefaultValue = proto.String(string(defValue.Name()))
case descriptorpb.FieldDescriptorProto_TYPE_DOUBLE,
descriptorpb.FieldDescriptorProto_TYPE_FLOAT,
descriptorpb.FieldDescriptorProto_TYPE_INT64,
descriptorpb.FieldDescriptorProto_TYPE_UINT64,
descriptorpb.FieldDescriptorProto_TYPE_INT32,
descriptorpb.FieldDescriptorProto_TYPE_FIXED64,
descriptorpb.FieldDescriptorProto_TYPE_FIXED32,
descriptorpb.FieldDescriptorProto_TYPE_UINT32,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED32,
descriptorpb.FieldDescriptorProto_TYPE_SFIXED64,
descriptorpb.FieldDescriptorProto_TYPE_SINT32,
descriptorpb.FieldDescriptorProto_TYPE_SINT64:
resultFDP.DefaultValue = proto.String("0")
}
}
// Clear proto3 optional annotation, as the backend converter can