blob: ab62ff38bda8339d0a481181c4f1f60dc58030e7 [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"
11 "google.golang.org/protobuf/internal/pragma"
12 "google.golang.org/protobuf/reflect/protoreflect"
13 "google.golang.org/protobuf/reflect/protoregistry"
14 "google.golang.org/protobuf/runtime/protoiface"
Damien Neilba23aa52018-12-07 14:38:17 -080015)
16
17// UnmarshalOptions configures the unmarshaler.
18//
19// Example usage:
20// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
21type UnmarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070022 pragma.NoUnkeyedLiterals
23
Joe Tsai705acad2019-09-14 18:22:59 -070024 // Merge merges the input into the destination message.
25 // The default behavior is to always reset the message before unmarshaling,
26 // unless Merge is specified.
27 Merge bool
28
Damien Neil96c229a2019-04-03 12:17:24 -070029 // AllowPartial accepts input for messages that will result in missing
30 // required fields. If AllowPartial is false (the default), Unmarshal will
31 // return an error if there are any missing required fields.
32 AllowPartial bool
33
Damien Neilba23aa52018-12-07 14:38:17 -080034 // If DiscardUnknown is set, unknown fields are ignored.
35 DiscardUnknown bool
36
Joe Tsaidb38ddd2019-05-07 15:14:40 -070037 // Resolver is used for looking up types when unmarshaling extension fields.
38 // If nil, this defaults to using protoregistry.GlobalTypes.
Joe Tsai1c283042019-05-14 14:28:19 -070039 Resolver interface {
40 protoregistry.ExtensionTypeResolver
41 }
Damien Neilba23aa52018-12-07 14:38:17 -080042}
43
Damien Neil0d3e8cc2019-04-01 13:31:55 -070044var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
45
Damien Neilba23aa52018-12-07 14:38:17 -080046// Unmarshal parses the wire-format message in b and places the result in m.
47func Unmarshal(b []byte, m Message) error {
48 return UnmarshalOptions{}.Unmarshal(b, m)
49}
50
51// Unmarshal parses the wire-format message in b and places the result in m.
52func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070053 if o.Resolver == nil {
54 o.Resolver = protoregistry.GlobalTypes
55 }
56
Joe Tsai705acad2019-09-14 18:22:59 -070057 if !o.Merge {
58 Reset(m)
59 }
Joe Tsai0f81b382019-07-10 23:14:31 -070060 err := o.unmarshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070061 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070062 return err
63 }
Damien Neil8c86fc52019-06-19 09:28:29 -070064 if o.AllowPartial {
65 return nil
Damien Neil4686e232019-04-05 13:31:40 -070066 }
Damien Neil8c86fc52019-06-19 09:28:29 -070067 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080068}
69
Joe Tsai0f81b382019-07-10 23:14:31 -070070func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsaif8b855d2019-07-12 13:37:59 -070071 if methods := protoMethods(m); methods != nil && methods.Unmarshal != nil &&
72 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
Joe Tsai0f81b382019-07-10 23:14:31 -070073 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -070074 }
Joe Tsai0f81b382019-07-10 23:14:31 -070075 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070076}
77
Joe Tsai0f81b382019-07-10 23:14:31 -070078func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Damien Neil302cb322019-06-19 15:22:13 -070079 md := m.Descriptor()
80 if messageset.IsMessageSet(md) {
81 return unmarshalMessageSet(b, m, o)
82 }
83 fields := md.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080084 for len(b) > 0 {
85 // Parse the tag (field number and wire type).
86 num, wtyp, tagLen := wire.ConsumeTag(b)
87 if tagLen < 0 {
88 return wire.ParseError(tagLen)
89 }
90
91 // Parse the field value.
Damien Neil302cb322019-06-19 15:22:13 -070092 fd := fields.ByNumber(num)
93 if fd == nil && md.ExtensionRanges().Has(num) {
94 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
Joe Tsai378c1322019-04-25 23:48:08 -070095 if err != nil && err != protoregistry.NotFound {
96 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070097 }
Damien Neil92f76182019-08-02 16:58:08 -070098 if extType != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -070099 fd = extType.TypeDescriptor()
Damien Neil92f76182019-08-02 16:58:08 -0700100 }
Damien Neild068d302018-12-17 14:06:08 -0800101 }
Damien Neilba23aa52018-12-07 14:38:17 -0800102 var err error
103 var valLen int
104 switch {
Joe Tsai378c1322019-04-25 23:48:08 -0700105 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -0800106 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -0700107 case fd.IsList():
108 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
109 case fd.IsMap():
110 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700111 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700112 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800113 }
114 if err == errUnknown {
115 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
116 if valLen < 0 {
117 return wire.ParseError(valLen)
118 }
Joe Tsai378c1322019-04-25 23:48:08 -0700119 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700120 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800121 return err
122 }
123 b = b[tagLen+valLen:]
124 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700125 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800126}
127
Joe Tsai378c1322019-04-25 23:48:08 -0700128func (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 -0700129 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700130 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800131 return 0, err
132 }
Joe Tsai378c1322019-04-25 23:48:08 -0700133 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800134 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700135 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700136 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700137 return n, err
138 }
Damien Neilba23aa52018-12-07 14:38:17 -0800139 default:
140 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700141 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800142 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700143 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800144}
145
Joe Tsai378c1322019-04-25 23:48:08 -0700146func (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 -0800147 if wtyp != wire.BytesType {
148 return 0, errUnknown
149 }
150 b, n = wire.ConsumeBytes(b)
151 if n < 0 {
152 return 0, wire.ParseError(n)
153 }
154 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700155 keyField = fd.MapKey()
156 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800157 key protoreflect.Value
158 val protoreflect.Value
159 haveKey bool
160 haveVal bool
161 )
162 switch valField.Kind() {
163 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neild91c4222019-09-04 10:46:00 -0700164 val = mapv.NewValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800165 }
166 // Map entries are represented as a two-element message with fields
167 // containing the key and value.
168 for len(b) > 0 {
169 num, wtyp, n := wire.ConsumeTag(b)
170 if n < 0 {
171 return 0, wire.ParseError(n)
172 }
173 b = b[n:]
174 err = errUnknown
175 switch num {
176 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700177 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700178 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800179 break
180 }
181 haveKey = true
182 case 2:
183 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700184 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700185 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800186 break
187 }
188 switch valField.Kind() {
189 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700190 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800191 return 0, err
192 }
193 default:
194 val = v
195 }
196 haveVal = true
197 }
198 if err == errUnknown {
199 n = wire.ConsumeFieldValue(num, wtyp, b)
200 if n < 0 {
201 return 0, wire.ParseError(n)
202 }
Damien Neilbc310b52019-04-11 11:46:55 -0700203 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800204 return 0, err
205 }
206 b = b[n:]
207 }
208 // Every map entry should have entries for key and value, but this is not strictly required.
209 if !haveKey {
210 key = keyField.Default()
211 }
212 if !haveVal {
213 switch valField.Kind() {
214 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800215 default:
216 val = valField.Default()
217 }
218 }
219 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700220 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800221}
222
223// errUnknown is used internally to indicate fields which should be added
224// to the unknown field set of a message. It is never returned from an exported
225// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700226var errUnknown = errors.New("BUG: internal error (unknown)")