blob: f64f887349dc4ac7ff7189499bd50af7c7aa5ddd [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 {
41 protoregistry.ExtensionTypeResolver
42 }
Damien Neilba23aa52018-12-07 14:38:17 -080043}
44
Damien Neil0d3e8cc2019-04-01 13:31:55 -070045var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
46
Damien Neilba23aa52018-12-07 14:38:17 -080047// Unmarshal parses the wire-format message in b and places the result in m.
48func Unmarshal(b []byte, m Message) error {
49 return UnmarshalOptions{}.Unmarshal(b, m)
50}
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 {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070054 if o.Resolver == nil {
55 o.Resolver = protoregistry.GlobalTypes
56 }
57
Joe Tsai705acad2019-09-14 18:22:59 -070058 if !o.Merge {
59 Reset(m)
60 }
Joe Tsai0f81b382019-07-10 23:14:31 -070061 err := o.unmarshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070062 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070063 return err
64 }
Damien Neil8c86fc52019-06-19 09:28:29 -070065 if o.AllowPartial {
66 return nil
Damien Neil4686e232019-04-05 13:31:40 -070067 }
Damien Neil8c86fc52019-06-19 09:28:29 -070068 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080069}
70
Joe Tsai0f81b382019-07-10 23:14:31 -070071func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsaif8b855d2019-07-12 13:37:59 -070072 if methods := protoMethods(m); methods != nil && methods.Unmarshal != nil &&
73 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
Joe Tsai0f81b382019-07-10 23:14:31 -070074 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -070075 }
Joe Tsai0f81b382019-07-10 23:14:31 -070076 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070077}
78
Joe Tsai0f81b382019-07-10 23:14:31 -070079func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Damien Neil302cb322019-06-19 15:22:13 -070080 md := m.Descriptor()
81 if messageset.IsMessageSet(md) {
82 return unmarshalMessageSet(b, m, o)
83 }
84 fields := md.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080085 for len(b) > 0 {
86 // Parse the tag (field number and wire type).
87 num, wtyp, tagLen := wire.ConsumeTag(b)
88 if tagLen < 0 {
89 return wire.ParseError(tagLen)
90 }
91
Joe Tsai6e095992019-08-10 13:56:36 -070092 // Find the field descriptor for this field number.
Damien Neil302cb322019-06-19 15:22:13 -070093 fd := fields.ByNumber(num)
94 if fd == nil && md.ExtensionRanges().Has(num) {
95 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -070096 if err != nil && err != protoregistry.NotFound {
97 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070098 }
Damien Neil92f76182019-08-02 16:58:08 -070099 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -0700100 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -0700101 }
Damien Neild068d302018-12-17 14:06:08 -0800102 }
Damien Neilba23aa52018-12-07 14:38:17 -0800103 var err error
Joe Tsai6e095992019-08-10 13:56:36 -0700104 if fd == nil {
105 err = errUnknown
106 } else if flags.ProtoLegacy {
107 if fd.IsWeak() && fd.Message().IsPlaceholder() {
108 err = errUnknown // weak referent is not linked in
109 }
110 }
111
112 // Parse the field value.
Damien Neilba23aa52018-12-07 14:38:17 -0800113 var valLen int
114 switch {
Joe Tsai6e095992019-08-10 13:56:36 -0700115 case err != nil:
Joe Tsai378c1322019-04-25 23:48:08 -0700116 case fd.IsList():
117 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
118 case fd.IsMap():
119 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700120 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700121 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800122 }
Joe Tsai6e095992019-08-10 13:56:36 -0700123 if err != nil {
124 if err != errUnknown {
125 return err
126 }
Damien Neilba23aa52018-12-07 14:38:17 -0800127 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
128 if valLen < 0 {
129 return wire.ParseError(valLen)
130 }
Joe Tsai378c1322019-04-25 23:48:08 -0700131 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neilba23aa52018-12-07 14:38:17 -0800132 }
133 b = b[tagLen+valLen:]
134 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700135 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800136}
137
Joe Tsai378c1322019-04-25 23:48:08 -0700138func (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 -0700139 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700140 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800141 return 0, err
142 }
Joe Tsai378c1322019-04-25 23:48:08 -0700143 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800144 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700145 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700146 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700147 return n, err
148 }
Damien Neilba23aa52018-12-07 14:38:17 -0800149 default:
150 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700151 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800152 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700153 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800154}
155
Joe Tsai378c1322019-04-25 23:48:08 -0700156func (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 -0800157 if wtyp != wire.BytesType {
158 return 0, errUnknown
159 }
160 b, n = wire.ConsumeBytes(b)
161 if n < 0 {
162 return 0, wire.ParseError(n)
163 }
164 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700165 keyField = fd.MapKey()
166 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800167 key protoreflect.Value
168 val protoreflect.Value
169 haveKey bool
170 haveVal bool
171 )
172 switch valField.Kind() {
173 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700174 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800175 }
176 // Map entries are represented as a two-element message with fields
177 // containing the key and value.
178 for len(b) > 0 {
179 num, wtyp, n := wire.ConsumeTag(b)
180 if n < 0 {
181 return 0, wire.ParseError(n)
182 }
183 b = b[n:]
184 err = errUnknown
185 switch num {
186 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700187 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700188 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800189 break
190 }
191 haveKey = true
192 case 2:
193 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700194 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700195 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800196 break
197 }
198 switch valField.Kind() {
199 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700200 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800201 return 0, err
202 }
203 default:
204 val = v
205 }
206 haveVal = true
207 }
208 if err == errUnknown {
209 n = wire.ConsumeFieldValue(num, wtyp, b)
210 if n < 0 {
211 return 0, wire.ParseError(n)
212 }
Damien Neilbc310b52019-04-11 11:46:55 -0700213 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800214 return 0, err
215 }
216 b = b[n:]
217 }
218 // Every map entry should have entries for key and value, but this is not strictly required.
219 if !haveKey {
220 key = keyField.Default()
221 }
222 if !haveVal {
223 switch valField.Kind() {
224 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800225 default:
226 val = valField.Default()
227 }
228 }
229 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700230 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800231}
232
233// errUnknown is used internally to indicate fields which should be added
234// to the unknown field set of a message. It is never returned from an exported
235// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700236var errUnknown = errors.New("BUG: internal error (unknown)")