blob: f5b5808899278970ed00e06b8866a0d732966660 [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) {
Damien Neil61781dd2020-01-21 13:29:51 -080075 _, err := methods.Unmarshal(m, protoiface.UnmarshalInput{
76 Buf: b,
77 Options: protoiface.UnmarshalOptions(o),
78 })
79 return err
Damien Neil0d3e8cc2019-04-01 13:31:55 -070080 }
Joe Tsai0f81b382019-07-10 23:14:31 -070081 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070082}
83
Joe Tsai0f81b382019-07-10 23:14:31 -070084func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Damien Neil302cb322019-06-19 15:22:13 -070085 md := m.Descriptor()
86 if messageset.IsMessageSet(md) {
87 return unmarshalMessageSet(b, m, o)
88 }
89 fields := md.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080090 for len(b) > 0 {
91 // Parse the tag (field number and wire type).
92 num, wtyp, tagLen := wire.ConsumeTag(b)
93 if tagLen < 0 {
94 return wire.ParseError(tagLen)
95 }
Damien Neilfe15dd42019-12-06 15:36:03 -080096 if num > wire.MaxValidNumber {
97 return errors.New("invalid field number")
98 }
Damien Neilba23aa52018-12-07 14:38:17 -080099
Joe Tsai6e095992019-08-10 13:56:36 -0700100 // Find the field descriptor for this field number.
Damien Neil302cb322019-06-19 15:22:13 -0700101 fd := fields.ByNumber(num)
102 if fd == nil && md.ExtensionRanges().Has(num) {
103 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -0700104 if err != nil && err != protoregistry.NotFound {
105 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -0700106 }
Damien Neil92f76182019-08-02 16:58:08 -0700107 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -0700108 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -0700109 }
Damien Neild068d302018-12-17 14:06:08 -0800110 }
Damien Neilba23aa52018-12-07 14:38:17 -0800111 var err error
Joe Tsai6e095992019-08-10 13:56:36 -0700112 if fd == nil {
113 err = errUnknown
114 } else if flags.ProtoLegacy {
115 if fd.IsWeak() && fd.Message().IsPlaceholder() {
116 err = errUnknown // weak referent is not linked in
117 }
118 }
119
120 // Parse the field value.
Damien Neilba23aa52018-12-07 14:38:17 -0800121 var valLen int
122 switch {
Joe Tsai6e095992019-08-10 13:56:36 -0700123 case err != nil:
Joe Tsai378c1322019-04-25 23:48:08 -0700124 case fd.IsList():
125 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
126 case fd.IsMap():
127 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700128 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700129 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800130 }
Joe Tsai6e095992019-08-10 13:56:36 -0700131 if err != nil {
132 if err != errUnknown {
133 return err
134 }
Damien Neilba23aa52018-12-07 14:38:17 -0800135 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
136 if valLen < 0 {
137 return wire.ParseError(valLen)
138 }
Joe Tsai378c1322019-04-25 23:48:08 -0700139 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neilba23aa52018-12-07 14:38:17 -0800140 }
141 b = b[tagLen+valLen:]
142 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700143 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800144}
145
Joe Tsai378c1322019-04-25 23:48:08 -0700146func (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 -0700147 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700148 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800149 return 0, err
150 }
Joe Tsai378c1322019-04-25 23:48:08 -0700151 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800152 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700153 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700154 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700155 return n, err
156 }
Damien Neilba23aa52018-12-07 14:38:17 -0800157 default:
158 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700159 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800160 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700161 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800162}
163
Joe Tsai378c1322019-04-25 23:48:08 -0700164func (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 -0800165 if wtyp != wire.BytesType {
166 return 0, errUnknown
167 }
168 b, n = wire.ConsumeBytes(b)
169 if n < 0 {
170 return 0, wire.ParseError(n)
171 }
172 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700173 keyField = fd.MapKey()
174 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800175 key protoreflect.Value
176 val protoreflect.Value
177 haveKey bool
178 haveVal bool
179 )
180 switch valField.Kind() {
181 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700182 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800183 }
184 // Map entries are represented as a two-element message with fields
185 // containing the key and value.
186 for len(b) > 0 {
187 num, wtyp, n := wire.ConsumeTag(b)
188 if n < 0 {
189 return 0, wire.ParseError(n)
190 }
Damien Neilf2427c02019-12-20 09:43:20 -0800191 if num > wire.MaxValidNumber {
192 return 0, errors.New("invalid field number")
193 }
Damien Neilba23aa52018-12-07 14:38:17 -0800194 b = b[n:]
195 err = errUnknown
196 switch num {
197 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700198 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700199 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800200 break
201 }
202 haveKey = true
203 case 2:
204 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700205 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700206 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800207 break
208 }
209 switch valField.Kind() {
210 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700211 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800212 return 0, err
213 }
214 default:
215 val = v
216 }
217 haveVal = true
218 }
219 if err == errUnknown {
220 n = wire.ConsumeFieldValue(num, wtyp, b)
221 if n < 0 {
222 return 0, wire.ParseError(n)
223 }
Damien Neilbc310b52019-04-11 11:46:55 -0700224 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800225 return 0, err
226 }
227 b = b[n:]
228 }
229 // Every map entry should have entries for key and value, but this is not strictly required.
230 if !haveKey {
231 key = keyField.Default()
232 }
233 if !haveVal {
234 switch valField.Kind() {
235 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800236 default:
237 val = valField.Default()
238 }
239 }
240 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700241 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800242}
243
244// errUnknown is used internally to indicate fields which should be added
245// to the unknown field set of a message. It is never returned from an exported
246// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700247var errUnknown = errors.New("BUG: internal error (unknown)")