blob: 4444de6038abb0eed30592d97a0a828dba9bc424 [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 var nerr errors.NonFatal
49
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)
58 if !nerr.Merge(err) {
59 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())
Herbie Ong800c9902018-12-06 15:28:53 -080066 if !nerr.Merge(err) {
67 return err
68 }
69
Damien Neil4686e232019-04-05 13:31:40 -070070 if !o.AllowPartial {
71 nerr.Merge(proto.IsInitialized(m))
72 }
73
Herbie Ong800c9902018-12-06 15:28:53 -080074 return nerr.E
75}
76
Herbie Ong800c9902018-12-06 15:28:53 -080077// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
78func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
79 var nerr errors.NonFatal
80
Joe Tsai0fc49f82019-05-01 12:29:25 -070081 messageDesc := m.Descriptor()
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
Joe Tsai0fc49f82019-05-01 12:29:25 -070088 fieldDescs := messageDesc.Fields()
89 reservedNames := messageDesc.ReservedNames()
Herbie Ong800c9902018-12-06 15:28:53 -080090 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -070091 var seenOneofs set.Ints
Herbie Ong800c9902018-12-06 15:28:53 -080092
93 for _, tfield := range tmsg {
94 tkey := tfield[0]
95 tval := tfield[1]
96
97 var fd pref.FieldDescriptor
Herbie Ongc525c972018-12-18 18:04:31 -080098 var name pref.Name
99 switch tkey.Type() {
100 case text.Name:
101 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -0800102 fd = fieldDescs.ByName(name)
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800103 if fd == nil {
104 // Check if this is a group field.
105 fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
106 }
Herbie Ongc525c972018-12-18 18:04:31 -0800107 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800108 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700109 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800110 break
111 }
112 // Extensions have to be registered first in the message's
Herbie Ongc525c972018-12-18 18:04:31 -0800113 // ExtensionTypes before setting a value to it.
Joe Tsai378c1322019-04-25 23:48:08 -0700114 extName := pref.FullName(tkey.String())
Herbie Ong66c365c2019-01-04 14:08:41 -0800115 // Check first if it is already registered. This is the case for
116 // repeated fields.
Joe Tsai378c1322019-04-25 23:48:08 -0700117 xt, err := o.findExtension(extName)
118 if err != nil && err != protoregistry.NotFound {
119 return errors.New("unable to resolve [%v]: %v", extName, err)
Herbie Ongc525c972018-12-18 18:04:31 -0800120 }
Joe Tsai378c1322019-04-25 23:48:08 -0700121 fd = xt
Herbie Ong800c9902018-12-06 15:28:53 -0800122 }
Herbie Ongc525c972018-12-18 18:04:31 -0800123
Herbie Ong800c9902018-12-06 15:28:53 -0800124 if fd == nil {
Herbie Ong7c624e22018-12-13 14:41:22 -0800125 // Ignore reserved names.
126 if reservedNames.Has(name) {
127 continue
128 }
Herbie Ong800c9902018-12-06 15:28:53 -0800129 // TODO: Can provide option to ignore unknown message fields.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700130 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800131 }
132
Joe Tsaiac31a352019-05-13 14:32:56 -0700133 switch {
134 case fd.IsList():
135 // If input is not a list, turn it into a list.
136 var items []text.Value
137 if tval.Type() != text.List {
138 items = []text.Value{tval}
139 } else {
140 items = tval.List()
141 }
142
Joe Tsai378c1322019-04-25 23:48:08 -0700143 list := m.Mutable(fd).List()
Joe Tsaiac31a352019-05-13 14:32:56 -0700144 if err := o.unmarshalList(items, fd, list); !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800145 return err
146 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700147 case fd.IsMap():
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 mmap := m.Mutable(fd).Map()
Joe Tsaiac31a352019-05-13 14:32:56 -0700157 if err := o.unmarshalMap(items, fd, mmap); !nerr.Merge(err) {
158 return err
159 }
160 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700161 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700162 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700163 idx := uint64(od.Index())
164 if seenOneofs.Has(idx) {
165 return errors.New("oneof %v is already set", od.FullName())
166 }
167 seenOneofs.Set(idx)
168 }
169
Herbie Ong800c9902018-12-06 15:28:53 -0800170 // Required or optional fields.
171 num := uint64(fd.Number())
172 if seenNums.Has(num) {
173 return errors.New("non-repeated field %v is repeated", fd.FullName())
174 }
Joe Tsai378c1322019-04-25 23:48:08 -0700175 if err := o.unmarshalSingular(tval, fd, m); !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800176 return err
177 }
Herbie Ong800c9902018-12-06 15:28:53 -0800178 seenNums.Set(num)
179 }
180 }
181
Herbie Ong800c9902018-12-06 15:28:53 -0800182 return nerr.E
183}
184
Herbie Ong6470ea62019-01-07 18:56:57 -0800185// findExtension returns protoreflect.ExtensionType from the Resolver if found.
186func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
187 xt, err := o.Resolver.FindExtensionByName(xtName)
188 if err == nil {
189 return xt, nil
190 }
191
192 // Check if this is a MessageSet extension field.
193 xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
194 if err == nil && isMessageSetExtension(xt) {
195 return xt, nil
196 }
197 return nil, protoregistry.NotFound
198}
199
Herbie Ong800c9902018-12-06 15:28:53 -0800200// unmarshalSingular unmarshals given text.Value into the non-repeated field.
Joe Tsai378c1322019-04-25 23:48:08 -0700201func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, m pref.Message) error {
Herbie Ong800c9902018-12-06 15:28:53 -0800202 var nerr errors.NonFatal
203 var val pref.Value
204 switch fd.Kind() {
205 case pref.MessageKind, pref.GroupKind:
206 if input.Type() != text.Message {
207 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
208 }
Joe Tsai378c1322019-04-25 23:48:08 -0700209 m2 := m.NewMessage(fd)
210 if err := o.unmarshalMessage(input.Message(), m2); !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800211 return err
212 }
Joe Tsai378c1322019-04-25 23:48:08 -0700213 val = pref.ValueOf(m2)
Herbie Ong800c9902018-12-06 15:28:53 -0800214 default:
215 var err error
216 val, err = unmarshalScalar(input, fd)
217 if !nerr.Merge(err) {
218 return err
219 }
220 }
Joe Tsai378c1322019-04-25 23:48:08 -0700221 m.Set(fd, val)
Herbie Ong800c9902018-12-06 15:28:53 -0800222
223 return nerr.E
224}
225
Herbie Ong800c9902018-12-06 15:28:53 -0800226// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
227// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
228func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
229 const b32 = false
230 const b64 = true
231
232 switch kind := fd.Kind(); kind {
233 case pref.BoolKind:
234 if b, ok := input.Bool(); ok {
235 return pref.ValueOf(bool(b)), nil
236 }
237 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
238 if n, ok := input.Int(b32); ok {
239 return pref.ValueOf(int32(n)), nil
240 }
241 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
242 if n, ok := input.Int(b64); ok {
243 return pref.ValueOf(int64(n)), nil
244 }
245 case pref.Uint32Kind, pref.Fixed32Kind:
246 if n, ok := input.Uint(b32); ok {
247 return pref.ValueOf(uint32(n)), nil
248 }
249 case pref.Uint64Kind, pref.Fixed64Kind:
250 if n, ok := input.Uint(b64); ok {
251 return pref.ValueOf(uint64(n)), nil
252 }
253 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700254 if n, ok := input.Float(b32); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800255 return pref.ValueOf(float32(n)), nil
256 }
257 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700258 if n, ok := input.Float(b64); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800259 return pref.ValueOf(float64(n)), nil
260 }
261 case pref.StringKind:
262 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700263 s := input.String()
264 if utf8.ValidString(s) {
265 return pref.ValueOf(s), nil
266 }
267 var nerr errors.NonFatal
268 nerr.AppendInvalidUTF8(string(fd.FullName()))
269 return pref.ValueOf(s), nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800270 }
271 case pref.BytesKind:
272 if input.Type() == text.String {
273 return pref.ValueOf([]byte(input.String())), nil
274 }
275 case pref.EnumKind:
276 // If input is int32, use directly.
277 if n, ok := input.Int(b32); ok {
278 return pref.ValueOf(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800279 }
280 if name, ok := input.Name(); ok {
281 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700282 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800283 return pref.ValueOf(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800284 }
285 }
286 default:
287 panic(fmt.Sprintf("invalid scalar kind %v", kind))
288 }
289
290 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
291}
292
293// unmarshalList unmarshals given []text.Value into given protoreflect.List.
294func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
295 var nerr errors.NonFatal
296
297 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()
Herbie Ong800c9902018-12-06 15:28:53 -0800304 if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
305 return err
306 }
307 list.Append(pref.ValueOf(m))
308 }
309 default:
310 for _, input := range inputList {
311 val, err := unmarshalScalar(input, fd)
312 if !nerr.Merge(err) {
313 return err
314 }
315 list.Append(val)
316 }
317 }
318
319 return nerr.E
320}
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 {
324 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800325
326 // Determine ahead whether map entry is a scalar type or a message type in order to call the
327 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800328 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700329 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800330 case pref.MessageKind, pref.GroupKind:
331 unmarshalMapValue = o.unmarshalMapMessageValue
332 }
333
334 for _, entry := range input {
335 if entry.Type() != text.Message {
336 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
337 }
338 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
339 if !nerr.Merge(err) {
340 return err
341 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700342 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Herbie Ong800c9902018-12-06 15:28:53 -0800343 if !nerr.Merge(err) {
344 return err
345 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700346 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Herbie Ong800c9902018-12-06 15:28:53 -0800347 if !nerr.Merge(err) {
348 return err
349 }
350 }
351
352 return nerr.E
353}
354
355// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
356// field values. If there are duplicate field names, the value for the last field is returned. If
357// the field name does not exist, it will return the zero value of text.Value. It will return an
358// error if there are unknown field names.
359func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
360 for _, field := range mapEntry {
361 keyStr, ok := field[0].Name()
362 if ok {
363 switch keyStr {
364 case "key":
365 if key.Type() != 0 {
366 return key, value, errors.New("%v contains duplicate key field", name)
367 }
368 key = field[1]
369 case "value":
370 if value.Type() != 0 {
371 return key, value, errors.New("%v contains duplicate value field", name)
372 }
373 value = field[1]
374 default:
375 ok = false
376 }
377 }
378 if !ok {
379 // TODO: Do not return error if ignore unknown option is added and enabled.
380 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
381 }
382 }
383 return key, value, nil
384}
385
386// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
387// integral or string type.
388func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
389 // If input is not set, use the zero value.
390 if input.Type() == 0 {
391 return fd.Default().MapKey(), nil
392 }
393
Herbie Ong21a39742019-04-08 17:32:44 -0700394 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800395 val, err := unmarshalScalar(input, fd)
Herbie Ong21a39742019-04-08 17:32:44 -0700396 if !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800397 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
398 }
Herbie Ong21a39742019-04-08 17:32:44 -0700399 return val.MapKey(), nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800400}
401
402// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
403// the given MapKey.
404func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
405 var nerr errors.NonFatal
406 var value [][2]text.Value
407 if input.Type() != 0 {
408 value = input.Message()
409 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800410 m := mmap.NewMessage()
Herbie Ong800c9902018-12-06 15:28:53 -0800411 if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
412 return err
413 }
414 mmap.Set(pkey, pref.ValueOf(m))
415 return nerr.E
416}
417
418// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
419// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800420func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong21a39742019-04-08 17:32:44 -0700421 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800422 var val pref.Value
423 if input.Type() == 0 {
424 val = fd.Default()
425 } else {
426 var err error
427 val, err = unmarshalScalar(input, fd)
Herbie Ong21a39742019-04-08 17:32:44 -0700428 if !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800429 return err
430 }
431 }
432 mmap.Set(pkey, val)
Herbie Ong21a39742019-04-08 17:32:44 -0700433 return nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800434}
Herbie Ong66c365c2019-01-04 14:08:41 -0800435
436// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
437// field with key type of text.String type and value type of text.Message.
438func isExpandedAny(tmsg [][2]text.Value) bool {
439 if len(tmsg) != 1 {
440 return false
441 }
442
443 field := tmsg[0]
444 return field[0].Type() == text.String && field[1].Type() == text.Message
445}
446
447// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
448// tfield has key type of text.String and value type of text.Message.
Joe Tsai378c1322019-04-25 23:48:08 -0700449func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, m pref.Message) error {
Herbie Ong66c365c2019-01-04 14:08:41 -0800450 var nerr errors.NonFatal
451
452 typeURL := tfield[0].String()
453 value := tfield[1].Message()
454
455 mt, err := o.Resolver.FindMessageByURL(typeURL)
456 if !nerr.Merge(err) {
457 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
458 }
459 // Create new message for the embedded message type and unmarshal the
460 // value into it.
Joe Tsai378c1322019-04-25 23:48:08 -0700461 m2 := mt.New()
462 if err := o.unmarshalMessage(value, m2); !nerr.Merge(err) {
Herbie Ong66c365c2019-01-04 14:08:41 -0800463 return err
464 }
465 // Serialize the embedded message and assign the resulting bytes to the value field.
Herbie Ong9c100452019-05-06 18:45:33 -0700466 // TODO: If binary marshaling returns required not set error, need to
467 // return another required not set error that contains both the path to this
468 // field and the path inside the embedded message.
Damien Neil96c229a2019-04-03 12:17:24 -0700469 b, err := proto.MarshalOptions{
470 AllowPartial: o.AllowPartial,
471 Deterministic: true,
Joe Tsai378c1322019-04-25 23:48:08 -0700472 }.Marshal(m2.Interface())
Herbie Ong66c365c2019-01-04 14:08:41 -0800473 if !nerr.Merge(err) {
474 return err
475 }
476
Joe Tsai378c1322019-04-25 23:48:08 -0700477 fds := m.Descriptor().Fields()
478 fdType := fds.ByNumber(fieldnum.Any_TypeUrl)
479 fdValue := fds.ByNumber(fieldnum.Any_Value)
480
481 m.Set(fdType, pref.ValueOf(typeURL))
482 m.Set(fdValue, pref.ValueOf(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800483
484 return nerr.E
485}