blob: b712786eef85bccdaedd63e602e23f56a805e18f [file] [log] [blame]
Damien Neilba23aa52018-12-07 14:38:17 -08001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style.
3// license that can be found in the LICENSE file.
4
5package proto
6
7import (
Damien Neil302cb322019-06-19 15:22:13 -07008 "google.golang.org/protobuf/internal/encoding/messageset"
Damien Neile89e6242019-05-13 23:55:40 -07009 "google.golang.org/protobuf/internal/encoding/wire"
10 "google.golang.org/protobuf/internal/errors"
Joe Tsai6e095992019-08-10 13:56:36 -070011 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070012 "google.golang.org/protobuf/internal/pragma"
13 "google.golang.org/protobuf/reflect/protoreflect"
14 "google.golang.org/protobuf/reflect/protoregistry"
15 "google.golang.org/protobuf/runtime/protoiface"
Damien Neilba23aa52018-12-07 14:38:17 -080016)
17
18// UnmarshalOptions configures the unmarshaler.
19//
20// Example usage:
21// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
22type UnmarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070023 pragma.NoUnkeyedLiterals
24
Joe Tsai705acad2019-09-14 18:22:59 -070025 // Merge merges the input into the destination message.
26 // The default behavior is to always reset the message before unmarshaling,
27 // unless Merge is specified.
28 Merge bool
29
Damien Neil96c229a2019-04-03 12:17:24 -070030 // AllowPartial accepts input for messages that will result in missing
31 // required fields. If AllowPartial is false (the default), Unmarshal will
32 // return an error if there are any missing required fields.
33 AllowPartial bool
34
Damien Neilba23aa52018-12-07 14:38:17 -080035 // If DiscardUnknown is set, unknown fields are ignored.
36 DiscardUnknown bool
37
Joe Tsaidb38ddd2019-05-07 15:14:40 -070038 // Resolver is used for looking up types when unmarshaling extension fields.
39 // If nil, this defaults to using protoregistry.GlobalTypes.
Joe Tsai1c283042019-05-14 14:28:19 -070040 Resolver interface {
Damien Neilf12fb452020-01-21 11:27:51 -080041 FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
42 FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
Joe Tsai1c283042019-05-14 14:28:19 -070043 }
Damien Neilba23aa52018-12-07 14:38:17 -080044}
45
46// Unmarshal parses the wire-format message in b and places the result in m.
47func Unmarshal(b []byte, m Message) error {
Damien Neild30e5612020-01-22 10:28:16 -080048 _, err := UnmarshalOptions{}.unmarshal(b, m)
49 return err
Damien Neilba23aa52018-12-07 14:38:17 -080050}
51
52// Unmarshal parses the wire-format message in b and places the result in m.
53func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
Damien Neild30e5612020-01-22 10:28:16 -080054 _, err := o.unmarshal(b, m)
55 return err
56}
57
58// UnmarshalState parses a wire-format message and places the result in m.
59//
60// This method permits fine-grained control over the unmarshaler.
61// Most users should use Unmarshal instead.
62func (o UnmarshalOptions) UnmarshalState(m Message, in protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) {
63 return o.unmarshal(in.Buf, m)
64}
65
66func (o UnmarshalOptions) unmarshal(b []byte, message Message) (out protoiface.UnmarshalOutput, err error) {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070067 if o.Resolver == nil {
68 o.Resolver = protoregistry.GlobalTypes
69 }
Joe Tsai705acad2019-09-14 18:22:59 -070070 if !o.Merge {
Damien Neild30e5612020-01-22 10:28:16 -080071 Reset(message)
Joe Tsai705acad2019-09-14 18:22:59 -070072 }
Damien Neild30e5612020-01-22 10:28:16 -080073 allowPartial := o.AllowPartial
74 o.Merge = true
75 o.AllowPartial = true
76 m := message.ProtoReflect()
77 methods := protoMethods(m)
78 if methods != nil && methods.Unmarshal != nil &&
79 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
Damien Neil524c6062020-01-28 13:32:01 -080080 opts := protoiface.UnmarshalOptions{
81 Resolver: o.Resolver,
82 }
83 if o.DiscardUnknown {
84 opts.Flags |= protoiface.UnmarshalDiscardUnknown
85 }
Damien Neild30e5612020-01-22 10:28:16 -080086 out, err = methods.Unmarshal(m, protoiface.UnmarshalInput{
87 Buf: b,
Damien Neil524c6062020-01-28 13:32:01 -080088 }, opts)
Damien Neild30e5612020-01-22 10:28:16 -080089 } else {
90 err = o.unmarshalMessageSlow(b, m)
91 }
Damien Neil8c86fc52019-06-19 09:28:29 -070092 if err != nil {
Damien Neild30e5612020-01-22 10:28:16 -080093 return out, err
Damien Neil0d3e8cc2019-04-01 13:31:55 -070094 }
Damien Neilca6f40c2020-01-29 14:39:00 -080095 if allowPartial || out.Initialized {
Damien Neild30e5612020-01-22 10:28:16 -080096 return out, nil
Damien Neil4686e232019-04-05 13:31:40 -070097 }
Damien Neild30e5612020-01-22 10:28:16 -080098 return out, isInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080099}
100
Joe Tsai0f81b382019-07-10 23:14:31 -0700101func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Damien Neild30e5612020-01-22 10:28:16 -0800102 _, err := o.unmarshal(b, m.Interface())
103 return err
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700104}
105
Joe Tsai0f81b382019-07-10 23:14:31 -0700106func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Damien Neil302cb322019-06-19 15:22:13 -0700107 md := m.Descriptor()
108 if messageset.IsMessageSet(md) {
109 return unmarshalMessageSet(b, m, o)
110 }
111 fields := md.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -0800112 for len(b) > 0 {
113 // Parse the tag (field number and wire type).
114 num, wtyp, tagLen := wire.ConsumeTag(b)
115 if tagLen < 0 {
116 return wire.ParseError(tagLen)
117 }
Damien Neilfe15dd42019-12-06 15:36:03 -0800118 if num > wire.MaxValidNumber {
119 return errors.New("invalid field number")
120 }
Damien Neilba23aa52018-12-07 14:38:17 -0800121
Joe Tsai6e095992019-08-10 13:56:36 -0700122 // Find the field descriptor for this field number.
Damien Neil302cb322019-06-19 15:22:13 -0700123 fd := fields.ByNumber(num)
124 if fd == nil && md.ExtensionRanges().Has(num) {
125 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -0700126 if err != nil && err != protoregistry.NotFound {
Damien Neil01b51b42020-01-17 13:40:51 -0800127 return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
Joe Tsaidb38ddd2019-05-07 15:14:40 -0700128 }
Damien Neil92f76182019-08-02 16:58:08 -0700129 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -0700130 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -0700131 }
Damien Neild068d302018-12-17 14:06:08 -0800132 }
Damien Neilba23aa52018-12-07 14:38:17 -0800133 var err error
Joe Tsai6e095992019-08-10 13:56:36 -0700134 if fd == nil {
135 err = errUnknown
136 } else if flags.ProtoLegacy {
137 if fd.IsWeak() && fd.Message().IsPlaceholder() {
138 err = errUnknown // weak referent is not linked in
139 }
140 }
141
142 // Parse the field value.
Damien Neilba23aa52018-12-07 14:38:17 -0800143 var valLen int
144 switch {
Joe Tsai6e095992019-08-10 13:56:36 -0700145 case err != nil:
Joe Tsai378c1322019-04-25 23:48:08 -0700146 case fd.IsList():
147 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
148 case fd.IsMap():
149 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700150 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700151 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800152 }
Joe Tsai6e095992019-08-10 13:56:36 -0700153 if err != nil {
154 if err != errUnknown {
155 return err
156 }
Damien Neilba23aa52018-12-07 14:38:17 -0800157 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
158 if valLen < 0 {
159 return wire.ParseError(valLen)
160 }
Damien Neila60e7092020-01-28 14:53:44 -0800161 if !o.DiscardUnknown {
162 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
163 }
Damien Neilba23aa52018-12-07 14:38:17 -0800164 }
165 b = b[tagLen+valLen:]
166 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700167 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800168}
169
Joe Tsai378c1322019-04-25 23:48:08 -0700170func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp wire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
Joe Tsai378c1322019-04-25 23:48:08 -0700171 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700172 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800173 return 0, err
174 }
Joe Tsai378c1322019-04-25 23:48:08 -0700175 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800176 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700177 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700178 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700179 return n, err
180 }
Damien Neilba23aa52018-12-07 14:38:17 -0800181 default:
182 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700183 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800184 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700185 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800186}
187
Joe Tsai378c1322019-04-25 23:48:08 -0700188func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
Damien Neilba23aa52018-12-07 14:38:17 -0800189 if wtyp != wire.BytesType {
190 return 0, errUnknown
191 }
192 b, n = wire.ConsumeBytes(b)
193 if n < 0 {
194 return 0, wire.ParseError(n)
195 }
196 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700197 keyField = fd.MapKey()
198 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800199 key protoreflect.Value
200 val protoreflect.Value
201 haveKey bool
202 haveVal bool
203 )
204 switch valField.Kind() {
205 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700206 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800207 }
208 // Map entries are represented as a two-element message with fields
209 // containing the key and value.
210 for len(b) > 0 {
211 num, wtyp, n := wire.ConsumeTag(b)
212 if n < 0 {
213 return 0, wire.ParseError(n)
214 }
Damien Neilf2427c02019-12-20 09:43:20 -0800215 if num > wire.MaxValidNumber {
216 return 0, errors.New("invalid field number")
217 }
Damien Neilba23aa52018-12-07 14:38:17 -0800218 b = b[n:]
219 err = errUnknown
220 switch num {
221 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700222 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700223 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800224 break
225 }
226 haveKey = true
227 case 2:
228 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700229 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700230 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800231 break
232 }
233 switch valField.Kind() {
234 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700235 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800236 return 0, err
237 }
238 default:
239 val = v
240 }
241 haveVal = true
242 }
243 if err == errUnknown {
244 n = wire.ConsumeFieldValue(num, wtyp, b)
245 if n < 0 {
246 return 0, wire.ParseError(n)
247 }
Damien Neilbc310b52019-04-11 11:46:55 -0700248 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800249 return 0, err
250 }
251 b = b[n:]
252 }
253 // Every map entry should have entries for key and value, but this is not strictly required.
254 if !haveKey {
255 key = keyField.Default()
256 }
257 if !haveVal {
258 switch valField.Kind() {
259 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800260 default:
261 val = valField.Default()
262 }
263 }
264 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700265 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800266}
267
268// errUnknown is used internally to indicate fields which should be added
269// to the unknown field set of a message. It is never returned from an exported
270// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700271var errUnknown = errors.New("BUG: internal error (unknown)")