blob: 99ef5dcd02870706550283076399990868476c8e [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 Tsai1c283042019-05-14 14:28:19 -070038 // Resolver is used for looking up types when unmarshaling
39 // google.protobuf.Any messages or extension fields.
40 // If nil, this defaults to using protoregistry.GlobalTypes.
41 Resolver interface {
42 protoregistry.MessageTypeResolver
43 protoregistry.ExtensionTypeResolver
44 }
Herbie Ong800c9902018-12-06 15:28:53 -080045}
46
47// Unmarshal reads the given []byte and populates the given proto.Message using options in
48// UnmarshalOptions object.
Joe Tsaicdb77732019-05-14 16:05:06 -070049func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -080050 // Clear all fields before populating it.
Damien Neil5c5b5312019-05-14 12:44:37 -070051 // TODO: Determine if this needs to be consistent with protojson and binary unmarshal where
Herbie Ong800c9902018-12-06 15:28:53 -080052 // behavior is to merge values into existing message. If decision is to not clear the fields
53 // ahead, code will need to be updated properly when merging nested messages.
Joe Tsai378c1322019-04-25 23:48:08 -070054 proto.Reset(m)
Herbie Ong800c9902018-12-06 15:28:53 -080055
56 // Parse into text.Value of message type.
57 val, err := text.Unmarshal(b)
Damien Neil8c86fc52019-06-19 09:28:29 -070058 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080059 return err
60 }
61
Herbie Ongc525c972018-12-18 18:04:31 -080062 if o.Resolver == nil {
63 o.Resolver = protoregistry.GlobalTypes
64 }
Joe Tsai378c1322019-04-25 23:48:08 -070065 err = o.unmarshalMessage(val.Message(), m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070066 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080067 return err
68 }
69
Damien Neil8c86fc52019-06-19 09:28:29 -070070 if o.AllowPartial {
71 return nil
Damien Neil4686e232019-04-05 13:31:40 -070072 }
Damien Neil8c86fc52019-06-19 09:28:29 -070073 return proto.IsInitialized(m)
Herbie Ong800c9902018-12-06 15:28:53 -080074}
75
Herbie Ong800c9902018-12-06 15:28:53 -080076// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
77func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070078 messageDesc := m.Descriptor()
Joe Tsai1799d112019-08-08 13:31:59 -070079 if !flags.ProtoLegacy && messageset.IsMessageSet(messageDesc) {
Joe Tsai5ae10aa2019-07-11 18:23:08 -070080 return errors.New("no support for proto1 MessageSets")
81 }
Herbie Ong66c365c2019-01-04 14:08:41 -080082
83 // Handle expanded Any message.
Joe Tsai0fc49f82019-05-01 12:29:25 -070084 if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
Joe Tsai378c1322019-04-25 23:48:08 -070085 return o.unmarshalAny(tmsg[0], m)
Herbie Ong66c365c2019-01-04 14:08:41 -080086 }
87
Herbie Ong800c9902018-12-06 15:28:53 -080088 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -070089 var seenOneofs set.Ints
Joe Tsai5ae10aa2019-07-11 18:23:08 -070090 fieldDescs := messageDesc.Fields()
Herbie Ong800c9902018-12-06 15:28:53 -080091 for _, tfield := range tmsg {
92 tkey := tfield[0]
93 tval := tfield[1]
94
95 var fd pref.FieldDescriptor
Herbie Ongc525c972018-12-18 18:04:31 -080096 var name pref.Name
97 switch tkey.Type() {
98 case text.Name:
99 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -0800100 fd = fieldDescs.ByName(name)
Joe Tsaid47ea192019-07-09 22:38:15 -0700101 switch {
102 case fd == nil:
103 // The proto name of a group field is in all lowercase,
104 // while the textproto field name is the group message name.
105 // Check to make sure that group name is correct.
Herbie Ong20aefe92019-06-24 19:21:46 -0700106 gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
107 if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
108 fd = gd
109 }
Joe Tsaid47ea192019-07-09 22:38:15 -0700110 case fd.Kind() == pref.GroupKind && fd.Message().Name() != name:
111 fd = nil // reset since field name is actually the message name
112 case fd.IsWeak() && fd.Message().IsPlaceholder():
113 fd = nil // reset since the weak reference is not linked in
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800114 }
Herbie Ongc525c972018-12-18 18:04:31 -0800115 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800116 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700117 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800118 break
119 }
120 // Extensions have to be registered first in the message's
Herbie Ongc525c972018-12-18 18:04:31 -0800121 // ExtensionTypes before setting a value to it.
Joe Tsai378c1322019-04-25 23:48:08 -0700122 extName := pref.FullName(tkey.String())
Herbie Ong66c365c2019-01-04 14:08:41 -0800123 // Check first if it is already registered. This is the case for
124 // repeated fields.
Joe Tsai378c1322019-04-25 23:48:08 -0700125 xt, err := o.findExtension(extName)
126 if err != nil && err != protoregistry.NotFound {
127 return errors.New("unable to resolve [%v]: %v", extName, err)
Herbie Ongc525c972018-12-18 18:04:31 -0800128 }
Damien Neil92f76182019-08-02 16:58:08 -0700129 if xt != nil {
Damien Neil79bfdbe2019-08-28 11:08:22 -0700130 fd = xt.TypeDescriptor()
Joe Tsai0bf41132019-09-05 21:46:36 -0700131 if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() {
132 return errors.New("message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName())
133 }
Damien Neil92f76182019-08-02 16:58:08 -0700134 }
Herbie Ong800c9902018-12-06 15:28:53 -0800135 }
Herbie Ongc525c972018-12-18 18:04:31 -0800136
Herbie Ong800c9902018-12-06 15:28:53 -0800137 if fd == nil {
Herbie Ong7c624e22018-12-13 14:41:22 -0800138 // Ignore reserved names.
Joe Tsai5ae10aa2019-07-11 18:23:08 -0700139 if messageDesc.ReservedNames().Has(name) {
Herbie Ong7c624e22018-12-13 14:41:22 -0800140 continue
141 }
Herbie Ong800c9902018-12-06 15:28:53 -0800142 // TODO: Can provide option to ignore unknown message fields.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700143 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800144 }
145
Joe Tsaiac31a352019-05-13 14:32:56 -0700146 switch {
147 case fd.IsList():
148 // If input is not a list, turn it into a list.
149 var items []text.Value
150 if tval.Type() != text.List {
151 items = []text.Value{tval}
152 } else {
153 items = tval.List()
154 }
155
Joe Tsai378c1322019-04-25 23:48:08 -0700156 list := m.Mutable(fd).List()
Damien Neil8c86fc52019-06-19 09:28:29 -0700157 if err := o.unmarshalList(items, fd, list); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800158 return err
159 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700160 case fd.IsMap():
161 // If input is not a list, turn it into a list.
162 var items []text.Value
163 if tval.Type() != text.List {
164 items = []text.Value{tval}
165 } else {
166 items = tval.List()
167 }
168
Joe Tsai378c1322019-04-25 23:48:08 -0700169 mmap := m.Mutable(fd).Map()
Damien Neil8c86fc52019-06-19 09:28:29 -0700170 if err := o.unmarshalMap(items, fd, mmap); err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700171 return err
172 }
173 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700174 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700175 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700176 idx := uint64(od.Index())
177 if seenOneofs.Has(idx) {
178 return errors.New("oneof %v is already set", od.FullName())
179 }
180 seenOneofs.Set(idx)
181 }
182
Herbie Ong800c9902018-12-06 15:28:53 -0800183 // Required or optional fields.
184 num := uint64(fd.Number())
185 if seenNums.Has(num) {
186 return errors.New("non-repeated field %v is repeated", fd.FullName())
187 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700188 if err := o.unmarshalSingular(tval, fd, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800189 return err
190 }
Herbie Ong800c9902018-12-06 15:28:53 -0800191 seenNums.Set(num)
192 }
193 }
194
Damien Neil8c86fc52019-06-19 09:28:29 -0700195 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800196}
197
Herbie Ong6470ea62019-01-07 18:56:57 -0800198// findExtension returns protoreflect.ExtensionType from the Resolver if found.
199func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
200 xt, err := o.Resolver.FindExtensionByName(xtName)
201 if err == nil {
202 return xt, nil
203 }
Joe Tsai5ae10aa2019-07-11 18:23:08 -0700204 return messageset.FindMessageSetExtension(o.Resolver, xtName)
Herbie Ong6470ea62019-01-07 18:56:57 -0800205}
206
Herbie Ong800c9902018-12-06 15:28:53 -0800207// unmarshalSingular unmarshals given text.Value into the non-repeated field.
Joe Tsai378c1322019-04-25 23:48:08 -0700208func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800209 var val pref.Value
210 switch fd.Kind() {
211 case pref.MessageKind, pref.GroupKind:
212 if input.Type() != text.Message {
213 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
214 }
Damien Neild91c4222019-09-04 10:46:00 -0700215 val = m.NewField(fd)
216 if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800217 return err
218 }
Herbie Ong800c9902018-12-06 15:28:53 -0800219 default:
220 var err error
221 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700222 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800223 return err
224 }
225 }
Joe Tsai378c1322019-04-25 23:48:08 -0700226 m.Set(fd, val)
Herbie Ong800c9902018-12-06 15:28:53 -0800227
Damien Neil8c86fc52019-06-19 09:28:29 -0700228 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800229}
230
Herbie Ong800c9902018-12-06 15:28:53 -0800231// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
232// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
233func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
234 const b32 = false
235 const b64 = true
236
237 switch kind := fd.Kind(); kind {
238 case pref.BoolKind:
239 if b, ok := input.Bool(); ok {
240 return pref.ValueOf(bool(b)), nil
241 }
242 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
243 if n, ok := input.Int(b32); ok {
244 return pref.ValueOf(int32(n)), nil
245 }
246 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
247 if n, ok := input.Int(b64); ok {
248 return pref.ValueOf(int64(n)), nil
249 }
250 case pref.Uint32Kind, pref.Fixed32Kind:
251 if n, ok := input.Uint(b32); ok {
252 return pref.ValueOf(uint32(n)), nil
253 }
254 case pref.Uint64Kind, pref.Fixed64Kind:
255 if n, ok := input.Uint(b64); ok {
256 return pref.ValueOf(uint64(n)), nil
257 }
258 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700259 if n, ok := input.Float(b32); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800260 return pref.ValueOf(float32(n)), nil
261 }
262 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700263 if n, ok := input.Float(b64); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800264 return pref.ValueOf(float64(n)), nil
265 }
266 case pref.StringKind:
267 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700268 s := input.String()
269 if utf8.ValidString(s) {
270 return pref.ValueOf(s), nil
271 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700272 return pref.Value{}, errors.InvalidUTF8(string(fd.FullName()))
Herbie Ong800c9902018-12-06 15:28:53 -0800273 }
274 case pref.BytesKind:
275 if input.Type() == text.String {
276 return pref.ValueOf([]byte(input.String())), nil
277 }
278 case pref.EnumKind:
279 // If input is int32, use directly.
280 if n, ok := input.Int(b32); ok {
281 return pref.ValueOf(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800282 }
283 if name, ok := input.Name(); ok {
284 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700285 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800286 return pref.ValueOf(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800287 }
288 }
289 default:
290 panic(fmt.Sprintf("invalid scalar kind %v", kind))
291 }
292
293 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
294}
295
296// unmarshalList unmarshals given []text.Value into given protoreflect.List.
297func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800298 switch fd.Kind() {
299 case pref.MessageKind, pref.GroupKind:
300 for _, input := range inputList {
301 if input.Type() != text.Message {
302 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
303 }
Damien Neild91c4222019-09-04 10:46:00 -0700304 val := list.NewElement()
305 if err := o.unmarshalMessage(input.Message(), val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800306 return err
307 }
Damien Neild91c4222019-09-04 10:46:00 -0700308 list.Append(val)
Herbie Ong800c9902018-12-06 15:28:53 -0800309 }
310 default:
311 for _, input := range inputList {
312 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700313 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800314 return err
315 }
316 list.Append(val)
317 }
318 }
319
Damien Neil8c86fc52019-06-19 09:28:29 -0700320 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800321}
322
323// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
324func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800325 // Determine ahead whether map entry is a scalar type or a message type in order to call the
326 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800327 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700328 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800329 case pref.MessageKind, pref.GroupKind:
330 unmarshalMapValue = o.unmarshalMapMessageValue
331 }
332
333 for _, entry := range input {
334 if entry.Type() != text.Message {
335 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
336 }
337 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
Damien Neil8c86fc52019-06-19 09:28:29 -0700338 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800339 return err
340 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700341 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Damien Neil8c86fc52019-06-19 09:28:29 -0700342 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800343 return err
344 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700345 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Damien Neil8c86fc52019-06-19 09:28:29 -0700346 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800347 return err
348 }
349 }
350
Damien Neil8c86fc52019-06-19 09:28:29 -0700351 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800352}
353
354// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
355// field values. If there are duplicate field names, the value for the last field is returned. If
356// the field name does not exist, it will return the zero value of text.Value. It will return an
357// error if there are unknown field names.
358func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
359 for _, field := range mapEntry {
360 keyStr, ok := field[0].Name()
361 if ok {
362 switch keyStr {
363 case "key":
364 if key.Type() != 0 {
365 return key, value, errors.New("%v contains duplicate key field", name)
366 }
367 key = field[1]
368 case "value":
369 if value.Type() != 0 {
370 return key, value, errors.New("%v contains duplicate value field", name)
371 }
372 value = field[1]
373 default:
374 ok = false
375 }
376 }
377 if !ok {
378 // TODO: Do not return error if ignore unknown option is added and enabled.
379 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
380 }
381 }
382 return key, value, nil
383}
384
385// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
386// integral or string type.
387func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
388 // If input is not set, use the zero value.
389 if input.Type() == 0 {
390 return fd.Default().MapKey(), nil
391 }
392
393 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700394 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800395 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
396 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700397 return val.MapKey(), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800398}
399
400// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
401// the given MapKey.
402func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800403 var value [][2]text.Value
404 if input.Type() != 0 {
405 value = input.Message()
406 }
Damien Neild91c4222019-09-04 10:46:00 -0700407 val := mmap.NewValue()
408 if err := o.unmarshalMessage(value, val.Message()); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800409 return err
410 }
Damien Neild91c4222019-09-04 10:46:00 -0700411 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700412 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800413}
414
415// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
416// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800417func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800418 var val pref.Value
419 if input.Type() == 0 {
420 val = fd.Default()
421 } else {
422 var err error
423 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700424 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800425 return err
426 }
427 }
428 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700429 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800430}
Herbie Ong66c365c2019-01-04 14:08:41 -0800431
432// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
433// field with key type of text.String type and value type of text.Message.
434func isExpandedAny(tmsg [][2]text.Value) bool {
435 if len(tmsg) != 1 {
436 return false
437 }
438
439 field := tmsg[0]
440 return field[0].Type() == text.String && field[1].Type() == text.Message
441}
442
443// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
444// tfield has key type of text.String and value type of text.Message.
Joe Tsai378c1322019-04-25 23:48:08 -0700445func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
Herbie Ong66c365c2019-01-04 14:08:41 -0800446 typeURL := tfield[0].String()
447 value := tfield[1].Message()
448
449 mt, err := o.Resolver.FindMessageByURL(typeURL)
Damien Neil8c86fc52019-06-19 09:28:29 -0700450 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800451 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
452 }
453 // Create new message for the embedded message type and unmarshal the
454 // value into it.
Joe Tsai378c1322019-04-25 23:48:08 -0700455 m2 := mt.New()
Damien Neil8c86fc52019-06-19 09:28:29 -0700456 if err := o.unmarshalMessage(value, m2); err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800457 return err
458 }
459 // Serialize the embedded message and assign the resulting bytes to the value field.
Damien Neil96c229a2019-04-03 12:17:24 -0700460 b, err := proto.MarshalOptions{
Damien Neil0c9f0a92019-06-19 10:41:09 -0700461 AllowPartial: true, // never check required fields inside an Any
Damien Neil96c229a2019-04-03 12:17:24 -0700462 Deterministic: true,
Joe Tsai378c1322019-04-25 23:48:08 -0700463 }.Marshal(m2.Interface())
Damien Neil8c86fc52019-06-19 09:28:29 -0700464 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800465 return err
466 }
467
Joe Tsai378c1322019-04-25 23:48:08 -0700468 fds := m.Descriptor().Fields()
469 fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
470 fdValue := fds.ByNumber(fieldnum.Any_Value)
471
472 m.Set(fdType, pref.ValueOf(typeURL))
473 m.Set(fdValue, pref.ValueOf(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800474
Damien Neil8c86fc52019-06-19 09:28:29 -0700475 return nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800476}