internal/cmd/generate-alias: fix for lacking go_package options (#1061)

In https://golang.org/cl/219598, we removed patching the well-known types
with an explicit go_package option and instead relied on M flags.
The lack of updated go_package options broke generate-alias since it
appears as if the import public is trying to alias Go identifiers
within the same Go package (thus generating nothing).

We fix generate-alias by replicating the same approach here,
where we construct the M flag mapping and pass it to protoc-gen-go.
This commit is contained in:
Joe Tsai 2020-03-20 14:41:29 -07:00 коммит произвёл GitHub
Родитель b860323f09
Коммит 7592abeb96
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 36 добавлений и 22 удалений

2
go.mod
Просмотреть файл

@ -4,5 +4,5 @@ go 1.9
require (
github.com/google/go-cmp v0.4.0
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967
google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892
)

4
go.sum
Просмотреть файл

@ -1,6 +1,7 @@
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
@ -10,5 +11,6 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967 h1:DwkfSP6tZMxKX50J0dBSqEgJvJdFYP1Gvzbjtvkmrug=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892 h1:LQPEPzH8usKk01hUU5qdXb17Tgjr/BLZhHy1h90Vd7U=
google.golang.org/protobuf v1.20.2-0.20200320194150-9d397869d892/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=

Просмотреть файл

@ -38,48 +38,60 @@ func main() {
// Set of generated proto packages to forward to v2.
files := []struct {
goPkg string
pbDesc protoreflect.FileDescriptor
oldGoPkg string
newGoPkg string
pbDesc protoreflect.FileDescriptor
}{{
goPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor",
pbDesc: descriptorpb.File_google_protobuf_descriptor_proto,
oldGoPkg: "github.com/golang/protobuf/protoc-gen-go/descriptor;descriptor",
newGoPkg: "google.golang.org/protobuf/types/descriptorpb",
pbDesc: descriptorpb.File_google_protobuf_descriptor_proto,
}, {
goPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go",
pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto,
oldGoPkg: "github.com/golang/protobuf/protoc-gen-go/plugin;plugin_go",
newGoPkg: "google.golang.org/protobuf/types/pluginpb",
pbDesc: pluginpb.File_google_protobuf_compiler_plugin_proto,
}, {
goPkg: "github.com/golang/protobuf/ptypes/any;any",
pbDesc: anypb.File_google_protobuf_any_proto,
oldGoPkg: "github.com/golang/protobuf/ptypes/any;any",
newGoPkg: "google.golang.org/protobuf/types/known/anypb",
pbDesc: anypb.File_google_protobuf_any_proto,
}, {
goPkg: "github.com/golang/protobuf/ptypes/duration;duration",
pbDesc: durationpb.File_google_protobuf_duration_proto,
oldGoPkg: "github.com/golang/protobuf/ptypes/duration;duration",
newGoPkg: "google.golang.org/protobuf/types/known/durationpb",
pbDesc: durationpb.File_google_protobuf_duration_proto,
}, {
goPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp",
pbDesc: timestamppb.File_google_protobuf_timestamp_proto,
oldGoPkg: "github.com/golang/protobuf/ptypes/timestamp;timestamp",
newGoPkg: "google.golang.org/protobuf/types/known/timestamppb",
pbDesc: timestamppb.File_google_protobuf_timestamp_proto,
}, {
goPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers",
pbDesc: wrapperspb.File_google_protobuf_wrappers_proto,
oldGoPkg: "github.com/golang/protobuf/ptypes/wrappers;wrappers",
newGoPkg: "google.golang.org/protobuf/types/known/wrapperspb",
pbDesc: wrapperspb.File_google_protobuf_wrappers_proto,
}, {
goPkg: "github.com/golang/protobuf/ptypes/struct;structpb",
pbDesc: structpb.File_google_protobuf_struct_proto,
oldGoPkg: "github.com/golang/protobuf/ptypes/struct;structpb",
newGoPkg: "google.golang.org/protobuf/types/known/structpb",
pbDesc: structpb.File_google_protobuf_struct_proto,
}, {
goPkg: "github.com/golang/protobuf/ptypes/empty;empty",
pbDesc: emptypb.File_google_protobuf_empty_proto,
oldGoPkg: "github.com/golang/protobuf/ptypes/empty;empty",
newGoPkg: "google.golang.org/protobuf/types/known/emptypb",
pbDesc: emptypb.File_google_protobuf_empty_proto,
}}
// For each package, construct a proto file that public imports the package.
var req pluginpb.CodeGeneratorRequest
var flags []string
for _, file := range files {
pkgPath := file.goPkg[:strings.IndexByte(file.goPkg, ';')]
pkgPath := file.oldGoPkg[:strings.IndexByte(file.oldGoPkg, ';')]
fd := &descriptorpb.FileDescriptorProto{
Name: proto.String(pkgPath + "/" + path.Base(pkgPath) + ".proto"),
Syntax: proto.String(file.pbDesc.Syntax().String()),
Dependency: []string{file.pbDesc.Path()},
PublicDependency: []int32{0},
Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.goPkg)},
Options: &descriptorpb.FileOptions{GoPackage: proto.String(file.oldGoPkg)},
}
req.ProtoFile = append(req.ProtoFile, protodesc.ToFileDescriptorProto(file.pbDesc), fd)
req.FileToGenerate = append(req.FileToGenerate, fd.GetName())
flags = append(flags, "M"+file.pbDesc.Path()+"="+file.newGoPkg)
}
req.Parameter = proto.String(strings.Join(flags, ","))
// Use the internal logic of protoc-gen-go to generate the files.
gen, err := protogen.Options{}.New(&req)