blob: cea434d30fcc1cc3c013027b6680e1ff8dc8619b [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
Damien Neil0d3e8cc2019-04-01 13:31:55 -070046var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
47
Damien Neilba23aa52018-12-07 14:38:17 -080048// Unmarshal parses the wire-format message in b and places the result in m.
49func Unmarshal(b []byte, m Message) error {
50 return UnmarshalOptions{}.Unmarshal(b, m)
51}
52
53// Unmarshal parses the wire-format message in b and places the result in m.
54func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070055 if o.Resolver == nil {
56 o.Resolver = protoregistry.GlobalTypes
57 }
58
Joe Tsai705acad2019-09-14 18:22:59 -070059 if !o.Merge {
60 Reset(m)
61 }
Joe Tsai0f81b382019-07-10 23:14:31 -070062 err := o.unmarshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070063 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070064 return err
65 }
Damien Neil8c86fc52019-06-19 09:28:29 -070066 if o.AllowPartial {
67 return nil
Damien Neil4686e232019-04-05 13:31:40 -070068 }
Damien Neil8c86fc52019-06-19 09:28:29 -070069 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080070}
71
Joe Tsai0f81b382019-07-10 23:14:31 -070072func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsaif8b855d2019-07-12 13:37:59 -070073 if methods := protoMethods(m); methods != nil && methods.Unmarshal != nil &&
74 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
Joe Tsai0f81b382019-07-10 23:14:31 -070075 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -070076 }
Joe Tsai0f81b382019-07-10 23:14:31 -070077 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070078}
79
Joe Tsai0f81b382019-07-10 23:14:31 -070080func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Damien Neil302cb322019-06-19 15:22:13 -070081 md := m.Descriptor()
82 if messageset.IsMessageSet(md) {
83 return unmarshalMessageSet(b, m, o)
84 }
85 fields := md.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080086 for len(b) > 0 {
87 // Parse the tag (field number and wire type).
88 num, wtyp, tagLen := wire.ConsumeTag(b)
89 if tagLen < 0 {
90 return wire.ParseError(tagLen)
91 }
Damien Neilfe15dd42019-12-06 15:36:03 -080092 if num > wire.MaxValidNumber {
93 return errors.New("invalid field number")
94 }
Damien Neilba23aa52018-12-07 14:38:17 -080095
Joe Tsai6e095992019-08-10 13:56:36 -070096 // Find the field descriptor for this field number.
Damien Neil302cb322019-06-19 15:22:13 -070097 fd := fields.ByNumber(num)
98 if fd == nil && md.ExtensionRanges().Has(num) {
99 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -0700100 if err != nil && err != protoregistry.NotFound {
101 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -0700102 }
Damien Neil92f76182019-08-02 16:58:08 -0700103 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -0700104 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -0700105 }
Damien Neild068d302018-12-17 14:06:08 -0800106 }
Damien Neilba23aa52018-12-07 14:38:17 -0800107 var err error
Joe Tsai6e095992019-08-10 13:56:36 -0700108 if fd == nil {
109 err = errUnknown
110 } else if flags.ProtoLegacy {
111 if fd.IsWeak() && fd.Message().IsPlaceholder() {
112 err = errUnknown // weak referent is not linked in
113 }
114 }
115
116 // Parse the field value.
Damien Neilba23aa52018-12-07 14:38:17 -0800117 var valLen int
118 switch {
Joe Tsai6e095992019-08-10 13:56:36 -0700119 case err != nil:
Joe Tsai378c1322019-04-25 23:48:08 -0700120 case fd.IsList():
121 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
122 case fd.IsMap():
123 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700124 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700125 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800126 }
Joe Tsai6e095992019-08-10 13:56:36 -0700127 if err != nil {
128 if err != errUnknown {
129 return err
130 }
Damien Neilba23aa52018-12-07 14:38:17 -0800131 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
132 if valLen < 0 {
133 return wire.ParseError(valLen)
134 }
Joe Tsai378c1322019-04-25 23:48:08 -0700135 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neilba23aa52018-12-07 14:38:17 -0800136 }
137 b = b[tagLen+valLen:]
138 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700139 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800140}
141
Joe Tsai378c1322019-04-25 23:48:08 -0700142func (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 -0700143 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700144 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800145 return 0, err
146 }
Joe Tsai378c1322019-04-25 23:48:08 -0700147 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800148 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700149 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700150 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700151 return n, err
152 }
Damien Neilba23aa52018-12-07 14:38:17 -0800153 default:
154 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700155 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800156 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700157 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800158}
159
Joe Tsai378c1322019-04-25 23:48:08 -0700160func (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 -0800161 if wtyp != wire.BytesType {
162 return 0, errUnknown
163 }
164 b, n = wire.ConsumeBytes(b)
165 if n < 0 {
166 return 0, wire.ParseError(n)
167 }
168 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700169 keyField = fd.MapKey()
170 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800171 key protoreflect.Value
172 val protoreflect.Value
173 haveKey bool
174 haveVal bool
175 )
176 switch valField.Kind() {
177 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700178 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800179 }
180 // Map entries are represented as a two-element message with fields
181 // containing the key and value.
182 for len(b) > 0 {
183 num, wtyp, n := wire.ConsumeTag(b)
184 if n < 0 {
185 return 0, wire.ParseError(n)
186 }
Damien Neilf2427c02019-12-20 09:43:20 -0800187 if num > wire.MaxValidNumber {
188 return 0, errors.New("invalid field number")
189 }
Damien Neilba23aa52018-12-07 14:38:17 -0800190 b = b[n:]
191 err = errUnknown
192 switch num {
193 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700194 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700195 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800196 break
197 }
198 haveKey = true
199 case 2:
200 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700201 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700202 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800203 break
204 }
205 switch valField.Kind() {
206 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700207 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800208 return 0, err
209 }
210 default:
211 val = v
212 }
213 haveVal = true
214 }
215 if err == errUnknown {
216 n = wire.ConsumeFieldValue(num, wtyp, b)
217 if n < 0 {
218 return 0, wire.ParseError(n)
219 }
Damien Neilbc310b52019-04-11 11:46:55 -0700220 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800221 return 0, err
222 }
223 b = b[n:]
224 }
225 // Every map entry should have entries for key and value, but this is not strictly required.
226 if !haveKey {
227 key = keyField.Default()
228 }
229 if !haveVal {
230 switch valField.Kind() {
231 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800232 default:
233 val = valField.Default()
234 }
235 }
236 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700237 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800238}
239
240// errUnknown is used internally to indicate fields which should be added
241// to the unknown field set of a message. It is never returned from an exported
242// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700243var errUnknown = errors.New("BUG: internal error (unknown)")