cmd/protoc-gen-go: only depend on v2 proto package
This CL breaks another dependency of v2 on v1.
A missing feature in v2 is proto.Clone, but we can use a
Marshal/Unmarshal roundtrip to achieve the same effect
as a temporary stop-gap.
Change-Id: I9d0432fd3efe9fc8b34db6c5a1eabbef0c36277c
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168217
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/cmd/protoc-gen-go/internal_gengo/main.go b/cmd/protoc-gen-go/internal_gengo/main.go
index f204853..7a28ae6 100644
--- a/cmd/protoc-gen-go/internal_gengo/main.go
+++ b/cmd/protoc-gen-go/internal_gengo/main.go
@@ -17,9 +17,9 @@
"unicode"
"unicode/utf8"
- "github.com/golang/protobuf/proto"
"github.com/golang/protobuf/v2/internal/descfield"
"github.com/golang/protobuf/v2/internal/encoding/tag"
+ "github.com/golang/protobuf/v2/proto"
"github.com/golang/protobuf/v2/protogen"
"github.com/golang/protobuf/v2/reflect/protoreflect"
@@ -228,11 +228,21 @@
}
func genFileDescriptor(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo) {
+ // TODO: Replace this with v2 Clone.
+ descProto := new(descriptorpb.FileDescriptorProto)
+ b, err := proto.Marshal(f.Proto)
+ if err != nil {
+ gen.Error(err)
+ return
+ }
+ if err := proto.Unmarshal(b, descProto); err != nil {
+ gen.Error(err)
+ return
+ }
+
// Trim the source_code_info from the descriptor.
- // Marshal and gzip it.
- descProto := proto.Clone(f.Proto).(*descriptorpb.FileDescriptorProto)
descProto.SourceCodeInfo = nil
- b, err := proto.Marshal(descProto)
+ b, err = proto.MarshalOptions{Deterministic: true}.Marshal(descProto)
if err != nil {
gen.Error(err)
return
diff --git a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta
index c966993..8b28463 100644
--- a/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta
+++ b/cmd/protoc-gen-go/testdata/annotations/annotations.pb.go.meta
@@ -1 +1 @@
-annotation:<path:5 path:0 source_file:"annotations/annotations.proto" begin:318 end:337 > annotation:<path:5 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:354 end:401 > annotation:<path:4 path:0 source_file:"annotations/annotations.proto" begin:1562 end:1584 > annotation:<path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:1595 end:1615 > annotation:<path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:3170 end:3193 >
\ No newline at end of file
+annotation:{path:5 path:0 source_file:"annotations/annotations.proto" begin:318 end:337} annotation:{path:5 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:354 end:401} annotation:{path:4 path:0 source_file:"annotations/annotations.proto" begin:1562 end:1584} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:1595 end:1615} annotation:{path:4 path:0 path:2 path:0 source_file:"annotations/annotations.proto" begin:3170 end:3193}
\ No newline at end of file
diff --git a/protogen/protogen.go b/protogen/protogen.go
index cf1e809..23bc69a 100644
--- a/protogen/protogen.go
+++ b/protogen/protogen.go
@@ -28,9 +28,10 @@
"strconv"
"strings"
- "github.com/golang/protobuf/proto"
+ "github.com/golang/protobuf/v2/encoding/textpb"
"github.com/golang/protobuf/v2/internal/descfield"
"github.com/golang/protobuf/v2/internal/scalar"
+ "github.com/golang/protobuf/v2/proto"
"github.com/golang/protobuf/v2/reflect/protodesc"
"github.com/golang/protobuf/v2/reflect/protoreflect"
"github.com/golang/protobuf/v2/reflect/protoregistry"
@@ -1109,7 +1110,11 @@
}
}
- return strings.TrimSpace(proto.CompactTextString(info)), nil
+ b, err := textpb.MarshalOptions{Compact: true}.Marshal(info)
+ if err != nil {
+ return "", err
+ }
+ return string(b), nil
}
type pathType int