blob: a7fc2bfd67b2548db7f62040a339df19377e7f76 [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)
Herbie Ong0dcfb9a2019-01-14 15:32:26 -080098 if fd == nil {
99 // Check if this is a group field.
100 fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
101 }
Herbie Ongc525c972018-12-18 18:04:31 -0800102 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800103 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700104 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800105 break
106 }
107 // Extensions have to be registered first in the message's
Herbie Ongc525c972018-12-18 18:04:31 -0800108 // ExtensionTypes before setting a value to it.
Joe Tsai378c1322019-04-25 23:48:08 -0700109 extName := pref.FullName(tkey.String())
Herbie Ong66c365c2019-01-04 14:08:41 -0800110 // Check first if it is already registered. This is the case for
111 // repeated fields.
Joe Tsai378c1322019-04-25 23:48:08 -0700112 xt, err := o.findExtension(extName)
113 if err != nil && err != protoregistry.NotFound {
114 return errors.New("unable to resolve [%v]: %v", extName, err)
Herbie Ongc525c972018-12-18 18:04:31 -0800115 }
Joe Tsai378c1322019-04-25 23:48:08 -0700116 fd = xt
Herbie Ong800c9902018-12-06 15:28:53 -0800117 }
Herbie Ongc525c972018-12-18 18:04:31 -0800118
Herbie Ong800c9902018-12-06 15:28:53 -0800119 if fd == nil {
Herbie Ong7c624e22018-12-13 14:41:22 -0800120 // Ignore reserved names.
121 if reservedNames.Has(name) {
122 continue
123 }
Herbie Ong800c9902018-12-06 15:28:53 -0800124 // TODO: Can provide option to ignore unknown message fields.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700125 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800126 }
127
Joe Tsaiac31a352019-05-13 14:32:56 -0700128 switch {
129 case fd.IsList():
130 // If input is not a list, turn it into a list.
131 var items []text.Value
132 if tval.Type() != text.List {
133 items = []text.Value{tval}
134 } else {
135 items = tval.List()
136 }
137
Joe Tsai378c1322019-04-25 23:48:08 -0700138 list := m.Mutable(fd).List()
Damien Neil8c86fc52019-06-19 09:28:29 -0700139 if err := o.unmarshalList(items, fd, list); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800140 return err
141 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700142 case fd.IsMap():
143 // If input is not a list, turn it into a list.
144 var items []text.Value
145 if tval.Type() != text.List {
146 items = []text.Value{tval}
147 } else {
148 items = tval.List()
149 }
150
Joe Tsai378c1322019-04-25 23:48:08 -0700151 mmap := m.Mutable(fd).Map()
Damien Neil8c86fc52019-06-19 09:28:29 -0700152 if err := o.unmarshalMap(items, fd, mmap); err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700153 return err
154 }
155 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700156 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700157 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700158 idx := uint64(od.Index())
159 if seenOneofs.Has(idx) {
160 return errors.New("oneof %v is already set", od.FullName())
161 }
162 seenOneofs.Set(idx)
163 }
164
Herbie Ong800c9902018-12-06 15:28:53 -0800165 // Required or optional fields.
166 num := uint64(fd.Number())
167 if seenNums.Has(num) {
168 return errors.New("non-repeated field %v is repeated", fd.FullName())
169 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700170 if err := o.unmarshalSingular(tval, fd, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800171 return err
172 }
Herbie Ong800c9902018-12-06 15:28:53 -0800173 seenNums.Set(num)
174 }
175 }
176
Damien Neil8c86fc52019-06-19 09:28:29 -0700177 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800178}
179
Herbie Ong6470ea62019-01-07 18:56:57 -0800180// findExtension returns protoreflect.ExtensionType from the Resolver if found.
181func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
182 xt, err := o.Resolver.FindExtensionByName(xtName)
183 if err == nil {
184 return xt, nil
185 }
186
187 // Check if this is a MessageSet extension field.
188 xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
189 if err == nil && isMessageSetExtension(xt) {
190 return xt, nil
191 }
192 return nil, protoregistry.NotFound
193}
194
Herbie Ong800c9902018-12-06 15:28:53 -0800195// unmarshalSingular unmarshals given text.Value into the non-repeated field.
Joe Tsai378c1322019-04-25 23:48:08 -0700196func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800197 var val pref.Value
198 switch fd.Kind() {
199 case pref.MessageKind, pref.GroupKind:
200 if input.Type() != text.Message {
201 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
202 }
Joe Tsai378c1322019-04-25 23:48:08 -0700203 m2 := m.NewMessage(fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700204 if err := o.unmarshalMessage(input.Message(), m2); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800205 return err
206 }
Joe Tsai378c1322019-04-25 23:48:08 -0700207 val = pref.ValueOf(m2)
Herbie Ong800c9902018-12-06 15:28:53 -0800208 default:
209 var err error
210 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700211 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800212 return err
213 }
214 }
Joe Tsai378c1322019-04-25 23:48:08 -0700215 m.Set(fd, val)
Herbie Ong800c9902018-12-06 15:28:53 -0800216
Damien Neil8c86fc52019-06-19 09:28:29 -0700217 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800218}
219
Herbie Ong800c9902018-12-06 15:28:53 -0800220// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
221// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
222func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
223 const b32 = false
224 const b64 = true
225
226 switch kind := fd.Kind(); kind {
227 case pref.BoolKind:
228 if b, ok := input.Bool(); ok {
229 return pref.ValueOf(bool(b)), nil
230 }
231 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
232 if n, ok := input.Int(b32); ok {
233 return pref.ValueOf(int32(n)), nil
234 }
235 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
236 if n, ok := input.Int(b64); ok {
237 return pref.ValueOf(int64(n)), nil
238 }
239 case pref.Uint32Kind, pref.Fixed32Kind:
240 if n, ok := input.Uint(b32); ok {
241 return pref.ValueOf(uint32(n)), nil
242 }
243 case pref.Uint64Kind, pref.Fixed64Kind:
244 if n, ok := input.Uint(b64); ok {
245 return pref.ValueOf(uint64(n)), nil
246 }
247 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700248 if n, ok := input.Float(b32); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800249 return pref.ValueOf(float32(n)), nil
250 }
251 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700252 if n, ok := input.Float(b64); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800253 return pref.ValueOf(float64(n)), nil
254 }
255 case pref.StringKind:
256 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700257 s := input.String()
258 if utf8.ValidString(s) {
259 return pref.ValueOf(s), nil
260 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700261 return pref.Value{}, errors.InvalidUTF8(string(fd.FullName()))
Herbie Ong800c9902018-12-06 15:28:53 -0800262 }
263 case pref.BytesKind:
264 if input.Type() == text.String {
265 return pref.ValueOf([]byte(input.String())), nil
266 }
267 case pref.EnumKind:
268 // If input is int32, use directly.
269 if n, ok := input.Int(b32); ok {
270 return pref.ValueOf(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800271 }
272 if name, ok := input.Name(); ok {
273 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700274 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800275 return pref.ValueOf(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800276 }
277 }
278 default:
279 panic(fmt.Sprintf("invalid scalar kind %v", kind))
280 }
281
282 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
283}
284
285// unmarshalList unmarshals given []text.Value into given protoreflect.List.
286func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800287 switch fd.Kind() {
288 case pref.MessageKind, pref.GroupKind:
289 for _, input := range inputList {
290 if input.Type() != text.Message {
291 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
292 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800293 m := list.NewMessage()
Damien Neil8c86fc52019-06-19 09:28:29 -0700294 if err := o.unmarshalMessage(input.Message(), m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800295 return err
296 }
297 list.Append(pref.ValueOf(m))
298 }
299 default:
300 for _, input := range inputList {
301 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700302 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800303 return err
304 }
305 list.Append(val)
306 }
307 }
308
Damien Neil8c86fc52019-06-19 09:28:29 -0700309 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800310}
311
312// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
313func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800314 // Determine ahead whether map entry is a scalar type or a message type in order to call the
315 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800316 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700317 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800318 case pref.MessageKind, pref.GroupKind:
319 unmarshalMapValue = o.unmarshalMapMessageValue
320 }
321
322 for _, entry := range input {
323 if entry.Type() != text.Message {
324 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
325 }
326 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
Damien Neil8c86fc52019-06-19 09:28:29 -0700327 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800328 return err
329 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700330 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Damien Neil8c86fc52019-06-19 09:28:29 -0700331 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800332 return err
333 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700334 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Damien Neil8c86fc52019-06-19 09:28:29 -0700335 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800336 return err
337 }
338 }
339
Damien Neil8c86fc52019-06-19 09:28:29 -0700340 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800341}
342
343// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
344// field values. If there are duplicate field names, the value for the last field is returned. If
345// the field name does not exist, it will return the zero value of text.Value. It will return an
346// error if there are unknown field names.
347func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
348 for _, field := range mapEntry {
349 keyStr, ok := field[0].Name()
350 if ok {
351 switch keyStr {
352 case "key":
353 if key.Type() != 0 {
354 return key, value, errors.New("%v contains duplicate key field", name)
355 }
356 key = field[1]
357 case "value":
358 if value.Type() != 0 {
359 return key, value, errors.New("%v contains duplicate value field", name)
360 }
361 value = field[1]
362 default:
363 ok = false
364 }
365 }
366 if !ok {
367 // TODO: Do not return error if ignore unknown option is added and enabled.
368 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
369 }
370 }
371 return key, value, nil
372}
373
374// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
375// integral or string type.
376func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
377 // If input is not set, use the zero value.
378 if input.Type() == 0 {
379 return fd.Default().MapKey(), nil
380 }
381
382 val, err := unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700383 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800384 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
385 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700386 return val.MapKey(), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800387}
388
389// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
390// the given MapKey.
391func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800392 var value [][2]text.Value
393 if input.Type() != 0 {
394 value = input.Message()
395 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800396 m := mmap.NewMessage()
Damien Neil8c86fc52019-06-19 09:28:29 -0700397 if err := o.unmarshalMessage(value, m); err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800398 return err
399 }
400 mmap.Set(pkey, pref.ValueOf(m))
Damien Neil8c86fc52019-06-19 09:28:29 -0700401 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800402}
403
404// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
405// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800406func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800407 var val pref.Value
408 if input.Type() == 0 {
409 val = fd.Default()
410 } else {
411 var err error
412 val, err = unmarshalScalar(input, fd)
Damien Neil8c86fc52019-06-19 09:28:29 -0700413 if err != nil {
Herbie Ong800c9902018-12-06 15:28:53 -0800414 return err
415 }
416 }
417 mmap.Set(pkey, val)
Damien Neil8c86fc52019-06-19 09:28:29 -0700418 return nil
Herbie Ong800c9902018-12-06 15:28:53 -0800419}
Herbie Ong66c365c2019-01-04 14:08:41 -0800420
421// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
422// field with key type of text.String type and value type of text.Message.
423func isExpandedAny(tmsg [][2]text.Value) bool {
424 if len(tmsg) != 1 {
425 return false
426 }
427
428 field := tmsg[0]
429 return field[0].Type() == text.String && field[1].Type() == text.Message
430}
431
432// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
433// tfield has key type of text.String and value type of text.Message.
Joe Tsai378c1322019-04-25 23:48:08 -0700434func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
Herbie Ong66c365c2019-01-04 14:08:41 -0800435 typeURL := tfield[0].String()
436 value := tfield[1].Message()
437
438 mt, err := o.Resolver.FindMessageByURL(typeURL)
Damien Neil8c86fc52019-06-19 09:28:29 -0700439 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800440 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
441 }
442 // Create new message for the embedded message type and unmarshal the
443 // value into it.
Joe Tsai378c1322019-04-25 23:48:08 -0700444 m2 := mt.New()
Damien Neil8c86fc52019-06-19 09:28:29 -0700445 if err := o.unmarshalMessage(value, m2); err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800446 return err
447 }
448 // Serialize the embedded message and assign the resulting bytes to the value field.
Damien Neil96c229a2019-04-03 12:17:24 -0700449 b, err := proto.MarshalOptions{
Damien Neil0c9f0a92019-06-19 10:41:09 -0700450 AllowPartial: true, // never check required fields inside an Any
Damien Neil96c229a2019-04-03 12:17:24 -0700451 Deterministic: true,
Joe Tsai378c1322019-04-25 23:48:08 -0700452 }.Marshal(m2.Interface())
Damien Neil8c86fc52019-06-19 09:28:29 -0700453 if err != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800454 return err
455 }
456
Joe Tsai378c1322019-04-25 23:48:08 -0700457 fds := m.Descriptor().Fields()
458 fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
459 fdValue := fds.ByNumber(fieldnum.Any_Value)
460
461 m.Set(fdType, pref.ValueOf(typeURL))
462 m.Set(fdValue, pref.ValueOf(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800463
Damien Neil8c86fc52019-06-19 09:28:29 -0700464 return nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800465}