Skip to content

Commit b1b17f0

Browse files
committed
all: cleanup protoimpl calls
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]>
1 parent 97960cd commit b1b17f0

File tree

14 files changed

+34
-60
lines changed

14 files changed

+34
-60
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.9
44

55
require (
66
github.com/google/go-cmp v0.4.0
7-
google.golang.org/protobuf v0.0.0-20200224223749-2dabbe471100
7+
google.golang.org/protobuf v0.0.0-20200225203307-f6cf4925a90e
88
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
88
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
99
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
1010
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
11-
google.golang.org/protobuf v0.0.0-20200224223749-2dabbe471100 h1:i1uRQ2Eod1FWX6jGO/l8K0oMIwtCWeEmeDtLy5YABYc=
12-
google.golang.org/protobuf v0.0.0-20200224223749-2dabbe471100/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
11+
google.golang.org/protobuf v0.0.0-20200225203307-f6cf4925a90e h1:wrJFqPhiw6ByHbcNMcv/+6kk7HQLewtkSxANqkwtQ/0=
12+
google.golang.org/protobuf v0.0.0-20200225203307-f6cf4925a90e/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=

jsonpb/decode.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
protoV2 "google.golang.org/protobuf/proto"
2121
"google.golang.org/protobuf/reflect/protoreflect"
2222
"google.golang.org/protobuf/reflect/protoregistry"
23-
"google.golang.org/protobuf/runtime/protoimpl"
2423
)
2524

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

90-
mr := protoimpl.X.MessageOf(m)
89+
mr := proto.MessageReflect(m)
9190

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

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

143-
if jsu, ok := protoimpl.X.ProtoMessageV1Of(m.Interface()).(JSONPBUnmarshaler); ok {
142+
if jsu, ok := proto.MessageV1(m.Interface()).(JSONPBUnmarshaler); ok {
144143
return jsu.UnmarshalJSONPB(u, in)
145144
}
146145

@@ -167,7 +166,7 @@ func (u *Unmarshaler) unmarshalMessage(m protoreflect.Message, in []byte) error
167166
if err != nil {
168167
return err
169168
}
170-
m2 = protoimpl.X.MessageOf(mi)
169+
m2 = proto.MessageReflect(mi)
171170
} else {
172171
mt, err := protoregistry.GlobalTypes.FindMessageByURL(typeURL)
173172
if err != nil {

jsonpb/encode.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
protoV2 "google.golang.org/protobuf/proto"
2222
"google.golang.org/protobuf/reflect/protoreflect"
2323
"google.golang.org/protobuf/reflect/protoregistry"
24-
"google.golang.org/protobuf/runtime/protoimpl"
2524
)
2625

2726
const wrapJSONMarshalV2 = false
@@ -105,10 +104,10 @@ func (jm *Marshaler) marshal(m proto.Message) ([]byte, error) {
105104
if jm.AnyResolver != nil {
106105
opts.Resolver = anyResolver{jm.AnyResolver}
107106
}
108-
return opts.Marshal(protoimpl.X.MessageOf(m).Interface())
107+
return opts.Marshal(proto.MessageReflect(m).Interface())
109108
} else {
110109
// Check for unpopulated required fields first.
111-
m2 := protoimpl.X.MessageOf(m)
110+
m2 := proto.MessageReflect(m)
112111
if err := protoV2.CheckInitialized(m2.Interface()); err != nil {
113112
return nil, err
114113
}
@@ -129,7 +128,7 @@ func (w *jsonWriter) write(s string) {
129128
}
130129

131130
func (w *jsonWriter) marshalMessage(m protoreflect.Message, indent, typeURL string) error {
132-
if jsm, ok := protoimpl.X.ProtoMessageV1Of(m.Interface()).(JSONPBMarshaler); ok {
131+
if jsm, ok := proto.MessageV1(m.Interface()).(JSONPBMarshaler); ok {
133132
b, err := jsm.MarshalJSONPB(w.Marshaler)
134133
if err != nil {
135134
return err
@@ -321,7 +320,7 @@ func (w *jsonWriter) marshalAny(m protoreflect.Message, indent string) error {
321320
if err != nil {
322321
return err
323322
}
324-
m2 = protoimpl.X.MessageOf(mi)
323+
m2 = proto.MessageReflect(mi)
325324
} else {
326325
mt, err := protoregistry.GlobalTypes.FindMessageByURL(typeURL)
327326
if err != nil {

proto/buffer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (m *unknownFields) ProtoMessage() { panic("not implemented") }
130130
// DebugPrint dumps the encoded bytes of b with a header and footer including s
131131
// to stdout. This is only intended for debugging.
132132
func (*Buffer) DebugPrint(s string, b []byte) {
133-
m := protoimpl.X.MessageOf(new(unknownFields))
133+
m := MessageReflect(new(unknownFields))
134134
m.SetUnknown(b)
135135
b, _ = prototext.MarshalOptions{AllowPartial: true, Indent: "\t"}.Marshal(m.Interface())
136136
fmt.Printf("==== %s ====\n%s==== %s ====\n", s, b, s)

proto/defaults.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ package proto
66

77
import (
88
"google.golang.org/protobuf/reflect/protoreflect"
9-
"google.golang.org/protobuf/runtime/protoimpl"
109
)
1110

1211
// SetDefaults sets unpopulated scalar fields to their default values.
1312
// Fields within a oneof are not set even if they have a default value.
1413
// SetDefaults is recursively called upon any populated message fields.
1514
func SetDefaults(m Message) {
1615
if m != nil {
17-
setDefaults(protoimpl.X.MessageOf(m))
16+
setDefaults(MessageReflect(m))
1817
}
1918
}
2019

proto/discard.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package proto
66

77
import (
88
"google.golang.org/protobuf/reflect/protoreflect"
9-
"google.golang.org/protobuf/runtime/protoimpl"
109
)
1110

1211
// DiscardUnknown recursively discards all unknown fields from this message
@@ -19,7 +18,7 @@ import (
1918
// explicitly clear the unknown fields after unmarshaling.
2019
func DiscardUnknown(m Message) {
2120
if m != nil {
22-
discardUnknown(protoimpl.X.MessageOf(m))
21+
discardUnknown(MessageReflect(m))
2322
}
2423
}
2524

proto/extensions.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ var errNotExtendable = errors.New("proto: not an extendable proto.Message")
4343
// HasExtension reports whether the extension field is present in m
4444
// either as an explicitly populated field or as an unknown field.
4545
func HasExtension(m Message, xt *ExtensionDesc) (has bool) {
46-
mr := protoimpl.X.MessageOf(m)
46+
mr := MessageReflect(m)
4747
if mr == nil || !mr.IsValid() {
4848
return false
4949
}
@@ -71,7 +71,7 @@ func HasExtension(m Message, xt *ExtensionDesc) (has bool) {
7171
// ClearExtension removes the the exntesion field from m
7272
// either as an explicitly populated field or as an unknown field.
7373
func ClearExtension(m Message, xt *ExtensionDesc) {
74-
mr := protoimpl.X.MessageOf(m)
74+
mr := MessageReflect(m)
7575
if mr == nil || !mr.IsValid() {
7676
return
7777
}
@@ -94,7 +94,7 @@ func ClearExtension(m Message, xt *ExtensionDesc) {
9494
// ClearAllExtensions clears all extensions from m.
9595
// This includes populated fields and unknown fields in the extension range.
9696
func ClearAllExtensions(m Message) {
97-
mr := protoimpl.X.MessageOf(m)
97+
mr := MessageReflect(m)
9898
if mr == nil || !mr.IsValid() {
9999
return
100100
}
@@ -118,7 +118,7 @@ func ClearAllExtensions(m Message) {
118118
// If the descriptor is type incomplete (i.e., ExtensionDesc.ExtensionType is nil),
119119
// then GetExtension returns the raw encoded bytes for the extension field.
120120
func GetExtension(m Message, xt *ExtensionDesc) (interface{}, error) {
121-
mr := protoimpl.X.MessageOf(m)
121+
mr := MessageReflect(m)
122122
if mr == nil || !mr.IsValid() || mr.Descriptor().ExtensionRanges().Len() == 0 {
123123
return nil, errNotExtendable
124124
}
@@ -200,7 +200,7 @@ func (r extensionResolver) FindExtensionByNumber(message protoreflect.FullName,
200200
// corresponding with the provided list of extension descriptors, xts.
201201
// If an extension is missing in m, the corresponding value is nil.
202202
func GetExtensions(m Message, xts []*ExtensionDesc) ([]interface{}, error) {
203-
mr := protoimpl.X.MessageOf(m)
203+
mr := MessageReflect(m)
204204
if mr == nil || !mr.IsValid() {
205205
return nil, errNotExtendable
206206
}
@@ -221,7 +221,7 @@ func GetExtensions(m Message, xts []*ExtensionDesc) ([]interface{}, error) {
221221

222222
// SetExtension sets an extension field in m to the provided value.
223223
func SetExtension(m Message, xt *ExtensionDesc, v interface{}) error {
224-
mr := protoimpl.X.MessageOf(m)
224+
mr := MessageReflect(m)
225225
if mr == nil || !mr.IsValid() || mr.Descriptor().ExtensionRanges().Len() == 0 {
226226
return errNotExtendable
227227
}
@@ -252,7 +252,7 @@ func SetExtension(m Message, xt *ExtensionDesc, v interface{}) error {
252252
//
253253
// Deprecated: Use Message.ProtoReflect.SetUnknown instead.
254254
func SetRawExtension(m Message, fnum int32, b []byte) {
255-
mr := protoimpl.X.MessageOf(m)
255+
mr := MessageReflect(m)
256256
if mr == nil || !mr.IsValid() {
257257
return
258258
}
@@ -277,7 +277,7 @@ func SetRawExtension(m Message, fnum int32, b []byte) {
277277
// the ExtensionDesc.Field field is populated.
278278
// The order of the extension descriptors is undefined.
279279
func ExtensionDescs(m Message) ([]*ExtensionDesc, error) {
280-
mr := protoimpl.X.MessageOf(m)
280+
mr := MessageReflect(m)
281281
if mr == nil || !mr.IsValid() || mr.Descriptor().ExtensionRanges().Len() == 0 {
282282
return nil, errNotExtendable
283283
}

proto/proto.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,7 @@ func checkRequiredNotSet(m protoV2.Message) error {
127127

128128
// Clone returns a deep copy of src.
129129
func Clone(src Message) Message {
130-
srcMsg := protoimpl.X.MessageOf(src)
131-
if srcMsg == nil || !srcMsg.IsValid() {
132-
return src
133-
}
134-
135-
dst := protoimpl.X.ProtoMessageV1Of(srcMsg.New().Interface())
136-
Merge(dst, src)
137-
return dst
130+
return MessageV1(protoV2.Clone(MessageV2(src)))
138131
}
139132

140133
// Merge merges src into dst, which must be messages of the same type.
@@ -146,15 +139,7 @@ func Clone(src Message) Message {
146139
// the corresponding map field in dst, possibly replacing existing entries.
147140
// The unknown fields of src are appended to the unknown fields of dst.
148141
func Merge(dst, src Message) {
149-
// TODO: Drop this type assertion if the aberrant wrapper in v2 calls this.
150-
if m, ok := dst.(Merger); ok {
151-
m.Merge(src)
152-
return
153-
}
154-
protoV2.Merge(
155-
protoimpl.X.ProtoMessageV2Of(dst),
156-
protoimpl.X.ProtoMessageV2Of(src),
157-
)
142+
protoV2.Merge(MessageV2(dst), MessageV2(src))
158143
}
159144

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

182164
func isMessageSet(md protoreflect.MessageDescriptor) bool {

proto/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func enumGoType(et protoreflect.EnumType) reflect.Type {
261261
}
262262

263263
func messageGoType(mt protoreflect.MessageType) reflect.Type {
264-
return reflect.TypeOf(protoimpl.X.ProtoMessageV1Of(mt.New().Interface()))
264+
return reflect.TypeOf(MessageV1(mt.Zero().Interface()))
265265
}
266266

267267
// MessageName returns the full protobuf name for the given message type.

0 commit comments

Comments
 (0)