blob: 4e4d0c937880b329244f9773dc00fc0aaf1fe204 [file] [log] [blame]
Herbie Ong800c9902018-12-06 15:28:53 -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
Damien Neil5c5b5312019-05-14 12:44:37 -07005package prototext
Herbie Ong800c9902018-12-06 15:28:53 -08006
7import (
8 "fmt"
Herbie Ong0dcfb9a2019-01-14 15:32:26 -08009 "strings"
Herbie Ong21a39742019-04-08 17:32:44 -070010 "unicode/utf8"
Herbie Ong800c9902018-12-06 15:28:53 -080011
Joe Tsai5ae10aa2019-07-11 18:23:08 -070012 "google.golang.org/protobuf/internal/encoding/messageset"
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/internal/encoding/text"
14 "google.golang.org/protobuf/internal/errors"
15 "google.golang.org/protobuf/internal/fieldnum"
Joe Tsai5ae10aa2019-07-11 18:23:08 -070016 "google.golang.org/protobuf/internal/flags"
Damien Neile89e6242019-05-13 23:55:40 -070017 "google.golang.org/protobuf/internal/pragma"
18 "google.golang.org/protobuf/internal/set"
19 "google.golang.org/protobuf/proto"
20 pref "google.golang.org/protobuf/reflect/protoreflect"
21 "google.golang.org/protobuf/reflect/protoregistry"
Herbie Ong800c9902018-12-06 15:28:53 -080022)
23
24// Unmarshal reads the given []byte into the given proto.Message.
Joe Tsaicdb77732019-05-14 16:05:06 -070025func Unmarshal(b []byte, m proto.Message) error {
26 return UnmarshalOptions{}.Unmarshal(b, m)
Herbie Ong800c9902018-12-06 15:28:53 -080027}
28
Herbie Ong42577ea2019-03-26 16:26:22 -070029// UnmarshalOptions is a configurable textproto format unmarshaler.
Herbie Ong800c9902018-12-06 15:28:53 -080030type UnmarshalOptions struct {
31 pragma.NoUnkeyedLiterals
Herbie Ongc525c972018-12-18 18:04:31 -080032
Herbie Ong42577ea2019-03-26 16:26:22 -070033 // AllowPartial accepts input for messages that will result in missing
34 // required fields. If AllowPartial is false (the default), Unmarshal will
35 // return error if there are any missing required fields.
36 AllowPartial bool
37
Joe Tsai8689fa52019-09-15 00:17:39 -070038 // DiscardUnknown specifies whether to ignore unknown fields when parsing.
39 // An unknown field is any field whose field name or field number does not
40 // resolve to any known or extension field in the message.
41 // By default, unmarshal rejects unknown fields as an error.
42 DiscardUnknown bool
43
Joe Tsai1c283042019-05-14 14:28:19 -070044 // Resolver is used for looking up types when unmarshaling
45 // google.protobuf.Any messages or extension fields.
46 // If nil, this defaults to using protoregistry.GlobalTypes.
47 Resolver interface {
48 protoregistry.MessageTypeResolver
49 protoregistry.ExtensionTypeResolver
50 }
Herbie Ong800c9902018-12-06 15:28:53 -080051}
52
53// Unmarshal reads the given []byte and populates the given proto.Message using options in
54// UnmarshalOptions object.
Joe Tsaicdb77732019-05-14 16:05:06 -070055func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
Joe Tsai378c1322019-04-25 23:48:08 -070056 proto.Reset(m)
Herbie Ong800c9902018-12-06 15:28:53 -080057
58 // Parse into text.Value of message type.
59 val, err := text.Unmarshal(b)
Damien Neil8c86fc52019-06-19 09:28:29 -070060 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080061 return err
62 }
63
Herbie Ongc525c972018-12-18 18:04:31 -080064 if o.Resolver == nil {
65 o.Resolver = protoregistry.GlobalTypes
66 }
Joe Tsai378c1322019-04-25 23:48:08 -070067 err = o.unmarshalMessage(val.Message(), m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070068 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080069 return err
70 }
71
Damien Neil8c86fc52019-06-19 09:28:29 -070072 if o.AllowPartial {
73 return nil
Damien Neil4686e232019-04-05 13:31:40 -070074 }
Damien Neil8c86fc52019-06-19 09:28:29 -070075 return proto.IsInitialized(m)
Herbie Ong800c9902018-12-06 15:28:53 -080076}
77
Herbie Ong800c9902018-12-06 15:28:53 -080078// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
79func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070080 messageDesc := m.Descriptor()
Joe Tsai1799d112019-08-08 13:31:59 -070081 if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
Joe Tsai5ae10aa2019-07-11 18:23:08 -070082 return errors.New("no support for proto1 MessageSets")
83 }
Herbie Ong66c365c2019-01-04 14:08:41 -080084
85 // Handle expanded Any message.
Joe Tsai0fc49f82019-05-01 12:29:25 -070086 if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
Joe Tsai378c1322019-04-25 23:48:08 -070087 return o.unmarshalAny(tmsg[0], m)
Herbie Ong66c365c2019-01-04 14:08:41 -080088 }
89
Herbie Ong800c9902018-12-06 15:28:53 -080090 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -070091 var seenOneofs set.Ints
Joe Tsai5ae10aa2019-07-11 18:23:08 -070092 fieldDescs := messageDesc.Fields()
Herbie Ong800c9902018-12-06 15:28:53 -080093 for _, tfield := range tmsg {
94 tkey := tfield[0]
95 tval := tfield[1]
96
Joe Tsai8689fa52019-09-15 00:17:39 -070097 // Resolve the field descriptor.
Herbie Ongc525c972018-12-18 18:04:31 -080098 var name pref.Name
Joe Tsai8689fa52019-09-15 00:17:39 -070099 var fd pref.FieldDescriptor
100 var xt pref.ExtensionType
101 var xtErr error
Herbie Ongc525c972018-12-18 18:04:31 -0800102 switch tkey.Type() {
103 case text.Name:
104 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -0800105 fd = fieldDescs.ByName(name)
Joe Tsai8689fa52019-09-15 00:17:39 -0700106 if fd == nil {
Joe Tsaid47ea192019-07-09 22:38:15 -0700107 // The proto name of a group field is in all lowercase,
108 // while the textproto field name is the group message name.
Herbie Ong20aefe92019-06-24 19:21:46 -0700109 gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
110 if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
111 fd = gd
112 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700113 } else if fd.Kind() == pref.GroupKind && fd.Message().Name() != name {
Joe Tsaid47ea192019-07-09 22:38:15 -0700114 fd = nil // reset since field name is actually the message name
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800115 }
Herbie Ongc525c972018-12-18 18:04:31 -0800116 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800117 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700118 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800119 break
120 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700121 xt, xtErr = o.findExtension(pref.FullName(tkey.String()))
122 case text.Uint:
123 v, _ := tkey.Uint(false)
124 num := pref.FieldNumber(v)
125 if !num.IsValid() {
126 return errors.New("invalid field number: %d", num)
Herbie Ongc525c972018-12-18 18:04:31 -0800127 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700128 fd = fieldDescs.ByNumber(num)
129 if fd == nil {
130 xt, xtErr = o.Resolver.FindExtensionByNumber(messageDesc.FullName(), num)
Damien Neil92f76182019-08-02 16:58:08 -0700131 }
Herbie Ong800c9902018-12-06 15:28:53 -0800132 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700133 if xt != nil {
134 fd = xt.TypeDescriptor()
135 if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
136 return errors.New("message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
137 }
138 } else if xtErr != nil && xtErr != protoregistry.NotFound {
139 return errors.New("unable to resolve: %v", xtErr)
140 }
Joe Tsai6e095992019-08-10 13:56:36 -0700141 if flags.ProtoLegacy {
142 if fd != nil && fd.IsWeak() && fd.Message().IsPlaceholder() {
143 fd = nil // reset since the weak reference is not linked in
144 }
Joe Tsai8689fa52019-09-15 00:17:39 -0700145 }
Herbie Ongc525c972018-12-18 18:04:31 -0800146
Joe Tsai8689fa52019-09-15 00:17:39 -0700147 // Handle unknown fields.
Herbie Ong800c9902018-12-06 15:28:53 -0800148 if fd == nil {
Joe Tsai8689fa52019-09-15 00:17:39 -0700149 if o.DiscardUnknown || messageDesc.ReservedNames().Has(name) {
Herbie Ong7c624e22018-12-13 14:41:22 -0800150 continue
151 }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700152 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800153 }
154
Joe Tsai8689fa52019-09-15 00:17:39 -0700155 // Handle fields identified by field number.
156 if tkey.Type() == text.Uint {
157 // TODO: Add an option to permit parsing field numbers.
158 //
159 // This requires careful thought as the MarshalOptions.EmitUnknown
160 // option allows formatting unknown fields as the field number
161 // and the best-effort textual representation of the field value.
162 // In that case, it may not be possible to unmarshal the value from
163 // a parser that does have information about the unknown field.
164 return errors.New("cannot specify field by number: %v", tkey)
165 }
166
Joe Tsaiac31a352019-05-13 14:32:56 -0700167 switch {
168 case fd.IsList():
169 // If input is not a list, turn it into a list.
170 var items []text.Value
171 if tval.Type() != text.List {
172 items = []text.Value{tval}
173 } else {
174 items = tval.List()
175 }
176
Joe Tsai378c1322019-04-25 23:48:08 -0700177 list := m.Mutable(fd).List()
Damien Neil8c86fc52019-06-19 09:28:29 -0700178 if err := o.unmarshalList(items, fd, list); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800179 return err
180 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700181 case fd.IsMap():
182 // If input is not a list, turn it into a list.
183 var items []text.Value
184 if tval.Type() != text.List {
185 items = []text.Value{tval}
186 } else {
187 items = tval.List()
188 }
189
Joe Tsai378c1322019-04-25 23:48:08 -0700190 mmap := m.Mutable(fd).Map()
Damien Neil8c86fc52019-06-19 09:28:29 -0700191 if err := o.unmarshalMap(items, fd, mmap); err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700192 return err
193 }
194 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700195 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700196 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700197 idx := uint64(od.Index())
198 if seenOneofs.Has(idx) {
199 return errors.New("oneof %v is already set", od.FullName())
200 }
201 seenOneofs.Set(idx)
202 }
203
Herbie Ong800c9902018-12-06 15:28:53 -0800204 // Required or optional fields.
205 num := uint64(fd.Number())
206 if seenNums.Has(num) {
207 return errors.New("non-repeated field %v is repeated", fd.FullName())
208 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700209 if err := o.unmarshalSingular(tval, fd, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800210 return err
211 }
Herbie Ong800c9902018-12-06 15:28:53 -0800212 seenNums.Set(num)
213 }
214 }
215
Damien Neil8c86fc52019-06-19 09:28:29 -0700216 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800217}
218
Herbie Ong6470ea62019-01-07 18:56:57 -0800219// findExtension returns protoreflect.ExtensionType from the Resolver if found.
220func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
221 xt, err := o.Resolver.FindExtensionByName(xtName)
222 if err == nil {
223 return xt, nil
224 }
Joe Tsai5ae10aa2019-07-11 18:23:08 -0700225 return messageset.FindMessageSetExtension(o.Resolver, xtName)
Herbie Ong6470ea62019-01-07 18:56:57 -0800226}
227
Herbie Ong800c9902018-12-06 15:28:53 -0800228// unmarshalSingular unmarshals given text.Value into the non-repeated field.
Joe Tsai378c1322019-04-25 23:48:08 -0700229func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800230 var val pref.Value
231 switch fd.Kind() {
232 case pref.MessageKind, pref.GroupKind:
233 if input.Type() != text.Message {
234 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
235 }
Damien Neild91c4222019-09-04 10:46:00 -0700236 val = m.NewField(fd)
237 if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800238 return err
239 }
Herbie Ong800c9902018-12-06 15:28:53 -0800240 default:
241 var err error
242 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700243 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800244 return err
245 }
246 }
Joe Tsai378c1322019-04-25 23:48:08 -0700247 m.Set(fd, val)
Herbie Ong800c9902018-12-06 15:28:53 -0800248
Damien Neil8c86fc52019-06-19 09:28:29 -0700249 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800250}
251
Herbie Ong800c9902018-12-06 15:28:53 -0800252// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
253// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
254func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
255 const b32 = false
256 const b64 = true
257
258 switch kind := fd.Kind(); kind {
259 case pref.BoolKind:
260 if b, ok := input.Bool(); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700261 return pref.ValueOfBool(bool(b)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800262 }
263 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
264 if n, ok := input.Int(b32); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700265 return pref.ValueOfInt32(int32(n)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800266 }
267 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
268 if n, ok := input.Int(b64); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700269 return pref.ValueOfInt64(int64(n)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800270 }
271 case pref.Uint32Kind, pref.Fixed32Kind:
272 if n, ok := input.Uint(b32); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700273 return pref.ValueOfUint32(uint32(n)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800274 }
275 case pref.Uint64Kind, pref.Fixed64Kind:
276 if n, ok := input.Uint(b64); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700277 return pref.ValueOfUint64(uint64(n)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800278 }
279 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700280 if n, ok := input.Float(b32); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700281 return pref.ValueOfFloat32(float32(n)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800282 }
283 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700284 if n, ok := input.Float(b64); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700285 return pref.ValueOfFloat64(float64(n)), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800286 }
287 case pref.StringKind:
288 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700289 s := input.String()
290 if utf8.ValidString(s) {
Joe Tsai84177c92019-09-17 13:38:48 -0700291 return pref.ValueOfString(s), nil
Herbie Ong21a39742019-04-08 17:32:44 -0700292 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700293 return pref.Value{}, errors.InvalidUTF8(string(fd.FullName()))
Herbie Ong800c9902018-12-06 15:28:53 -0800294 }
295 case pref.BytesKind:
296 if input.Type() == text.String {
Joe Tsai84177c92019-09-17 13:38:48 -0700297 return pref.ValueOfBytes([]byte(input.String())), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800298 }
299 case pref.EnumKind:
300 // If input is int32, use directly.
301 if n, ok := input.Int(b32); ok {
Joe Tsai84177c92019-09-17 13:38:48 -0700302 return pref.ValueOfEnum(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800303 }
304 if name, ok := input.Name(); ok {
305 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700306 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Joe Tsai84177c92019-09-17 13:38:48 -0700307 return pref.ValueOfEnum(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800308 }
309 }
310 default:
311 panic(fmt.Sprintf("invalid scalar kind %v", kind))
312 }
313
314 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
315}
316
317// unmarshalList unmarshals given []text.Value into given protoreflect.List.
318func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800319 switch fd.Kind() {
320 case pref.MessageKind, pref.GroupKind:
321 for _, input := range inputList {
322 if input.Type() != text.Message {
323 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
324 }
Damien Neild91c4222019-09-04 10:46:00 -0700325 val := list.NewElement()
326 if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800327 return err
328 }
Damien Neild91c4222019-09-04 10:46:00 -0700329 list.Append(val)
Herbie Ong800c9902018-12-06 15:28:53 -0800330 }
331 default:
332 for _, input := range inputList {
333 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700334 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800335 return err
336 }
337 list.Append(val)
338 }
339 }
340
Damien Neil8c86fc52019-06-19 09:28:29 -0700341 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800342}
343
344// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
345func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800346 // Determine ahead whether map entry is a scalar type or a message type in order to call the
347 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800348 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700349 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800350 case pref.MessageKind, pref.GroupKind:
351 unmarshalMapValue = o.unmarshalMapMessageValue
352 }
353
354 for _, entry := range input {
355 if entry.Type() != text.Message {
356 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
357 }
358 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
Damien Neil8c86fc52019-06-19 09:28:29 -0700359 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800360 return err
361 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700362 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Damien Neil8c86fc52019-06-19 09:28:29 -0700363 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800364 return err
365 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700366 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Damien Neil8c86fc52019-06-19 09:28:29 -0700367 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800368 return err
369 }
370 }
371
Damien Neil8c86fc52019-06-19 09:28:29 -0700372 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800373}
374
375// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
376// field values. If there are duplicate field names, the value for the last field is returned. If
377// the field name does not exist, it will return the zero value of text.Value. It will return an
378// error if there are unknown field names.
379func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
380 for _, field := range mapEntry {
381 keyStr, ok := field[0].Name()
382 if ok {
383 switch keyStr {
384 case "key":
385 if key.Type() != 0 {
386 return key, value, errors.New("%v contains duplicate key field", name)
387 }
388 key = field[1]
389 case "value":
390 if value.Type() != 0 {
391 return key, value, errors.New("%v contains duplicate value field", name)
392 }
393 value = field[1]
394 default:
395 ok = false
396 }
397 }
398 if !ok {
399 // TODO: Do not return error if ignore unknown option is added and enabled.
400 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
401 }
402 }
403 return key, value, nil
404}
405
406// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
407// integral or string type.
408func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
409 // If input is not set, use the zero value.
410 if input.Type() == 0 {
411 return fd.Default().MapKey(), nil
412 }
413
414 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700415 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800416 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
417 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700418 return val.MapKey(), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800419}
420
421// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
422// the given MapKey.
Damien Neil1a08d542019-12-22 09:46:57 -0800423func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800424 var value [][2]text.Value
Damien Neil1a08d542019-12-22 09:46:57 -0800425 switch input.Type() {
426 case 0:
427 case text.Message:
Herbie Ong800c9902018-12-06 15:28:53 -0800428 value = input.Message()
Damien Neil1a08d542019-12-22 09:46:57 -0800429 default:
430 return errors.New("%v contains invalid value: %v", fd.FullName(), input)
Herbie Ong800c9902018-12-06 15:28:53 -0800431 }
Damien Neild91c4222019-09-04 10:46:00 -0700432 val := mmap.NewValue()
433 if err := o.unmarshalMessage(value, val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800434 return err
435 }
Damien Neild91c4222019-09-04 10:46:00 -0700436 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700437 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800438}
439
440// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
441// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800442func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800443 var val pref.Value
444 if input.Type() == 0 {
445 val = fd.Default()
446 } else {
447 var err error
448 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700449 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800450 return err
451 }
452 }
453 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700454 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800455}
Herbie Ong66c365c2019-01-04 14:08:41 -0800456
457// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
458// field with key type of text.String type and value type of text.Message.
459func isExpandedAny(tmsg [][2]text.Value) bool {
460 if len(tmsg) != 1 {
461 return false
462 }
463
464 field := tmsg[0]
465 return field[0].Type() == text.String && field[1].Type() == text.Message
466}
467
468// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
469// tfield has key type of text.String and value type of text.Message.
Joe Tsai378c1322019-04-25 23:48:08 -0700470func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
Herbie Ong66c365c2019-01-04 14:08:41 -0800471 typeURL := tfield[0].String()
472 value := tfield[1].Message()
473
474 mt, err := o.Resolver.FindMessageByURL(typeURL)
Damien Neil8c86fc52019-06-19 09:28:29 -0700475 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800476 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
477 }
478 // Create new message for the embedded message type and unmarshal the
479 // value into it.
Joe Tsai378c1322019-04-25 23:48:08 -0700480 m2 := mt.New()
Damien Neil8c86fc52019-06-19 09:28:29 -0700481 if err := o.unmarshalMessage(value, m2); err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800482 return err
483 }
484 // Serialize the embedded message and assign the resulting bytes to the value field.
Damien Neil96c229a2019-04-03 12:17:24 -0700485 b, err := proto.MarshalOptions{
Damien Neil0c9f0a92019-06-19 10:41:09 -0700486 AllowPartial: true, // never check required fields inside an Any
Damien Neil96c229a2019-04-03 12:17:24 -0700487 Deterministic: true,
Joe Tsai378c1322019-04-25 23:48:08 -0700488 }.Marshal(m2.Interface())
Damien Neil8c86fc52019-06-19 09:28:29 -0700489 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800490 return err
491 }
492
Joe Tsai378c1322019-04-25 23:48:08 -0700493 fds := m.Descriptor().Fields()
494 fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
495 fdValue := fds.ByNumber(fieldnum.Any_Value)
496
Joe Tsai84177c92019-09-17 13:38:48 -0700497 m.Set(fdType, pref.ValueOfString(typeURL))
498 m.Set(fdValue, pref.ValueOfBytes(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800499
Damien Neil8c86fc52019-06-19 09:28:29 -0700500 return nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800501}