blob: 78744104f2615570e95fa8d251909d8de038b802 [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
Damien Neile89e6242019-05-13 23:55:40 -070012 "google.golang.org/protobuf/internal/encoding/text"
13 "google.golang.org/protobuf/internal/errors"
14 "google.golang.org/protobuf/internal/fieldnum"
15 "google.golang.org/protobuf/internal/pragma"
16 "google.golang.org/protobuf/internal/set"
17 "google.golang.org/protobuf/proto"
18 pref "google.golang.org/protobuf/reflect/protoreflect"
19 "google.golang.org/protobuf/reflect/protoregistry"
Herbie Ong800c9902018-12-06 15:28:53 -080020)
21
22// Unmarshal reads the given []byte into the given proto.Message.
Joe Tsaicdb77732019-05-14 16:05:06 -070023func Unmarshal(b []byte, m proto.Message) error {
24 return UnmarshalOptions{}.Unmarshal(b, m)
Herbie Ong800c9902018-12-06 15:28:53 -080025}
26
Herbie Ong42577ea2019-03-26 16:26:22 -070027// UnmarshalOptions is a configurable textproto format unmarshaler.
Herbie Ong800c9902018-12-06 15:28:53 -080028type UnmarshalOptions struct {
29 pragma.NoUnkeyedLiterals
Herbie Ongc525c972018-12-18 18:04:31 -080030
Herbie Ong42577ea2019-03-26 16:26:22 -070031 // AllowPartial accepts input for messages that will result in missing
32 // required fields. If AllowPartial is false (the default), Unmarshal will
33 // return error if there are any missing required fields.
34 AllowPartial bool
35
Joe Tsai1c283042019-05-14 14:28:19 -070036 // Resolver is used for looking up types when unmarshaling
37 // google.protobuf.Any messages or extension fields.
38 // If nil, this defaults to using protoregistry.GlobalTypes.
39 Resolver interface {
40 protoregistry.MessageTypeResolver
41 protoregistry.ExtensionTypeResolver
42 }
Herbie Ong800c9902018-12-06 15:28:53 -080043}
44
45// Unmarshal reads the given []byte and populates the given proto.Message using options in
46// UnmarshalOptions object.
Joe Tsaicdb77732019-05-14 16:05:06 -070047func (o UnmarshalOptions) Unmarshal(b []byte, m proto.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -080048 // Clear all fields before populating it.
Damien Neil5c5b5312019-05-14 12:44:37 -070049 // TODO: Determine if this needs to be consistent with protojson and binary unmarshal where
Herbie Ong800c9902018-12-06 15:28:53 -080050 // behavior is to merge values into existing message. If decision is to not clear the fields
51 // ahead, code will need to be updated properly when merging nested messages.
Joe Tsai378c1322019-04-25 23:48:08 -070052 proto.Reset(m)
Herbie Ong800c9902018-12-06 15:28:53 -080053
54 // Parse into text.Value of message type.
55 val, err := text.Unmarshal(b)
Damien Neil8c86fc52019-06-19 09:28:29 -070056 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080057 return err
58 }
59
Herbie Ongc525c972018-12-18 18:04:31 -080060 if o.Resolver == nil {
61 o.Resolver = protoregistry.GlobalTypes
62 }
Joe Tsai378c1322019-04-25 23:48:08 -070063 err = o.unmarshalMessage(val.Message(), m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070064 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -080065 return err
66 }
67
Damien Neil8c86fc52019-06-19 09:28:29 -070068 if o.AllowPartial {
69 return nil
Damien Neil4686e232019-04-05 13:31:40 -070070 }
Damien Neil8c86fc52019-06-19 09:28:29 -070071 return proto.IsInitialized(m)
Herbie Ong800c9902018-12-06 15:28:53 -080072}
73
Herbie Ong800c9902018-12-06 15:28:53 -080074// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
75func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
Joe Tsai0fc49f82019-05-01 12:29:25 -070076 messageDesc := m.Descriptor()
Herbie Ong66c365c2019-01-04 14:08:41 -080077
78 // Handle expanded Any message.
Joe Tsai0fc49f82019-05-01 12:29:25 -070079 if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
Joe Tsai378c1322019-04-25 23:48:08 -070080 return o.unmarshalAny(tmsg[0], m)
Herbie Ong66c365c2019-01-04 14:08:41 -080081 }
82
Joe Tsai0fc49f82019-05-01 12:29:25 -070083 fieldDescs := messageDesc.Fields()
84 reservedNames := messageDesc.ReservedNames()
Herbie Ong800c9902018-12-06 15:28:53 -080085 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -070086 var seenOneofs set.Ints
Herbie Ong800c9902018-12-06 15:28:53 -080087
88 for _, tfield := range tmsg {
89 tkey := tfield[0]
90 tval := tfield[1]
91
92 var fd pref.FieldDescriptor
Herbie Ongc525c972018-12-18 18:04:31 -080093 var name pref.Name
94 switch tkey.Type() {
95 case text.Name:
96 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -080097 fd = fieldDescs.ByName(name)
Joe Tsaid47ea192019-07-09 22:38:15 -070098 switch {
99 case fd == nil:
100 // The proto name of a group field is in all lowercase,
101 // while the textproto field name is the group message name.
102 // Check to make sure that group name is correct.
Herbie Ong20aefe92019-06-24 19:21:46 -0700103 gd := fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
104 if gd != nil && gd.Kind() == pref.GroupKind && gd.Message().Name() == name {
105 fd = gd
106 }
Joe Tsaid47ea192019-07-09 22:38:15 -0700107 case fd.Kind() == pref.GroupKind && fd.Message().Name() != name:
108 fd = nil // reset since field name is actually the message name
109 case fd.IsWeak() && fd.Message().IsPlaceholder():
110 fd = nil // reset since the weak reference is not linked in
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800111 }
Herbie Ongc525c972018-12-18 18:04:31 -0800112 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800113 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700114 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800115 break
116 }
117 // Extensions have to be registered first in the message's
Herbie Ongc525c972018-12-18 18:04:31 -0800118 // ExtensionTypes before setting a value to it.
Joe Tsai378c1322019-04-25 23:48:08 -0700119 extName := pref.FullName(tkey.String())
Herbie Ong66c365c2019-01-04 14:08:41 -0800120 // Check first if it is already registered. This is the case for
121 // repeated fields.
Joe Tsai378c1322019-04-25 23:48:08 -0700122 xt, err := o.findExtension(extName)
123 if err != nil && err != protoregistry.NotFound {
124 return errors.New("unable to resolve [%v]: %v", extName, err)
Herbie Ongc525c972018-12-18 18:04:31 -0800125 }
Joe Tsai378c1322019-04-25 23:48:08 -0700126 fd = xt
Herbie Ong800c9902018-12-06 15:28:53 -0800127 }
Herbie Ongc525c972018-12-18 18:04:31 -0800128
Herbie Ong800c9902018-12-06 15:28:53 -0800129 if fd == nil {
Herbie Ong7c624e22018-12-13 14:41:22 -0800130 // Ignore reserved names.
131 if reservedNames.Has(name) {
132 continue
133 }
Herbie Ong800c9902018-12-06 15:28:53 -0800134 // TODO: Can provide option to ignore unknown message fields.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700135 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800136 }
137
Joe Tsaiac31a352019-05-13 14:32:56 -0700138 switch {
139 case fd.IsList():
140 // If input is not a list, turn it into a list.
141 var items []text.Value
142 if tval.Type() != text.List {
143 items = []text.Value{tval}
144 } else {
145 items = tval.List()
146 }
147
Joe Tsai378c1322019-04-25 23:48:08 -0700148 list := m.Mutable(fd).List()
Damien Neil8c86fc52019-06-19 09:28:29 -0700149 if err := o.unmarshalList(items, fd, list); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800150 return err
151 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700152 case fd.IsMap():
153 // If input is not a list, turn it into a list.
154 var items []text.Value
155 if tval.Type() != text.List {
156 items = []text.Value{tval}
157 } else {
158 items = tval.List()
159 }
160
Joe Tsai378c1322019-04-25 23:48:08 -0700161 mmap := m.Mutable(fd).Map()
Damien Neil8c86fc52019-06-19 09:28:29 -0700162 if err := o.unmarshalMap(items, fd, mmap); err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700163 return err
164 }
165 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700166 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700167 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700168 idx := uint64(od.Index())
169 if seenOneofs.Has(idx) {
170 return errors.New("oneof %v is already set", od.FullName())
171 }
172 seenOneofs.Set(idx)
173 }
174
Herbie Ong800c9902018-12-06 15:28:53 -0800175 // Required or optional fields.
176 num := uint64(fd.Number())
177 if seenNums.Has(num) {
178 return errors.New("non-repeated field %v is repeated", fd.FullName())
179 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700180 if err := o.unmarshalSingular(tval, fd, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800181 return err
182 }
Herbie Ong800c9902018-12-06 15:28:53 -0800183 seenNums.Set(num)
184 }
185 }
186
Damien Neil8c86fc52019-06-19 09:28:29 -0700187 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800188}
189
Herbie Ong6470ea62019-01-07 18:56:57 -0800190// findExtension returns protoreflect.ExtensionType from the Resolver if found.
191func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
192 xt, err := o.Resolver.FindExtensionByName(xtName)
193 if err == nil {
194 return xt, nil
195 }
196
197 // Check if this is a MessageSet extension field.
198 xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
199 if err == nil && isMessageSetExtension(xt) {
200 return xt, nil
201 }
202 return nil, protoregistry.NotFound
203}
204
Herbie Ong800c9902018-12-06 15:28:53 -0800205// unmarshalSingular unmarshals given text.Value into the non-repeated field.
Joe Tsai378c1322019-04-25 23:48:08 -0700206func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800207 var val pref.Value
208 switch fd.Kind() {
209 case pref.MessageKind, pref.GroupKind:
210 if input.Type() != text.Message {
211 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
212 }
Joe Tsai378c1322019-04-25 23:48:08 -0700213 m2 := m.NewMessage(fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700214 if err := o.unmarshalMessage(input.Message(), m2); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800215 return err
216 }
Joe Tsai378c1322019-04-25 23:48:08 -0700217 val = pref.ValueOf(m2)
Herbie Ong800c9902018-12-06 15:28:53 -0800218 default:
219 var err error
220 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700221 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800222 return err
223 }
224 }
Joe Tsai378c1322019-04-25 23:48:08 -0700225 m.Set(fd, val)
Herbie Ong800c9902018-12-06 15:28:53 -0800226
Damien Neil8c86fc52019-06-19 09:28:29 -0700227 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800228}
229
Herbie Ong800c9902018-12-06 15:28:53 -0800230// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
231// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
232func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
233 const b32 = false
234 const b64 = true
235
236 switch kind := fd.Kind(); kind {
237 case pref.BoolKind:
238 if b, ok := input.Bool(); ok {
239 return pref.ValueOf(bool(b)), nil
240 }
241 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
242 if n, ok := input.Int(b32); ok {
243 return pref.ValueOf(int32(n)), nil
244 }
245 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
246 if n, ok := input.Int(b64); ok {
247 return pref.ValueOf(int64(n)), nil
248 }
249 case pref.Uint32Kind, pref.Fixed32Kind:
250 if n, ok := input.Uint(b32); ok {
251 return pref.ValueOf(uint32(n)), nil
252 }
253 case pref.Uint64Kind, pref.Fixed64Kind:
254 if n, ok := input.Uint(b64); ok {
255 return pref.ValueOf(uint64(n)), nil
256 }
257 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700258 if n, ok := input.Float(b32); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800259 return pref.ValueOf(float32(n)), nil
260 }
261 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700262 if n, ok := input.Float(b64); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800263 return pref.ValueOf(float64(n)), nil
264 }
265 case pref.StringKind:
266 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700267 s := input.String()
268 if utf8.ValidString(s) {
269 return pref.ValueOf(s), nil
270 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700271 return pref.Value{}, errors.InvalidUTF8(string(fd.FullName()))
Herbie Ong800c9902018-12-06 15:28:53 -0800272 }
273 case pref.BytesKind:
274 if input.Type() == text.String {
275 return pref.ValueOf([]byte(input.String())), nil
276 }
277 case pref.EnumKind:
278 // If input is int32, use directly.
279 if n, ok := input.Int(b32); ok {
280 return pref.ValueOf(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800281 }
282 if name, ok := input.Name(); ok {
283 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700284 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800285 return pref.ValueOf(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800286 }
287 }
288 default:
289 panic(fmt.Sprintf("invalid scalar kind %v", kind))
290 }
291
292 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
293}
294
295// unmarshalList unmarshals given []text.Value into given protoreflect.List.
296func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800297 switch fd.Kind() {
298 case pref.MessageKind, pref.GroupKind:
299 for _, input := range inputList {
300 if input.Type() != text.Message {
301 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
302 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800303 m := list.NewMessage()
Damien Neil8c86fc52019-06-19 09:28:29 -0700304 if err := o.unmarshalMessage(input.Message(), m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800305 return err
306 }
307 list.Append(pref.ValueOf(m))
308 }
309 default:
310 for _, input := range inputList {
311 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700312 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800313 return err
314 }
315 list.Append(val)
316 }
317 }
318
Damien Neil8c86fc52019-06-19 09:28:29 -0700319 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800320}
321
322// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
323func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800324 // Determine ahead whether map entry is a scalar type or a message type in order to call the
325 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800326 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700327 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800328 case pref.MessageKind, pref.GroupKind:
329 unmarshalMapValue = o.unmarshalMapMessageValue
330 }
331
332 for _, entry := range input {
333 if entry.Type() != text.Message {
334 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
335 }
336 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
Damien Neil8c86fc52019-06-19 09:28:29 -0700337 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800338 return err
339 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700340 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Damien Neil8c86fc52019-06-19 09:28:29 -0700341 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800342 return err
343 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700344 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Damien Neil8c86fc52019-06-19 09:28:29 -0700345 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800346 return err
347 }
348 }
349
Damien Neil8c86fc52019-06-19 09:28:29 -0700350 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800351}
352
353// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
354// field values. If there are duplicate field names, the value for the last field is returned. If
355// the field name does not exist, it will return the zero value of text.Value. It will return an
356// error if there are unknown field names.
357func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
358 for _, field := range mapEntry {
359 keyStr, ok := field[0].Name()
360 if ok {
361 switch keyStr {
362 case "key":
363 if key.Type() != 0 {
364 return key, value, errors.New("%v contains duplicate key field", name)
365 }
366 key = field[1]
367 case "value":
368 if value.Type() != 0 {
369 return key, value, errors.New("%v contains duplicate value field", name)
370 }
371 value = field[1]
372 default:
373 ok = false
374 }
375 }
376 if !ok {
377 // TODO: Do not return error if ignore unknown option is added and enabled.
378 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
379 }
380 }
381 return key, value, nil
382}
383
384// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
385// integral or string type.
386func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
387 // If input is not set, use the zero value.
388 if input.Type() == 0 {
389 return fd.Default().MapKey(), nil
390 }
391
392 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700393 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800394 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
395 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700396 return val.MapKey(), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800397}
398
399// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
400// the given MapKey.
401func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800402 var value [][2]text.Value
403 if input.Type() != 0 {
404 value = input.Message()
405 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800406 m := mmap.NewMessage()
Damien Neil8c86fc52019-06-19 09:28:29 -0700407 if err := o.unmarshalMessage(value, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800408 return err
409 }
410 mmap.Set(pkey, pref.ValueOf(m))
Damien Neil8c86fc52019-06-19 09:28:29 -0700411 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800412}
413
414// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
415// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800416func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800417 var val pref.Value
418 if input.Type() == 0 {
419 val = fd.Default()
420 } else {
421 var err error
422 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700423 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800424 return err
425 }
426 }
427 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700428 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800429}
Herbie Ong66c365c2019-01-04 14:08:41 -0800430
431// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
432// field with key type of text.String type and value type of text.Message.
433func isExpandedAny(tmsg [][2]text.Value) bool {
434 if len(tmsg) != 1 {
435 return false
436 }
437
438 field := tmsg[0]
439 return field[0].Type() == text.String && field[1].Type() == text.Message
440}
441
442// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
443// tfield has key type of text.String and value type of text.Message.
Joe Tsai378c1322019-04-25 23:48:08 -0700444func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
Herbie Ong66c365c2019-01-04 14:08:41 -0800445 typeURL := tfield[0].String()
446 value := tfield[1].Message()
447
448 mt, err := o.Resolver.FindMessageByURL(typeURL)
Damien Neil8c86fc52019-06-19 09:28:29 -0700449 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800450 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
451 }
452 // Create new message for the embedded message type and unmarshal the
453 // value into it.
Joe Tsai378c1322019-04-25 23:48:08 -0700454 m2 := mt.New()
Damien Neil8c86fc52019-06-19 09:28:29 -0700455 if err := o.unmarshalMessage(value, m2); err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800456 return err
457 }
458 // Serialize the embedded message and assign the resulting bytes to the value field.
Damien Neil96c229a2019-04-03 12:17:24 -0700459 b, err := proto.MarshalOptions{
Damien Neil0c9f0a92019-06-19 10:41:09 -0700460 AllowPartial: true, // never check required fields inside an Any
Damien Neil96c229a2019-04-03 12:17:24 -0700461 Deterministic: true,
Joe Tsai378c1322019-04-25 23:48:08 -0700462 }.Marshal(m2.Interface())
Damien Neil8c86fc52019-06-19 09:28:29 -0700463 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800464 return err
465 }
466
Joe Tsai378c1322019-04-25 23:48:08 -0700467 fds := m.Descriptor().Fields()
468 fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
469 fdValue := fds.ByNumber(fieldnum.Any_Value)
470
471 m.Set(fdType, pref.ValueOf(typeURL))
472 m.Set(fdValue, pref.ValueOf(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800473
Damien Neil8c86fc52019-06-19 09:28:29 -0700474 return nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800475}