Skip to content

Commit 8f85408

Browse files
authored
Merge pull request #54 from stephenoken/master
Enums are now treated as type INTEGER in BigQuery
2 parents 5c940a6 + a7413e4 commit 8f85408

13 files changed

+66
-25
lines changed

.github/workflows/go.yml

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
protoc -I. -Iexamples --plugin=./protoc-gen-bq-schema --bq-schema_out=examples examples/foo.proto
4444
protoc -I. -Iexamples --plugin=./protoc-gen-bq-schema --bq-schema_out=examples examples/foo-proto3.proto
4545
protoc -I. -Iexamples --plugin=./protoc-gen-bq-schema --bq-schema_out=examples --bq-schema_opt=single-message examples/single_message.proto
46+
protoc -I. -Iexamples --plugin=./protoc-gen-bq-schema --bq-schema_out=examples --bq-schema_opt=enum-as-string examples/enum_as_string.proto
4647
- name: Verify examples are working
4748
run: |
4849
if [ -n "$(git status --porcelain)" ]; then

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ So you can reuse existing data definitions in .proto for BigQuery with this plug
88
## Installation
99

1010
```sh
11-
go install github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v2@latest
11+
go install github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v3@latest
1212
```
1313

1414
## Usage
@@ -72,6 +72,8 @@ The message `foo.Baz` is ignored because it doesn't have option `gen_bq_schema.b
7272
`protoc --bq-schema_out=. --bq-schema_opt=single-message single_message.proto` will generate a file named `foo/single_message.schema`.
7373
The message `foo.Baz` is also ignored because it is not the first message in the file.
7474

75+
`protoc --bq-schema_out=. --bq-schema_opt=enum-as-string foo.proto` will generate a file named `foo/bar_table.schema`.
76+
Fields that are of type enum will be converted to strings in the generated BigQuery schema.
7577

7678
### Support for PolicyTags
7779
`protoc-gen-bq-schema` now supports [policyTags](https://cloud.google.com/bigquery/docs/column-level-security-intro).

examples/enum_as_string.proto

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
syntax = "proto2";
2+
package foo;
3+
import "bq_table.proto";
4+
5+
message Bar {
6+
option (gen_bq_schema.bigquery_opts).table_name = "enum_as_string_table";
7+
optional Baz f = 6;
8+
9+
enum Baz {
10+
foo = 1;
11+
}
12+
}

examples/foo.proto

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ message Bar {
3232
}
3333
];
3434

35+
optional Qux f = 6;
36+
37+
enum Qux {
38+
foo = 1;
39+
}
40+
3541
optional google.protobuf.Int32Value wkt1 = 11;
3642
optional google.protobuf.Timestamp wkt2 = 12;
3743
}

examples/foo/bar_table.schema

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"mode": "REQUIRED",
3131
"description": "TIMESTAMP (uint64 in proto) - required in BigQuery"
3232
},
33+
{
34+
"name": "f",
35+
"type": "INTEGER",
36+
"mode": "NULLABLE"
37+
},
3338
{
3439
"name": "wkt1",
3540
"type": "INTEGER",
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"name": "f",
4+
"type": "STRING",
5+
"mode": "NULLABLE"
6+
}
7+
]

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
module github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v2
1+
module github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v3
22

33
go 1.21
44

55
require (
6-
github.com/golang/glog v1.2.3
7-
google.golang.org/protobuf v1.35.2
6+
github.com/golang/glog v1.2.4
7+
google.golang.org/protobuf v1.36.3
88
)

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
github.com/golang/glog v1.2.3 h1:oDTdz9f5VGVVNGu/Q7UXKWYsD0873HXLHdJUNBsSEKM=
2-
github.com/golang/glog v1.2.3/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
1+
github.com/golang/glog v1.2.4 h1:CNNw5U8lSiiBk7druxtSHHTsRWcxKoac6kZKm2peBBc=
2+
github.com/golang/glog v1.2.4/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w=
33
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
44
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
5-
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
6-
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
5+
google.golang.org/protobuf v1.36.3 h1:82DV7MYdb8anAVi3qge1wSnMDrnKK7ebr+I0hHRN1BU=
6+
google.golang.org/protobuf v1.36.3/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"fmt"
3030
"os"
3131

32-
"github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v2/pkg/converter"
32+
"github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v3/pkg/converter"
3333
"github.com/golang/glog"
3434
"google.golang.org/protobuf/proto"
3535
plugin "google.golang.org/protobuf/types/pluginpb"

pkg/converter/convert.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"sort"
99
"strings"
1010

11-
"github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v2/protos"
11+
"github.com/GoogleCloudPlatform/protoc-gen-bq-schema/v3/protos"
1212
"github.com/golang/glog"
1313
"google.golang.org/protobuf/encoding/prototext"
1414
"google.golang.org/protobuf/proto"
@@ -47,7 +47,7 @@ var (
4747

4848
descriptor.FieldDescriptorProto_TYPE_STRING: "STRING",
4949
descriptor.FieldDescriptorProto_TYPE_BYTES: "BYTES",
50-
descriptor.FieldDescriptorProto_TYPE_ENUM: "STRING",
50+
descriptor.FieldDescriptorProto_TYPE_ENUM: "INTEGER",
5151

5252
descriptor.FieldDescriptorProto_TYPE_BOOL: "BOOLEAN",
5353

@@ -401,6 +401,15 @@ func handleSingleMessageOpt(file *descriptor.FileDescriptorProto, requestParam s
401401
})
402402
}
403403

404+
// enumAsStringOpt handles --bq-schema_opt=enum-as-string in protoc params.
405+
// providing that param tesll protoc-gen-bq-schema to treat enums as strings.
406+
func enumAsStringOpt(requestParam string) {
407+
if !strings.Contains(requestParam, "enum-as-string") {
408+
return
409+
}
410+
typeFromFieldType[descriptor.FieldDescriptorProto_TYPE_ENUM] = "STRING"
411+
}
412+
404413
func Convert(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, error) {
405414
generateTargets := make(map[string]bool)
406415
for _, file := range req.GetFileToGenerate() {
@@ -412,6 +421,7 @@ func Convert(req *plugin.CodeGeneratorRequest) (*plugin.CodeGeneratorResponse, e
412421
MinimumEdition: proto.Int32(int32(descriptor.Edition_EDITION_PROTO2)),
413422
MaximumEdition: proto.Int32(int32(descriptor.Edition_EDITION_MAX)),
414423
}
424+
enumAsStringOpt(req.GetParameter())
415425
for _, file := range req.GetProtoFile() {
416426
for msgIndex, msg := range file.GetMessageType() {
417427
glog.V(1).Infof("Loading a message type %s from package %s", msg.GetName(), file.GetPackage())

pkg/converter/plugin_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ func TestTypes(t *testing.T) {
309309
{ "name": "bool", "type": "BOOLEAN", "mode": "NULLABLE" },
310310
{ "name": "str", "type": "STRING", "mode": "NULLABLE" },
311311
{ "name": "bytes", "type": "BYTES", "mode": "NULLABLE" },
312-
{ "name": "enum1", "type": "STRING", "mode": "NULLABLE" },
313-
{ "name": "enum2", "type": "STRING", "mode": "NULLABLE" },
312+
{ "name": "enum1", "type": "INTEGER", "mode": "NULLABLE" },
313+
{ "name": "enum2", "type": "INTEGER", "mode": "NULLABLE" },
314314
{
315315
"name": "grp1", "type": "RECORD", "mode": "NULLABLE",
316316
"fields": [{ "name": "i1", "type": "INTEGER", "mode": "NULLABLE" }]

protos/bq_field.pb.go

+5-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protos/bq_table.pb.go

+5-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)