blob: 380d903b38278356e91a2c06eb8fb1e71bda8be3 [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 {
Damien Neil96c229a2019-04-03 12:17:24 -070021 // AllowPartial accepts input for messages that will result in missing
22 // required fields. If AllowPartial is false (the default), Unmarshal will
23 // return an error if there are any missing required fields.
24 AllowPartial bool
25
Damien Neilba23aa52018-12-07 14:38:17 -080026 // If DiscardUnknown is set, unknown fields are ignored.
27 DiscardUnknown bool
28
Joe Tsaidb38ddd2019-05-07 15:14:40 -070029 // Resolver is used for looking up types when unmarshaling extension fields.
30 // If nil, this defaults to using protoregistry.GlobalTypes.
Joe Tsai1c283042019-05-14 14:28:19 -070031 Resolver interface {
32 protoregistry.ExtensionTypeResolver
33 }
Joe Tsaidb38ddd2019-05-07 15:14:40 -070034
Damien Neilba23aa52018-12-07 14:38:17 -080035 pragma.NoUnkeyedLiterals
36}
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?
Damien Neil4686e232019-04-05 13:31:40 -070052 err := o.unmarshalMessageFast(b, m)
53 if err == errInternalNoFast {
54 err = o.unmarshalMessage(b, m.ProtoReflect())
55 }
Damien Neil8c86fc52019-06-19 09:28:29 -070056 if err != nil {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070057 return err
58 }
Damien Neil8c86fc52019-06-19 09:28:29 -070059 if o.AllowPartial {
60 return nil
Damien Neil4686e232019-04-05 13:31:40 -070061 }
Damien Neil8c86fc52019-06-19 09:28:29 -070062 return IsInitialized(m)
Damien Neilba23aa52018-12-07 14:38:17 -080063}
64
Damien Neil0d3e8cc2019-04-01 13:31:55 -070065func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070066 methods := protoMethods(m)
67 if methods == nil || methods.Unmarshal == nil {
68 return errInternalNoFast
69 }
70 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
71}
72
Damien Neilba23aa52018-12-07 14:38:17 -080073func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070074 messageDesc := m.Descriptor()
75 fieldDescs := messageDesc.Fields()
Damien Neilba23aa52018-12-07 14:38:17 -080076 for len(b) > 0 {
77 // Parse the tag (field number and wire type).
78 num, wtyp, tagLen := wire.ConsumeTag(b)
79 if tagLen < 0 {
80 return wire.ParseError(tagLen)
81 }
82
83 // Parse the field value.
Joe Tsai378c1322019-04-25 23:48:08 -070084 fd := fieldDescs.ByNumber(num)
85 if fd == nil && messageDesc.ExtensionRanges().Has(num) {
86 extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
87 if err != nil && err != protoregistry.NotFound {
88 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070089 }
Joe Tsai378c1322019-04-25 23:48:08 -070090 fd = extType
Damien Neild068d302018-12-17 14:06:08 -080091 }
Damien Neilba23aa52018-12-07 14:38:17 -080092 var err error
93 var valLen int
94 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070095 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080096 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -070097 case fd.IsList():
98 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
99 case fd.IsMap():
100 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700101 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700102 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800103 }
104 if err == errUnknown {
105 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
106 if valLen < 0 {
107 return wire.ParseError(valLen)
108 }
Joe Tsai378c1322019-04-25 23:48:08 -0700109 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil8c86fc52019-06-19 09:28:29 -0700110 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800111 return err
112 }
113 b = b[tagLen+valLen:]
114 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700115 return nil
Damien Neilba23aa52018-12-07 14:38:17 -0800116}
117
Joe Tsai378c1322019-04-25 23:48:08 -0700118func (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 -0700119 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700120 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800121 return 0, err
122 }
Joe Tsai378c1322019-04-25 23:48:08 -0700123 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800124 case protoreflect.GroupKind, protoreflect.MessageKind:
125 // Messages are merged with any existing message value,
126 // unless the message is part of a oneof.
127 //
128 // TODO: C++ merges into oneofs, while v1 does not.
129 // Evaluate which behavior to pick.
Joe Tsai378c1322019-04-25 23:48:08 -0700130 var m2 protoreflect.Message
131 if m.Has(fd) && fd.ContainingOneof() == nil {
132 m2 = m.Mutable(fd).Message()
Damien Neilba23aa52018-12-07 14:38:17 -0800133 } else {
Joe Tsai378c1322019-04-25 23:48:08 -0700134 m2 = m.NewMessage(fd)
135 m.Set(fd, protoreflect.ValueOf(m2))
Damien Neilba23aa52018-12-07 14:38:17 -0800136 }
Damien Neil96c229a2019-04-03 12:17:24 -0700137 // Pass up errors (fatal and otherwise).
Damien Neil8c86fc52019-06-19 09:28:29 -0700138 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
Damien Neilbc310b52019-04-11 11:46:55 -0700139 return n, err
140 }
Damien Neilba23aa52018-12-07 14:38:17 -0800141 default:
142 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700143 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800144 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700145 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800146}
147
Joe Tsai378c1322019-04-25 23:48:08 -0700148func (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 -0800149 if wtyp != wire.BytesType {
150 return 0, errUnknown
151 }
152 b, n = wire.ConsumeBytes(b)
153 if n < 0 {
154 return 0, wire.ParseError(n)
155 }
156 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700157 keyField = fd.MapKey()
158 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800159 key protoreflect.Value
160 val protoreflect.Value
161 haveKey bool
162 haveVal bool
163 )
164 switch valField.Kind() {
165 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800166 val = protoreflect.ValueOf(mapv.NewMessage())
Damien Neilba23aa52018-12-07 14:38:17 -0800167 }
168 // Map entries are represented as a two-element message with fields
169 // containing the key and value.
170 for len(b) > 0 {
171 num, wtyp, n := wire.ConsumeTag(b)
172 if n < 0 {
173 return 0, wire.ParseError(n)
174 }
175 b = b[n:]
176 err = errUnknown
177 switch num {
178 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700179 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700180 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800181 break
182 }
183 haveKey = true
184 case 2:
185 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700186 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neil8c86fc52019-06-19 09:28:29 -0700187 if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800188 break
189 }
190 switch valField.Kind() {
191 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil8c86fc52019-06-19 09:28:29 -0700192 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800193 return 0, err
194 }
195 default:
196 val = v
197 }
198 haveVal = true
199 }
200 if err == errUnknown {
201 n = wire.ConsumeFieldValue(num, wtyp, b)
202 if n < 0 {
203 return 0, wire.ParseError(n)
204 }
Damien Neilbc310b52019-04-11 11:46:55 -0700205 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800206 return 0, err
207 }
208 b = b[n:]
209 }
210 // Every map entry should have entries for key and value, but this is not strictly required.
211 if !haveKey {
212 key = keyField.Default()
213 }
214 if !haveVal {
215 switch valField.Kind() {
216 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800217 default:
218 val = valField.Default()
219 }
220 }
221 mapv.Set(key.MapKey(), val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700222 return n, nil
Damien Neilba23aa52018-12-07 14:38:17 -0800223}
224
225// errUnknown is used internally to indicate fields which should be added
226// to the unknown field set of a message. It is never returned from an exported
227// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700228var errUnknown = errors.New("BUG: internal error (unknown)")