Skip to content

Commit

Permalink
all: cleanup protoimpl calls
Browse files Browse the repository at this point in the history
Use MessageV1, MessageV2, and MessageReflect directly.

Change-Id: Ifb44e2cc2dcb52a44894b52d2e932558e275aecd
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/219558
Reviewed-by: Damien Neil <[email protected]>
  • Loading branch information
dsnet committed Feb 25, 2020
1 parent 97960cd commit b1b17f0
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 60 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.9

require (
github.com/google/go-cmp v0.4.0
google.golang.org/protobuf v0.0.0-20200224223749-2dabbe471100
google.golang.org/protobuf v0.0.0-20200225203307-f6cf4925a90e
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
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-20200224223749-2dabbe471100 h1:i1uRQ2Eod1FWX6jGO/l8K0oMIwtCWeEmeDtLy5YABYc=
google.golang.org/protobuf v0.0.0-20200224223749-2dabbe471100/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v0.0.0-20200225203307-f6cf4925a90e h1:wrJFqPhiw6ByHbcNMcv/+6kk7HQLewtkSxANqkwtQ/0=
google.golang.org/protobuf v0.0.0-20200225203307-f6cf4925a90e/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
9 changes: 4 additions & 5 deletions jsonpb/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
protoV2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"
)

const wrapJSONUnmarshalV2 = false
Expand Down Expand Up @@ -87,7 +86,7 @@ func (u *Unmarshaler) UnmarshalNext(d *json.Decoder, m proto.Message) error {
return jsu.UnmarshalJSONPB(u, raw)
}

mr := protoimpl.X.MessageOf(m)
mr := proto.MessageReflect(m)

// NOTE: For historical reasons, a top-level null is treated as a noop.
// This is incorrect, but kept for compatibility.
Expand All @@ -109,7 +108,7 @@ func (u *Unmarshaler) UnmarshalNext(d *json.Decoder, m proto.Message) error {
mr = mr.New()

// Use a defer to copy all unmarshaled fields into the original message.
dst := protoimpl.X.MessageOf(m)
dst := proto.MessageReflect(m)
defer mr.Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
dst.Set(fd, v)
return true
Expand Down Expand Up @@ -140,7 +139,7 @@ func (u *Unmarshaler) unmarshalMessage(m protoreflect.Message, in []byte) error
return nil
}

if jsu, ok := protoimpl.X.ProtoMessageV1Of(m.Interface()).(JSONPBUnmarshaler); ok {
if jsu, ok := proto.MessageV1(m.Interface()).(JSONPBUnmarshaler); ok {
return jsu.UnmarshalJSONPB(u, in)
}

Expand All @@ -167,7 +166,7 @@ func (u *Unmarshaler) unmarshalMessage(m protoreflect.Message, in []byte) error
if err != nil {
return err
}
m2 = protoimpl.X.MessageOf(mi)
m2 = proto.MessageReflect(mi)
} else {
mt, err := protoregistry.GlobalTypes.FindMessageByURL(typeURL)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions jsonpb/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
protoV2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"
)

const wrapJSONMarshalV2 = false
Expand Down Expand Up @@ -105,10 +104,10 @@ func (jm *Marshaler) marshal(m proto.Message) ([]byte, error) {
if jm.AnyResolver != nil {
opts.Resolver = anyResolver{jm.AnyResolver}
}
return opts.Marshal(protoimpl.X.MessageOf(m).Interface())
return opts.Marshal(proto.MessageReflect(m).Interface())
} else {
// Check for unpopulated required fields first.
m2 := protoimpl.X.MessageOf(m)
m2 := proto.MessageReflect(m)
if err := protoV2.CheckInitialized(m2.Interface()); err != nil {
return nil, err
}
Expand All @@ -129,7 +128,7 @@ func (w *jsonWriter) write(s string) {
}

func (w *jsonWriter) marshalMessage(m protoreflect.Message, indent, typeURL string) error {
if jsm, ok := protoimpl.X.ProtoMessageV1Of(m.Interface()).(JSONPBMarshaler); ok {
if jsm, ok := proto.MessageV1(m.Interface()).(JSONPBMarshaler); ok {
b, err := jsm.MarshalJSONPB(w.Marshaler)
if err != nil {
return err
Expand Down Expand Up @@ -321,7 +320,7 @@ func (w *jsonWriter) marshalAny(m protoreflect.Message, indent string) error {
if err != nil {
return err
}
m2 = protoimpl.X.MessageOf(mi)
m2 = proto.MessageReflect(mi)
} else {
mt, err := protoregistry.GlobalTypes.FindMessageByURL(typeURL)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion proto/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (m *unknownFields) ProtoMessage() { panic("not implemented") }
// DebugPrint dumps the encoded bytes of b with a header and footer including s
// to stdout. This is only intended for debugging.
func (*Buffer) DebugPrint(s string, b []byte) {
m := protoimpl.X.MessageOf(new(unknownFields))
m := MessageReflect(new(unknownFields))
m.SetUnknown(b)
b, _ = prototext.MarshalOptions{AllowPartial: true, Indent: "\t"}.Marshal(m.Interface())
fmt.Printf("==== %s ====\n%s==== %s ====\n", s, b, s)
Expand Down
3 changes: 1 addition & 2 deletions proto/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ package proto

import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"
)

// SetDefaults sets unpopulated scalar fields to their default values.
// Fields within a oneof are not set even if they have a default value.
// SetDefaults is recursively called upon any populated message fields.
func SetDefaults(m Message) {
if m != nil {
setDefaults(protoimpl.X.MessageOf(m))
setDefaults(MessageReflect(m))
}
}

Expand Down
3 changes: 1 addition & 2 deletions proto/discard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package proto

import (
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/runtime/protoimpl"
)

// DiscardUnknown recursively discards all unknown fields from this message
Expand All @@ -19,7 +18,7 @@ import (
// explicitly clear the unknown fields after unmarshaling.
func DiscardUnknown(m Message) {
if m != nil {
discardUnknown(protoimpl.X.MessageOf(m))
discardUnknown(MessageReflect(m))
}
}

Expand Down
16 changes: 8 additions & 8 deletions proto/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var errNotExtendable = errors.New("proto: not an extendable proto.Message")
// HasExtension reports whether the extension field is present in m
// either as an explicitly populated field or as an unknown field.
func HasExtension(m Message, xt *ExtensionDesc) (has bool) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() {
return false
}
Expand Down Expand Up @@ -71,7 +71,7 @@ func HasExtension(m Message, xt *ExtensionDesc) (has bool) {
// ClearExtension removes the the exntesion field from m
// either as an explicitly populated field or as an unknown field.
func ClearExtension(m Message, xt *ExtensionDesc) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() {
return
}
Expand All @@ -94,7 +94,7 @@ func ClearExtension(m Message, xt *ExtensionDesc) {
// ClearAllExtensions clears all extensions from m.
// This includes populated fields and unknown fields in the extension range.
func ClearAllExtensions(m Message) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() {
return
}
Expand All @@ -118,7 +118,7 @@ func ClearAllExtensions(m Message) {
// If the descriptor is type incomplete (i.e., ExtensionDesc.ExtensionType is nil),
// then GetExtension returns the raw encoded bytes for the extension field.
func GetExtension(m Message, xt *ExtensionDesc) (interface{}, error) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() || mr.Descriptor().ExtensionRanges().Len() == 0 {
return nil, errNotExtendable
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func (r extensionResolver) FindExtensionByNumber(message protoreflect.FullName,
// corresponding with the provided list of extension descriptors, xts.
// If an extension is missing in m, the corresponding value is nil.
func GetExtensions(m Message, xts []*ExtensionDesc) ([]interface{}, error) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() {
return nil, errNotExtendable
}
Expand All @@ -221,7 +221,7 @@ func GetExtensions(m Message, xts []*ExtensionDesc) ([]interface{}, error) {

// SetExtension sets an extension field in m to the provided value.
func SetExtension(m Message, xt *ExtensionDesc, v interface{}) error {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() || mr.Descriptor().ExtensionRanges().Len() == 0 {
return errNotExtendable
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func SetExtension(m Message, xt *ExtensionDesc, v interface{}) error {
//
// Deprecated: Use Message.ProtoReflect.SetUnknown instead.
func SetRawExtension(m Message, fnum int32, b []byte) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() {
return
}
Expand All @@ -277,7 +277,7 @@ func SetRawExtension(m Message, fnum int32, b []byte) {
// the ExtensionDesc.Field field is populated.
// The order of the extension descriptors is undefined.
func ExtensionDescs(m Message) ([]*ExtensionDesc, error) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() || mr.Descriptor().ExtensionRanges().Len() == 0 {
return nil, errNotExtendable
}
Expand Down
24 changes: 3 additions & 21 deletions proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,7 @@ func checkRequiredNotSet(m protoV2.Message) error {

// Clone returns a deep copy of src.
func Clone(src Message) Message {
srcMsg := protoimpl.X.MessageOf(src)
if srcMsg == nil || !srcMsg.IsValid() {
return src
}

dst := protoimpl.X.ProtoMessageV1Of(srcMsg.New().Interface())
Merge(dst, src)
return dst
return MessageV1(protoV2.Clone(MessageV2(src)))
}

// Merge merges src into dst, which must be messages of the same type.
Expand All @@ -146,15 +139,7 @@ func Clone(src Message) Message {
// the corresponding map field in dst, possibly replacing existing entries.
// The unknown fields of src are appended to the unknown fields of dst.
func Merge(dst, src Message) {
// TODO: Drop this type assertion if the aberrant wrapper in v2 calls this.
if m, ok := dst.(Merger); ok {
m.Merge(src)
return
}
protoV2.Merge(
protoimpl.X.ProtoMessageV2Of(dst),
protoimpl.X.ProtoMessageV2Of(src),
)
protoV2.Merge(MessageV2(dst), MessageV2(src))
}

// Equal reports whether two messages are equal.
Expand All @@ -173,10 +158,7 @@ func Merge(dst, src Message) {
// Maps are equal if they have the same set of keys, where the pair of values
// for each key is also equal.
func Equal(x, y Message) bool {
return protoV2.Equal(
protoimpl.X.ProtoMessageV2Of(x),
protoimpl.X.ProtoMessageV2Of(y),
)
return protoV2.Equal(MessageV2(x), MessageV2(y))
}

func isMessageSet(md protoreflect.MessageDescriptor) bool {
Expand Down
2 changes: 1 addition & 1 deletion proto/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func enumGoType(et protoreflect.EnumType) reflect.Type {
}

func messageGoType(mt protoreflect.MessageType) reflect.Type {
return reflect.TypeOf(protoimpl.X.ProtoMessageV1Of(mt.New().Interface()))
return reflect.TypeOf(MessageV1(mt.Zero().Interface()))
}

// MessageName returns the full protobuf name for the given message type.
Expand Down
3 changes: 1 addition & 2 deletions proto/text_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
protoV2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"
)

const wrapTextUnmarshalV2 = false
Expand Down Expand Up @@ -47,7 +46,7 @@ func UnmarshalText(s string, m Message) error {
}

m.Reset()
mi := protoimpl.X.ProtoMessageV2Of(m)
mi := MessageV2(m)

if wrapTextUnmarshalV2 {
err := prototext.UnmarshalOptions{
Expand Down
3 changes: 1 addition & 2 deletions proto/text_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"
)

const wrapTextMarshalV2 = false
Expand Down Expand Up @@ -47,7 +46,7 @@ func (tm *TextMarshaler) Text(m Message) string {
}

func (tm *TextMarshaler) marshal(m Message) ([]byte, error) {
mr := protoimpl.X.MessageOf(m)
mr := MessageReflect(m)
if mr == nil || !mr.IsValid() {
return []byte("<nil>"), nil
}
Expand Down
7 changes: 3 additions & 4 deletions proto/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ package proto
import (
protoV2 "google.golang.org/protobuf/proto"
"google.golang.org/protobuf/runtime/protoiface"
"google.golang.org/protobuf/runtime/protoimpl"
)

// Size returns the size in bytes of the wire-format encoding of m.
func Size(m Message) int {
if m == nil {
return 0
}
mi := protoimpl.X.ProtoMessageV2Of(m)
mi := MessageV2(m)
return protoV2.Size(mi)
}

Expand All @@ -34,7 +33,7 @@ func marshalAppend(buf []byte, m Message, deterministic bool) ([]byte, error) {
if m == nil {
return nil, ErrNil
}
mi := protoimpl.X.ProtoMessageV2Of(m)
mi := MessageV2(m)
nbuf, err := protoV2.MarshalOptions{
Deterministic: deterministic,
AllowPartial: true,
Expand All @@ -61,7 +60,7 @@ func Unmarshal(b []byte, m Message) error {

// UnmarshalMerge parses a wire-format message in b and places the decoded results in m.
func UnmarshalMerge(b []byte, m Message) error {
mi := protoimpl.X.ProtoMessageV2Of(m)
mi := MessageV2(m)
out, err := protoV2.UnmarshalOptions{
AllowPartial: true,
Merge: true,
Expand Down
7 changes: 3 additions & 4 deletions ptypes/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/runtime/protoimpl"

anypb "github.com/golang/protobuf/ptypes/any"
)
Expand Down Expand Up @@ -68,7 +67,7 @@ func Empty(any *anypb.Any) (proto.Message, error) {
if err != nil {
return nil, err
}
return protoimpl.X.ProtoMessageV1Of(mt.New().Interface()), nil
return proto.MessageV1(mt.New().Interface()), nil
}

// UnmarshalAny unmarshals the encoded value contained in the anypb.Any message
Expand Down Expand Up @@ -141,7 +140,7 @@ func (m DynamicAny) ProtoReflect() protoreflect.Message {
if m.Message == nil {
return nil
}
return dynamicAny{protoimpl.X.MessageOf(m.Message)}
return dynamicAny{proto.MessageReflect(m.Message)}
}

type dynamicAny struct{ protoreflect.Message }
Expand All @@ -153,7 +152,7 @@ func (m dynamicAny) New() protoreflect.Message {
return dynamicAnyType{m.Message.Type()}.New()
}
func (m dynamicAny) Interface() protoreflect.ProtoMessage {
return DynamicAny{protoimpl.X.ProtoMessageV1Of(m.Message.Interface())}
return DynamicAny{proto.MessageV1(m.Message.Interface())}
}

type dynamicAnyType struct{ protoreflect.MessageType }
Expand Down

0 comments on commit b1b17f0

Please sign in to comment.