Skip to content

Commit

Permalink
Add support for protbuf enums
Browse files Browse the repository at this point in the history
  • Loading branch information
ficoos committed Oct 29, 2023
1 parent c6d21f4 commit c683a8c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tools/mc2bq/pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"time"

"cloud.google.com/go/bigquery"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/GoogleCloudPlatform/migrationcenter-utils/tools/mc2bq/pkg/messages"
Expand Down Expand Up @@ -230,6 +232,8 @@ func normalizeToSchema(obj any, schema *bigquery.FieldSchema) (any, error) {
switch obj := objValue.Interface().(type) {
case string:
return obj, nil
case protoreflect.Enum:
return protoimpl.X.EnumStringOf(obj.Descriptor(), protoreflect.EnumNumber(obj.Number())), nil
default:
return nil, wrapWithSerializeError(schema.Name, fmt.Errorf("convert field of type %q to string", reflect.TypeOf(obj).String()))
}
Expand Down
4 changes: 4 additions & 0 deletions tools/mc2bq/pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"cloud.google.com/go/bigquery"
"cloud.google.com/go/migrationcenter/apiv1/migrationcenterpb"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/types/known/timestamppb"

Expand Down Expand Up @@ -55,6 +56,7 @@ func TestObjectSerializer(t *testing.T) {
RecordArray []*TestInnerStruct
RecordMap map[string]*TestInnerStruct
DeprecatedField string
Enum migrationcenterpb.CommitmentPlan
}
schema := bigquery.Schema{
{Name: "int_scalar", Type: bigquery.IntegerFieldType},
Expand All @@ -76,6 +78,7 @@ func TestObjectSerializer(t *testing.T) {
{Name: "key", Type: bigquery.StringFieldType},
{Name: "value", Type: bigquery.RecordFieldType, Schema: innerStructSchema},
}},
{Name: "enum", Type: bigquery.StringFieldType},
// DeprecatedField doesn't appear in the schema as it has been deprecated
}
// This ensures that we are testing all the field types that exist in the schema
Expand All @@ -97,6 +100,7 @@ func TestObjectSerializer(t *testing.T) {
RecordArray: []*TestInnerStruct{{FieldA: "1", FieldB: 1}, {FieldA: "2", FieldB: 2}},
RecordMap: map[string]*TestInnerStruct{"key": {FieldA: "value"}},
DeprecatedField: "I should not be in the result JSON",
Enum: 1,
}

got, err := SerializeObjectToBigQuery(obj, "object", schema)
Expand Down
1 change: 1 addition & 0 deletions tools/mc2bq/pkg/schema/testdata/exporter.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"value": "bar"
}
],
"enum": "COMMITMENT_PLAN_NONE",
"float32_scalar": 0.32,
"float64_scalar": 0.64,
"int_array": [
Expand Down

0 comments on commit c683a8c

Please # to comment.