blob: 0b98366ab5cb239790e5bb2ead138371efa17349 [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 Neile89e6242019-05-13 23:55:40 -07008 "google.golang.org/protobuf/internal/encoding/wire"
9 "google.golang.org/protobuf/internal/errors"
10 "google.golang.org/protobuf/internal/pragma"
11 "google.golang.org/protobuf/reflect/protoreflect"
12 "google.golang.org/protobuf/reflect/protoregistry"
13 "google.golang.org/protobuf/runtime/protoiface"
Damien Neilba23aa52018-12-07 14:38:17 -080014)
15
16// UnmarshalOptions configures the unmarshaler.
17//
18// Example usage:
19// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
20type UnmarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070021 pragma.NoUnkeyedLiterals
22
Damien Neil96c229a2019-04-03 12:17:24 -070023 // AllowPartial accepts input for messages that will result in missing
24 // required fields. If AllowPartial is false (the default), Unmarshal will
25 // return an error if there are any missing required fields.
26 AllowPartial bool
27
Damien Neilba23aa52018-12-07 14:38:17 -080028 // If DiscardUnknown is set, unknown fields are ignored.
29 DiscardUnknown bool
30
Joe Tsaidb38ddd2019-05-07 15:14:40 -070031 // Resolver is used for looking up types when unmarshaling extension fields.
32 // If nil, this defaults to using protoregistry.GlobalTypes.
Joe Tsai1c283042019-05-14 14:28:19 -070033 Resolver interface {
34 protoregistry.ExtensionTypeResolver
35 }
Damien Neilba23aa52018-12-07 14:38:17 -080036}
37
Damien Neil0d3e8cc2019-04-01 13:31:55 -070038var _ = protoiface.UnmarshalOptions(UnmarshalOptions{})
39
Damien Neilba23aa52018-12-07 14:38:17 -080040// Unmarshal parses the wire-format message in b and places the result in m.
41func Unmarshal(b []byte, m Message) error {
42 return UnmarshalOptions{}.Unmarshal(b, m)
43}
44
45// Unmarshal parses the wire-format message in b and places the result in m.
46func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
Joe Tsaidb38ddd2019-05-07 15:14:40 -070047 if o.Resolver == nil {
48 o.Resolver = protoregistry.GlobalTypes
49 }
50
Damien Neilba23aa52018-12-07 14:38:17 -080051 // TODO: Reset m?
Joe Tsai0f81b382019-07-10 23:14:31 -070052 err := o.unmarshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070053 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070054 return err
55 }
Damien Neil8c86fc52019-06-19 09:28:29 -070056 if o.AllowPartial {
57 return nil
Damien Neil4686e232019-04-05 13:31:40 -070058 }
Damien Neil8c86fc52019-06-19 09:28:29 -070059 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080060}
61
Joe Tsai0f81b382019-07-10 23:14:31 -070062func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsaif8b855d2019-07-12 13:37:59 -070063 if methods := protoMethods(m); methods != nil && methods.Unmarshal != nil &&
64 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
Joe Tsai0f81b382019-07-10 23:14:31 -070065 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -070066 }
Joe Tsai0f81b382019-07-10 23:14:31 -070067 return o.unmarshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -070068}
69
Joe Tsai0f81b382019-07-10 23:14:31 -070070func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070071 messageDesc := m.Descriptor()
72 fieldDescs := messageDesc.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080073 for len(b) > 0 {
74 // Parse the tag (field number and wire type).
75 num, wtyp, tagLen := wire.ConsumeTag(b)
76 if tagLen < 0 {
77 return wire.ParseError(tagLen)
78 }
79
80 // Parse the field value.
Joe Tsai378c1322019-04-25 23:48:08 -070081 fd := fieldDescs.ByNumber(num)
82 if fd == nil && messageDesc.ExtensionRanges().Has(num) {
83 extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
84 if err != nil && err != protoregistry.NotFound {
85 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070086 }
Joe Tsai378c1322019-04-25 23:48:08 -070087 fd = extType
Damien Neild068d302018-12-17 14:06:08 -080088 }
Damien Neilba23aa52018-12-07 14:38:17 -080089 var err error
90 var valLen int
91 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070092 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080093 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -070094 case fd.IsList():
95 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
96 case fd.IsMap():
97 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -070098 default:
Joe Tsai378c1322019-04-25 23:48:08 -070099 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800100 }
101 if err == errUnknown {
102 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
103 if valLen < 0 {
104 return wire.ParseError(valLen)
105 }
Joe Tsai378c1322019-04-25 23:48:08 -0700106 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700107 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800108 return err
109 }
110 b = b[tagLen+valLen:]
111 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700112 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800113}
114
Joe Tsai378c1322019-04-25 23:48:08 -0700115func (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 -0700116 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700117 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800118 return 0, err
119 }
Joe Tsai378c1322019-04-25 23:48:08 -0700120 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800121 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai6c286742019-07-11 23:15:05 -0700122 m2 := m.Mutable(fd).Message()
Damien Neil8c86fc52019-06-19 09:28:29 -0700123 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700124 return n, err
125 }
Damien Neilba23aa52018-12-07 14:38:17 -0800126 default:
127 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700128 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800129 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700130 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800131}
132
Joe Tsai378c1322019-04-25 23:48:08 -0700133func (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 -0800134 if wtyp != wire.BytesType {
135 return 0, errUnknown
136 }
137 b, n = wire.ConsumeBytes(b)
138 if n < 0 {
139 return 0, wire.ParseError(n)
140 }
141 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700142 keyField = fd.MapKey()
143 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800144 key protoreflect.Value
145 val protoreflect.Value
146 haveKey bool
147 haveVal bool
148 )
149 switch valField.Kind() {
150 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800151 val = protoreflect.ValueOf(mapv.NewMessage())
Damien Neilba23aa52018-12-07 14:38:17 -0800152 }
153 // Map entries are represented as a two-element message with fields
154 // containing the key and value.
155 for len(b) > 0 {
156 num, wtyp, n := wire.ConsumeTag(b)
157 if n < 0 {
158 return 0, wire.ParseError(n)
159 }
160 b = b[n:]
161 err = errUnknown
162 switch num {
163 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700164 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700165 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800166 break
167 }
168 haveKey = true
169 case 2:
170 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700171 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700172 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800173 break
174 }
175 switch valField.Kind() {
176 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700177 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800178 return 0, err
179 }
180 default:
181 val = v
182 }
183 haveVal = true
184 }
185 if err == errUnknown {
186 n = wire.ConsumeFieldValue(num, wtyp, b)
187 if n < 0 {
188 return 0, wire.ParseError(n)
189 }
Damien Neilbc310b52019-04-11 11:46:55 -0700190 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800191 return 0, err
192 }
193 b = b[n:]
194 }
195 // Every map entry should have entries for key and value, but this is not strictly required.
196 if !haveKey {
197 key = keyField.Default()
198 }
199 if !haveVal {
200 switch valField.Kind() {
201 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800202 default:
203 val = valField.Default()
204 }
205 }
206 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700207 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800208}
209
210// errUnknown is used internally to indicate fields which should be added
211// to the unknown field set of a message. It is never returned from an exported
212// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700213var errUnknown = errors.New("BUG: internal error (unknown)")