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>
This commit is contained in:
Joe Tsai 2019-03-18 14:54:34 -07:00 committed by Joe Tsai
parent 8e506a8704
commit f31bf2612e
3 changed files with 22 additions and 7 deletions

View File

@ -17,9 +17,9 @@ import (
"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 genImport(gen *protogen.Plugin, g *protogen.GeneratedFile, f *fileInfo, imp
}
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

View File

@ -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 >
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}

View File

@ -28,9 +28,10 @@ import (
"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 @@ func (g *GeneratedFile) metaFile(content []byte) (string, error) {
}
}
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