diff --git a/bq_field.proto b/bq_field.proto index d1899c8..c18b7fd 100644 --- a/bq_field.proto +++ b/bq_field.proto @@ -44,6 +44,12 @@ message BigQueryFieldOptions { // Optionally add PolicyTag for a field in BigQuery schema. string policy_tags = 6; + + // Optional default value. + // + // See https://cloud.google.com/bigquery/docs/default-values for possible + // values. + string default_value_expression = 7; } diff --git a/examples/foo/test_table.schema b/examples/foo/test_table.schema index 8db5e92..222add6 100644 --- a/examples/foo/test_table.schema +++ b/examples/foo/test_table.schema @@ -7,7 +7,8 @@ "names": [ "private" ] - } + }, + "defaultValueExpression": "GENERATE_UUID()" }, { "name": "b", diff --git a/examples/test_table.proto b/examples/test_table.proto index 871b262..e3b3e44 100644 --- a/examples/test_table.proto +++ b/examples/test_table.proto @@ -8,19 +8,18 @@ message TestTable{ int32 a = 1 [ (gen_bq_schema.bigquery) = { - require: true - policy_tags : "private" - } - ]; - + require: true + policy_tags : "private" + default_value_expression: "GENERATE_UUID()" + }]; + string b = 2 [(gen_bq_schema.bigquery).policy_tags="public"]; message Nested { int32 a = 1 [(gen_bq_schema.bigquery) = { require: true policy_tags : "private" - } - ]; + }]; string b = 2; } diff --git a/go.mod b/go.mod index 79c7b62..7cbf30a 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/GoogleCloudPlatform/protoc-gen-bq-schema go 1.16 require ( - github.com/golang/glog v1.2.0 + github.com/golang/glog v1.2.1 google.golang.org/protobuf v1.33.0 ) diff --git a/go.sum b/go.sum index 4e1de97..96f4636 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/golang/glog v1.2.0 h1:uCdmnmatrKCgMBlM4rMuJZWOkPDqdbZPnrMXDY4gI68= github.com/golang/glog v1.2.0/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= +github.com/golang/glog v1.2.1 h1:OptwRhECazUx5ix5TTWC3EZhsZEHWcYWY4FQHTIubm4= +github.com/golang/glog v1.2.1/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= diff --git a/pkg/converter/convert.go b/pkg/converter/convert.go index b15f1a0..de49954 100644 --- a/pkg/converter/convert.go +++ b/pkg/converter/convert.go @@ -64,12 +64,13 @@ var ( // Field describes the schema of a field in BigQuery. type Field struct { - Name string `json:"name"` - Type string `json:"type"` - Mode string `json:"mode"` - Description string `json:"description,omitempty"` - Fields []*Field `json:"fields,omitempty"` - PolicyTags *PolicyTags `json:"policyTags,omitempty"` + Name string `json:"name"` + Type string `json:"type"` + Mode string `json:"mode"` + Description string `json:"description,omitempty"` + Fields []*Field `json:"fields,omitempty"` + PolicyTags *PolicyTags `json:"policyTags,omitempty"` + DefaultValueExpression string `json:"defaultValueExpression,omitempty"` } // PolicyTags describes the structure of a Policy Tag @@ -166,6 +167,10 @@ func convertField( Names: []string{opt.PolicyTags}, } } + + if len(opt.DefaultValueExpression) > 0 { + field.DefaultValueExpression = opt.DefaultValueExpression + } } if len(field.Description) > 1024 { diff --git a/protos/bq_field.pb.go b/protos/bq_field.pb.go index f89665a..82d5770 100644 --- a/protos/bq_field.pb.go +++ b/protos/bq_field.pb.go @@ -58,6 +58,11 @@ type BigQueryFieldOptions struct { Name string `protobuf:"bytes,5,opt,name=name,proto3" json:"name,omitempty"` // Optionally add PolicyTag for a field in BigQuery schema. PolicyTags string `protobuf:"bytes,6,opt,name=policy_tags,json=policyTags,proto3" json:"policy_tags,omitempty"` + // Optional default value. + // + // See https://cloud.google.com/bigquery/docs/default-values for possible + // values. + DefaultValueExpression string `protobuf:"bytes,7,opt,name=default_value_expression,json=defaultValueExpression,proto3" json:"default_value_expression,omitempty"` } func (x *BigQueryFieldOptions) Reset() { @@ -134,6 +139,13 @@ func (x *BigQueryFieldOptions) GetPolicyTags() string { return "" } +func (x *BigQueryFieldOptions) GetDefaultValueExpression() string { + if x != nil { + return x.DefaultValueExpression + } + return "" +} + var file_bq_field_proto_extTypes = []protoimpl.ExtensionInfo{ { ExtendedType: (*descriptorpb.FieldOptions)(nil), @@ -160,7 +172,7 @@ var file_bq_field_proto_rawDesc = []byte{ 0x12, 0x0d, 0x67, 0x65, 0x6e, 0x5f, 0x62, 0x71, 0x5f, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x22, 0xc4, 0x01, 0x0a, 0x14, 0x42, 0x69, 0x67, 0x51, 0x75, 0x65, 0x72, 0x79, 0x46, 0x69, + 0x6f, 0x22, 0xfe, 0x01, 0x0a, 0x14, 0x42, 0x69, 0x67, 0x51, 0x75, 0x65, 0x72, 0x79, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x07, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x74, 0x79, 0x70, 0x65, 0x5f, 0x6f, 0x76, 0x65, @@ -172,17 +184,21 @@ var file_bq_field_proto_rawDesc = []byte{ 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x0b, 0x70, 0x6f, 0x6c, 0x69, 0x63, 0x79, 0x5f, 0x74, 0x61, 0x67, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a, 0x70, 0x6f, - 0x6c, 0x69, 0x63, 0x79, 0x54, 0x61, 0x67, 0x73, 0x3a, 0x5f, 0x0a, 0x08, 0x62, 0x69, 0x67, 0x71, - 0x75, 0x65, 0x72, 0x79, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, - 0x6f, 0x6e, 0x73, 0x18, 0xfd, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x23, 0x2e, 0x67, 0x65, 0x6e, - 0x5f, 0x62, 0x71, 0x5f, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x2e, 0x42, 0x69, 0x67, 0x51, 0x75, - 0x65, 0x72, 0x79, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, - 0x08, 0x62, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x42, 0x3c, 0x5a, 0x3a, 0x67, 0x69, 0x74, - 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x43, 0x6c, - 0x6f, 0x75, 0x64, 0x50, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x2f, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, 0x2d, 0x62, 0x71, 0x2d, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, - 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6c, 0x69, 0x63, 0x79, 0x54, 0x61, 0x67, 0x73, 0x12, 0x38, 0x0a, 0x18, 0x64, 0x65, 0x66, 0x61, + 0x75, 0x6c, 0x74, 0x5f, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x5f, 0x65, 0x78, 0x70, 0x72, 0x65, 0x73, + 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x16, 0x64, 0x65, 0x66, 0x61, + 0x75, 0x6c, 0x74, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x73, 0x69, + 0x6f, 0x6e, 0x3a, 0x5f, 0x0a, 0x08, 0x62, 0x69, 0x67, 0x71, 0x75, 0x65, 0x72, 0x79, 0x12, 0x1d, + 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, + 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xfd, 0x07, + 0x20, 0x01, 0x28, 0x0b, 0x32, 0x23, 0x2e, 0x67, 0x65, 0x6e, 0x5f, 0x62, 0x71, 0x5f, 0x73, 0x63, + 0x68, 0x65, 0x6d, 0x61, 0x2e, 0x42, 0x69, 0x67, 0x51, 0x75, 0x65, 0x72, 0x79, 0x46, 0x69, 0x65, + 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x08, 0x62, 0x69, 0x67, 0x71, 0x75, + 0x65, 0x72, 0x79, 0x42, 0x3c, 0x5a, 0x3a, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x43, 0x6c, 0x6f, 0x75, 0x64, 0x50, 0x6c, 0x61, + 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x2d, 0x67, 0x65, 0x6e, + 0x2d, 0x62, 0x71, 0x2d, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var (