Skip to content

Commit

Permalink
fix: refactor codebase to protobuf-go v2 (#1489)
Browse files Browse the repository at this point in the history
* refactor gengapic protov2
* refactor gencli protov2
* remove last remnants
* update test script and ci
* update bazel deps to proto go v2
* remove unnecessary wkt resolution directives
* update repositories.bzl
  • Loading branch information
noahdietz authored Apr 11, 2024
1 parent c1fd44c commit e84b5ef
Show file tree
Hide file tree
Showing 49 changed files with 1,222 additions and 1,283 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ jobs:
- name: Install tools and dependencies
run: |
go install golang.org/x/exp/cmd/apidiff@latest
go install github.com/golang/protobuf/protoc-gen-go@latest
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
curl -sSL https://github.com/googleapis/googleapis/archive/master.zip > googleapis.zip
unzip googleapis.zip -x "googleapis-master/google/ads/*"
mv googleapis-master /tmp/googleapis
Expand Down
7 changes: 0 additions & 7 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,3 @@ gazelle(name = "gazelle")
# gazelle:resolve proto go google/api/annotations.proto @org_golang_google_genproto//googleapis/api/annotations
# gazelle:resolve proto proto google/longrunning/operations.proto @com_google_googleapis//google/longrunning:operations_proto
# gazelle:resolve proto go google/longrunning/operations.proto @com_google_googleapis//google/longrunning:longrunning_go_proto

# Note: Direct gazelle to resolve the protobuf-go v1 module to rules_go well
# known types. This will be removed when we migrate to protobuf-go v2.
#
# gazelle:resolve go go github.com/golang/protobuf/protoc-gen-go/plugin @io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto
# gazelle:resolve go go github.com/golang/protobuf/protoc-gen-go/descriptor @io_bazel_rules_go//proto/wkt:descriptor_go_proto
# gazelle:resolve go go github.com/golang/protobuf/ptypes/duration @io_bazel_rules_go//proto/wkt:duration_go_proto
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go_cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//internal/gencli",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@org_golang_google_protobuf//proto",
"@org_golang_google_protobuf//types/pluginpb",
],
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/protoc-gen-go_cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
"log"
"os"

plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/googleapis/gapic-generator-go/internal/gencli"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)

func main() {
reqBytes, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
var genReq plugin.CodeGeneratorRequest
var genReq pluginpb.CodeGeneratorRequest
if err := proto.Unmarshal(reqBytes, &genReq); err != nil {
log.Fatal(err)
}
Expand All @@ -38,7 +38,7 @@ func main() {
if err != nil {
genResp.Error = proto.String(err.Error())
}
genResp.SupportedFeatures = proto.Uint64(uint64(plugin.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL))
genResp.SupportedFeatures = proto.Uint64(uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL))

outBytes, err := proto.Marshal(genResp)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go_gapic/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//internal/gengapic",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@org_golang_google_protobuf//proto",
"@org_golang_google_protobuf//types/pluginpb",
],
)

Expand Down
6 changes: 3 additions & 3 deletions cmd/protoc-gen-go_gapic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ import (
"log"
"os"

plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/googleapis/gapic-generator-go/internal/gengapic"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)

func main() {
reqBytes, err := io.ReadAll(os.Stdin)
if err != nil {
log.Fatal(err)
}
var genReq plugin.CodeGeneratorRequest
var genReq pluginpb.CodeGeneratorRequest
if err := proto.Unmarshal(reqBytes, &genReq); err != nil {
log.Fatal(err)
}
Expand All @@ -39,7 +39,7 @@ func main() {
genResp.Error = proto.String(err.Error())
}

genResp.SupportedFeatures = proto.Uint64(uint64(plugin.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL))
genResp.SupportedFeatures = proto.Uint64(uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL))

outBytes, err := proto.Marshal(genResp)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require (
cloud.google.com/go/iam v1.1.7
cloud.google.com/go/longrunning v0.5.6
github.com/ghodss/yaml v1.0.0
github.com/golang/protobuf v1.5.4
github.com/google/go-cmp v0.6.0
github.com/jhump/protoreflect v1.15.6
gitlab.com/golang-commonmark/markdown v0.0.0-20211110145824-bf3e522c626a
Expand All @@ -17,6 +16,7 @@ require (

require (
github.com/bufbuild/protocompile v0.8.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
gitlab.com/golang-commonmark/html v0.0.0-20191124015941-a22733972181 // indirect
gitlab.com/golang-commonmark/linkify v0.0.0-20191026162114-a0c2df6c8f82 // indirect
gitlab.com/golang-commonmark/mdurl v0.0.0-20191124015652-932350d1cb84 // indirect
Expand Down
6 changes: 3 additions & 3 deletions internal/gencli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ go_library(
"//internal/printer",
"@com_github_jhump_protoreflect//desc",
"@com_google_cloud_go_longrunning//autogen/longrunningpb",
"@io_bazel_rules_go//proto/wkt:compiler_plugin_go_proto",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@org_golang_google_genproto_googleapis_api//annotations",
"@org_golang_google_protobuf//proto",
"@org_golang_google_protobuf//types/descriptorpb",
"@org_golang_google_protobuf//types/pluginpb",
"@org_golang_x_text//cases",
"@org_golang_x_text//language",
],
Expand All @@ -42,6 +42,6 @@ go_test(
deps = [
"//internal/pbinfo",
"//internal/txtdiff",
"@io_bazel_rules_go//proto/wkt:descriptor_go_proto",
"@org_golang_google_protobuf//types/descriptorpb",
],
)
12 changes: 6 additions & 6 deletions internal/gencli/cmd_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
"path/filepath"
"testing"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"github.com/googleapis/gapic-generator-go/internal/txtdiff"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestCommandFile(t *testing.T) {
Expand All @@ -46,7 +46,7 @@ func TestCommandFile(t *testing.T) {
Flags: []*Flag{
&Flag{
Name: "task",
Type: descriptor.FieldDescriptorProto_TYPE_STRING,
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING,
Required: true,
FieldName: "Task",
VarName: "CreateTodoInput",
Expand All @@ -56,14 +56,14 @@ func TestCommandFile(t *testing.T) {
Name: "done",
FieldName: "Done",
VarName: "CreateTodoInput",
Type: descriptor.FieldDescriptorProto_TYPE_BOOL,
Type: descriptorpb.FieldDescriptorProto_TYPE_BOOL,
Usage: "task completion status",
Optional: true,
},
&Flag{
Name: "priority",
FieldName: "Priority",
Type: descriptor.FieldDescriptorProto_TYPE_ENUM,
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM,
Usage: "importance of the task",
Message: "Priority",
MessageImport: pbinfo.ImportSpec{Name: "todopb"},
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestCommandFile(t *testing.T) {
Name: "id",
FieldName: "Id",
VarName: "StartTodoInput",
Type: descriptor.FieldDescriptorProto_TYPE_INT32,
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32,
Required: true,
Usage: "task to start",
},
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestCommandFile(t *testing.T) {
Name: "id",
FieldName: "Id",
VarName: "WatchTodoInput",
Type: descriptor.FieldDescriptorProto_TYPE_INT32,
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32,
Required: true,
Usage: "task to watch",
},
Expand Down
12 changes: 6 additions & 6 deletions internal/gencli/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import (
"unicode"
"unicode/utf8"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/googleapis/gapic-generator-go/internal/pbinfo"
"github.com/jhump/protoreflect/desc"
"google.golang.org/protobuf/types/descriptorpb"
)

// Flag is used to represent fields as flags
type Flag struct {
Name string
Type descriptor.FieldDescriptorProto_Type
Type descriptorpb.FieldDescriptorProto_Type
Message string
Repeated bool
Required bool
Expand Down Expand Up @@ -61,7 +61,7 @@ func (f *Flag) GenFlag() string {
fType := toTitle(tStr)

if f.Repeated {
if f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
if f.Type == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE {
// repeated Messages are entered as JSON strings and unmarshaled into the Message type later
return fmt.Sprintf(`StringArrayVar(&%s, "%s", []string{}, "%s")`, f.VarName, f.Name, f.Usage)
}
Expand Down Expand Up @@ -111,17 +111,17 @@ func (f *Flag) GenFlag() string {

// IsMessage is a template helper that reports if the flag is a message type
func (f *Flag) IsMessage() bool {
return f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE
return f.Type == descriptorpb.FieldDescriptorProto_TYPE_MESSAGE
}

// IsEnum is a template helper that reports if the flag is of an enum type
func (f *Flag) IsEnum() bool {
return f.Type == descriptor.FieldDescriptorProto_TYPE_ENUM
return f.Type == descriptorpb.FieldDescriptorProto_TYPE_ENUM
}

// IsBytes is a helper that reports if the flag is of a type bytes
func (f *Flag) IsBytes() bool {
return f.Type == descriptor.FieldDescriptorProto_TYPE_BYTES
return f.Type == descriptorpb.FieldDescriptorProto_TYPE_BYTES
}

// EnumFieldAccess constructs the input message field accessor for an enum
Expand Down
32 changes: 16 additions & 16 deletions internal/gencli/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package gencli
import (
"testing"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/protobuf/types/descriptorpb"
)

func TestGenFlag(t *testing.T) {
Expand All @@ -30,7 +30,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_STRING,
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING,
Usage: "this is the usage",
},
want: `StringVar(&ClientInput.Field, "field", "", "this is the usage")`,
Expand All @@ -40,7 +40,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_BOOL,
Type: descriptorpb.FieldDescriptorProto_TYPE_BOOL,
Usage: "this is the usage",
},
want: `BoolVar(&ClientInput.Field, "field", false, "this is the usage")`,
Expand All @@ -50,7 +50,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_INT32,
Type: descriptorpb.FieldDescriptorProto_TYPE_INT32,
Usage: "this is the usage",
},
want: `Int32Var(&ClientInput.Field, "field", 0, "this is the usage")`,
Expand All @@ -60,7 +60,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_FLOAT,
Type: descriptorpb.FieldDescriptorProto_TYPE_FLOAT,
Usage: "this is the usage",
},
want: `Float32Var(&ClientInput.Field, "field", 0.0, "this is the usage")`,
Expand All @@ -70,7 +70,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_DOUBLE,
Type: descriptorpb.FieldDescriptorProto_TYPE_DOUBLE,
Usage: "this is the usage",
},
want: `Float64Var(&ClientInput.Field, "field", 0.0, "this is the usage")`,
Expand All @@ -80,7 +80,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_BYTES,
Type: descriptorpb.FieldDescriptorProto_TYPE_BYTES,
Usage: "this is the usage",
},
want: `BytesHexVar(&ClientInput.Field, "field", []byte{}, "this is the usage")`,
Expand All @@ -90,7 +90,7 @@ func TestGenFlag(t *testing.T) {
Name: "field",
FieldName: "Field",
VarName: "ClientInput",
Type: descriptor.FieldDescriptorProto_TYPE_STRING,
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING,
Usage: "this is the usage",
Repeated: true,
},
Expand All @@ -100,7 +100,7 @@ func TestGenFlag(t *testing.T) {
f: &Flag{
Name: "field",
FieldName: "Field",
Type: descriptor.FieldDescriptorProto_TYPE_MESSAGE,
Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE,
Usage: "this is the usage",
Repeated: true,
VarName: "ClientInputField",
Expand All @@ -111,7 +111,7 @@ func TestGenFlag(t *testing.T) {
f: &Flag{
Name: "field",
FieldName: "Field",
Type: descriptor.FieldDescriptorProto_TYPE_ENUM,
Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM,
Usage: "this is the usage",
VarName: "ClientInputField",
},
Expand All @@ -121,7 +121,7 @@ func TestGenFlag(t *testing.T) {
f: &Flag{
Name: "oneof.field",
FieldName: "Field",
Type: descriptor.FieldDescriptorProto_TYPE_STRING,
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING,
Usage: "this is the usage",
VarName: "ClientInputOneofField",
IsOneOfField: true,
Expand All @@ -132,7 +132,7 @@ func TestGenFlag(t *testing.T) {
f: &Flag{
Name: "oneof_selector",
VarName: "ClientInputOneofSelector",
Type: descriptor.FieldDescriptorProto_TYPE_STRING,
Type: descriptorpb.FieldDescriptorProto_TYPE_STRING,
Usage: "this is the usage",
OneOfs: map[string]*Flag{"test": &Flag{}},
},
Expand All @@ -151,11 +151,11 @@ func TestIsMessage(t *testing.T) {
want bool
}{
{
f: &Flag{Type: descriptor.FieldDescriptorProto_TYPE_MESSAGE},
f: &Flag{Type: descriptorpb.FieldDescriptorProto_TYPE_MESSAGE},
want: true,
},
{
f: &Flag{Type: descriptor.FieldDescriptorProto_TYPE_STRING},
f: &Flag{Type: descriptorpb.FieldDescriptorProto_TYPE_STRING},
want: false,
},
} {
Expand All @@ -171,11 +171,11 @@ func TestIsEnum(t *testing.T) {
want bool
}{
{
f: &Flag{Type: descriptor.FieldDescriptorProto_TYPE_ENUM},
f: &Flag{Type: descriptorpb.FieldDescriptorProto_TYPE_ENUM},
want: true,
},
{
f: &Flag{Type: descriptor.FieldDescriptorProto_TYPE_STRING},
f: &Flag{Type: descriptorpb.FieldDescriptorProto_TYPE_STRING},
want: false,
},
} {
Expand Down
Loading

0 comments on commit e84b5ef

Please sign in to comment.