blob: d6148127561cd2cc83a1c8f43cb83dc4fbfca3bc [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 }
56 var nerr errors.NonFatal
57 if !nerr.Merge(err) {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070058 return err
59 }
Damien Neil4686e232019-04-05 13:31:40 -070060 if !o.AllowPartial {
61 nerr.Merge(IsInitialized(m))
62 }
63 return nerr.E
Damien Neilba23aa52018-12-07 14:38:17 -080064}
65
Damien Neil0d3e8cc2019-04-01 13:31:55 -070066func (o UnmarshalOptions) unmarshalMessageFast(b []byte, m Message) error {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070067 methods := protoMethods(m)
68 if methods == nil || methods.Unmarshal == nil {
69 return errInternalNoFast
70 }
71 return methods.Unmarshal(b, m, protoiface.UnmarshalOptions(o))
72}
73
Damien Neilba23aa52018-12-07 14:38:17 -080074func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070075 messageDesc := m.Descriptor()
76 fieldDescs := messageDesc.Fields()
Damien Neil96c229a2019-04-03 12:17:24 -070077 var nerr errors.NonFatal
Damien Neilba23aa52018-12-07 14:38:17 -080078 for len(b) > 0 {
79 // Parse the tag (field number and wire type).
80 num, wtyp, tagLen := wire.ConsumeTag(b)
81 if tagLen < 0 {
82 return wire.ParseError(tagLen)
83 }
84
85 // Parse the field value.
Joe Tsai378c1322019-04-25 23:48:08 -070086 fd := fieldDescs.ByNumber(num)
87 if fd == nil && messageDesc.ExtensionRanges().Has(num) {
88 extType, err := o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
89 if err != nil && err != protoregistry.NotFound {
90 return err
Joe Tsaidb38ddd2019-05-07 15:14:40 -070091 }
Joe Tsai378c1322019-04-25 23:48:08 -070092 fd = extType
Damien Neild068d302018-12-17 14:06:08 -080093 }
Damien Neilba23aa52018-12-07 14:38:17 -080094 var err error
95 var valLen int
96 switch {
Joe Tsai378c1322019-04-25 23:48:08 -070097 case fd == nil:
Damien Neilba23aa52018-12-07 14:38:17 -080098 err = errUnknown
Joe Tsai378c1322019-04-25 23:48:08 -070099 case fd.IsList():
100 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
101 case fd.IsMap():
102 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
Joe Tsaiac31a352019-05-13 14:32:56 -0700103 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700104 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
Damien Neilba23aa52018-12-07 14:38:17 -0800105 }
106 if err == errUnknown {
107 valLen = wire.ConsumeFieldValue(num, wtyp, b[tagLen:])
108 if valLen < 0 {
109 return wire.ParseError(valLen)
110 }
Joe Tsai378c1322019-04-25 23:48:08 -0700111 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
Damien Neil96c229a2019-04-03 12:17:24 -0700112 } else if !nerr.Merge(err) {
Damien Neilba23aa52018-12-07 14:38:17 -0800113 return err
114 }
115 b = b[tagLen+valLen:]
116 }
Damien Neil96c229a2019-04-03 12:17:24 -0700117 return nerr.E
Damien Neilba23aa52018-12-07 14:38:17 -0800118}
119
Joe Tsai378c1322019-04-25 23:48:08 -0700120func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp wire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
Damien Neilbc310b52019-04-11 11:46:55 -0700121 var nerr errors.NonFatal
Joe Tsai378c1322019-04-25 23:48:08 -0700122 v, n, err := o.unmarshalScalar(b, wtyp, fd)
Damien Neilbc310b52019-04-11 11:46:55 -0700123 if !nerr.Merge(err) {
Damien Neilba23aa52018-12-07 14:38:17 -0800124 return 0, err
125 }
Joe Tsai378c1322019-04-25 23:48:08 -0700126 switch fd.Kind() {
Damien Neilba23aa52018-12-07 14:38:17 -0800127 case protoreflect.GroupKind, protoreflect.MessageKind:
128 // Messages are merged with any existing message value,
129 // unless the message is part of a oneof.
130 //
131 // TODO: C++ merges into oneofs, while v1 does not.
132 // Evaluate which behavior to pick.
Joe Tsai378c1322019-04-25 23:48:08 -0700133 var m2 protoreflect.Message
134 if m.Has(fd) && fd.ContainingOneof() == nil {
135 m2 = m.Mutable(fd).Message()
Damien Neilba23aa52018-12-07 14:38:17 -0800136 } else {
Joe Tsai378c1322019-04-25 23:48:08 -0700137 m2 = m.NewMessage(fd)
138 m.Set(fd, protoreflect.ValueOf(m2))
Damien Neilba23aa52018-12-07 14:38:17 -0800139 }
Damien Neil96c229a2019-04-03 12:17:24 -0700140 // Pass up errors (fatal and otherwise).
Joe Tsai378c1322019-04-25 23:48:08 -0700141 if err := o.unmarshalMessage(v.Bytes(), m2); !nerr.Merge(err) {
Damien Neilbc310b52019-04-11 11:46:55 -0700142 return n, err
143 }
Damien Neilba23aa52018-12-07 14:38:17 -0800144 default:
145 // Non-message scalars replace the previous value.
Joe Tsai378c1322019-04-25 23:48:08 -0700146 m.Set(fd, v)
Damien Neilba23aa52018-12-07 14:38:17 -0800147 }
Damien Neilbc310b52019-04-11 11:46:55 -0700148 return n, nerr.E
Damien Neilba23aa52018-12-07 14:38:17 -0800149}
150
Joe Tsai378c1322019-04-25 23:48:08 -0700151func (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 -0800152 if wtyp != wire.BytesType {
153 return 0, errUnknown
154 }
155 b, n = wire.ConsumeBytes(b)
156 if n < 0 {
157 return 0, wire.ParseError(n)
158 }
159 var (
Joe Tsai378c1322019-04-25 23:48:08 -0700160 keyField = fd.MapKey()
161 valField = fd.MapValue()
Damien Neilba23aa52018-12-07 14:38:17 -0800162 key protoreflect.Value
163 val protoreflect.Value
164 haveKey bool
165 haveVal bool
166 )
167 switch valField.Kind() {
168 case protoreflect.GroupKind, protoreflect.MessageKind:
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800169 val = protoreflect.ValueOf(mapv.NewMessage())
Damien Neilba23aa52018-12-07 14:38:17 -0800170 }
171 // Map entries are represented as a two-element message with fields
172 // containing the key and value.
Damien Neil96c229a2019-04-03 12:17:24 -0700173 var nerr errors.NonFatal
Damien Neilba23aa52018-12-07 14:38:17 -0800174 for len(b) > 0 {
175 num, wtyp, n := wire.ConsumeTag(b)
176 if n < 0 {
177 return 0, wire.ParseError(n)
178 }
179 b = b[n:]
180 err = errUnknown
181 switch num {
182 case 1:
Joe Tsai378c1322019-04-25 23:48:08 -0700183 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
Damien Neilbc310b52019-04-11 11:46:55 -0700184 if !nerr.Merge(err) {
Damien Neilba23aa52018-12-07 14:38:17 -0800185 break
186 }
Damien Neilbc310b52019-04-11 11:46:55 -0700187 err = nil
Damien Neilba23aa52018-12-07 14:38:17 -0800188 haveKey = true
189 case 2:
190 var v protoreflect.Value
Joe Tsai378c1322019-04-25 23:48:08 -0700191 v, n, err = o.unmarshalScalar(b, wtyp, valField)
Damien Neilbc310b52019-04-11 11:46:55 -0700192 if !nerr.Merge(err) {
Damien Neilba23aa52018-12-07 14:38:17 -0800193 break
194 }
Damien Neilbc310b52019-04-11 11:46:55 -0700195 err = nil
Damien Neilba23aa52018-12-07 14:38:17 -0800196 switch valField.Kind() {
197 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neil96c229a2019-04-03 12:17:24 -0700198 if err := o.unmarshalMessage(v.Bytes(), val.Message()); !nerr.Merge(err) {
Damien Neilba23aa52018-12-07 14:38:17 -0800199 return 0, err
200 }
201 default:
202 val = v
203 }
204 haveVal = true
205 }
206 if err == errUnknown {
207 n = wire.ConsumeFieldValue(num, wtyp, b)
208 if n < 0 {
209 return 0, wire.ParseError(n)
210 }
Damien Neilbc310b52019-04-11 11:46:55 -0700211 } else if err != nil {
Damien Neilba23aa52018-12-07 14:38:17 -0800212 return 0, err
213 }
214 b = b[n:]
215 }
216 // Every map entry should have entries for key and value, but this is not strictly required.
217 if !haveKey {
218 key = keyField.Default()
219 }
220 if !haveVal {
221 switch valField.Kind() {
222 case protoreflect.GroupKind, protoreflect.MessageKind:
Damien Neilba23aa52018-12-07 14:38:17 -0800223 default:
224 val = valField.Default()
225 }
226 }
227 mapv.Set(key.MapKey(), val)
Damien Neil96c229a2019-04-03 12:17:24 -0700228 return n, nerr.E
Damien Neilba23aa52018-12-07 14:38:17 -0800229}
230
231// errUnknown is used internally to indicate fields which should be added
232// to the unknown field set of a message. It is never returned from an exported
233// function.
Damien Neil96c229a2019-04-03 12:17:24 -0700234var errUnknown = errors.New("BUG: internal error (unknown)")