blob: 07ae4677a00f8099d7838b33d0f2a8cd0aa1a438 [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 }
Damien Neilfe15dd42019-12-06 15:36:03 -080091 if num > wire.MaxValidNumber {
92 return errors.New("invalid field number")
93 }
Damien Neilba23aa52018-12-07 14:38:17 -080094
Joe Tsai6e095992019-08-10 13:56:36 -070095 // Find the field descriptor for this field number.
Damien Neil302cb322019-06-19 15:22:13 -070096 fd := fields.ByNumber(num)
97 if fd == nil && md.ExtensionRanges().Has(num) {
98 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -070099 if err != nil && err != protoregistry.NotFound {
100 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -0700101 }
Damien Neil92f76182019-08-02 16:58:08 -0700102 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -0700103 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -0700104 }
Damien Neild068d302018-12-17 14:06:08 -0800105 }
Damien Neilba23aa52018-12-07 14:38:17 -0800106 var err error
Joe Tsai6e095992019-08-10 13:56:36 -0700107 if fd == nil {
108 err = errUnknown
109 } else if flags.ProtoLegacy {
110 if fd.IsWeak() && fd.Message().IsPlaceholder() {
111 err = errUnknown // weak referent is not linked in
112 }
113 }
114
115 // Parse the field value.
Damien Neilba23aa52018-12-07 14:38:17 -0800116 var valLen int
117 switch {
Joe Tsai6e095992019-08-10 13:56:36 -0700118 case err != nil:
Joe Tsai378c1322019-04-25 23:48:08 -0700119 case fd.IsList():
120 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
121 case fd.IsMap():
122 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700123 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700124 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800125 }
Joe Tsai6e095992019-08-10 13:56:36 -0700126 if err != nil {
127 if err != errUnknown {
128 return err
129 }
Damien Neilba23aa52018-12-07 14:38:17 -0800130 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
131 if valLen < 0 {
132 return wire.ParseError(valLen)
133 }
Joe Tsai378c1322019-04-25 23:48:08 -0700134 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neilba23aa52018-12-07 14:38:17 -0800135 }
136 b = b[tagLen+valLen:]
137 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700138 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800139}
140
Joe Tsai378c1322019-04-25 23:48:08 -0700141func (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 -0700142 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700143 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800144 return 0, err
145 }
Joe Tsai378c1322019-04-25 23:48:08 -0700146 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800147 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700148 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700149 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700150 return n, err
151 }
Damien Neilba23aa52018-12-07 14:38:17 -0800152 default:
153 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700154 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800155 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700156 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800157}
158
Joe Tsai378c1322019-04-25 23:48:08 -0700159func (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 -0800160 if wtyp != wire.BytesType {
161 return 0, errUnknown
162 }
163 b, n = wire.ConsumeBytes(b)
164 if n < 0 {
165 return 0, wire.ParseError(n)
166 }
167 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700168 keyField = fd.MapKey()
169 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800170 key protoreflect.Value
171 val protoreflect.Value
172 haveKey bool
173 haveVal bool
174 )
175 switch valField.Kind() {
176 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700177 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800178 }
179 // Map entries are represented as a two-element message with fields
180 // containing the key and value.
181 for len(b) > 0 {
182 num, wtyp, n := wire.ConsumeTag(b)
183 if n < 0 {
184 return 0, wire.ParseError(n)
185 }
186 b = b[n:]
187 err = errUnknown
188 switch num {
189 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700190 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700191 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800192 break
193 }
194 haveKey = true
195 case 2:
196 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700197 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700198 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800199 break
200 }
201 switch valField.Kind() {
202 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700203 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800204 return 0, err
205 }
206 default:
207 val = v
208 }
209 haveVal = true
210 }
211 if err == errUnknown {
212 n = wire.ConsumeFieldValue(num, wtyp, b)
213 if n < 0 {
214 return 0, wire.ParseError(n)
215 }
Damien Neilbc310b52019-04-11 11:46:55 -0700216 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800217 return 0, err
218 }
219 b = b[n:]
220 }
221 // Every map entry should have entries for key and value, but this is not strictly required.
222 if !haveKey {
223 key = keyField.Default()
224 }
225 if !haveVal {
226 switch valField.Kind() {
227 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800228 default:
229 val = valField.Default()
230 }
231 }
232 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700233 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800234}
235
236// errUnknown is used internally to indicate fields which should be added
237// to the unknown field set of a message. It is never returned from an exported
238// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700239var errUnknown = errors.New("BUG: internal error (unknown)")