blob: 20bdfe6b902f417d34453b46544aaf03323ad5b4 [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
50 mr := m.ProtoReflect()
51 // Clear all fields before populating it.
Damien Neil5c5b5312019-05-14 12:44:37 -070052 // TODO: Determine if this needs to be consistent with protojson and binary unmarshal where
Herbie Ong800c9902018-12-06 15:28:53 -080053 // behavior is to merge values into existing message. If decision is to not clear the fields
54 // ahead, code will need to be updated properly when merging nested messages.
55 resetMessage(mr)
56
57 // Parse into text.Value of message type.
58 val, err := text.Unmarshal(b)
59 if !nerr.Merge(err) {
60 return err
61 }
62
Herbie Ongc525c972018-12-18 18:04:31 -080063 if o.Resolver == nil {
64 o.Resolver = protoregistry.GlobalTypes
65 }
Herbie Ong800c9902018-12-06 15:28:53 -080066 err = o.unmarshalMessage(val.Message(), mr)
67 if !nerr.Merge(err) {
68 return err
69 }
70
Damien Neil4686e232019-04-05 13:31:40 -070071 if !o.AllowPartial {
72 nerr.Merge(proto.IsInitialized(m))
73 }
74
Herbie Ong800c9902018-12-06 15:28:53 -080075 return nerr.E
76}
77
78// resetMessage clears all fields of given protoreflect.Message.
79// TODO: This should go into the proto package.
80func resetMessage(m pref.Message) {
81 knownFields := m.KnownFields()
82 knownFields.Range(func(num pref.FieldNumber, _ pref.Value) bool {
83 knownFields.Clear(num)
84 return true
85 })
86 unknownFields := m.UnknownFields()
87 unknownFields.Range(func(num pref.FieldNumber, _ pref.RawFields) bool {
88 unknownFields.Set(num, nil)
89 return true
90 })
Herbie Ong800c9902018-12-06 15:28:53 -080091 extTypes := knownFields.ExtensionTypes()
92 extTypes.Range(func(xt pref.ExtensionType) bool {
93 extTypes.Remove(xt)
94 return true
95 })
96}
97
98// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
99func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
100 var nerr errors.NonFatal
101
Joe Tsai0fc49f82019-05-01 12:29:25 -0700102 messageDesc := m.Descriptor()
Herbie Ong66c365c2019-01-04 14:08:41 -0800103 knownFields := m.KnownFields()
104
105 // Handle expanded Any message.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700106 if messageDesc.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
Herbie Ong66c365c2019-01-04 14:08:41 -0800107 return o.unmarshalAny(tmsg[0], knownFields)
108 }
109
Joe Tsai0fc49f82019-05-01 12:29:25 -0700110 fieldDescs := messageDesc.Fields()
111 reservedNames := messageDesc.ReservedNames()
Herbie Ongc525c972018-12-18 18:04:31 -0800112 xtTypes := knownFields.ExtensionTypes()
Herbie Ong800c9902018-12-06 15:28:53 -0800113 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -0700114 var seenOneofs set.Ints
Herbie Ong800c9902018-12-06 15:28:53 -0800115
116 for _, tfield := range tmsg {
117 tkey := tfield[0]
118 tval := tfield[1]
119
120 var fd pref.FieldDescriptor
Herbie Ongc525c972018-12-18 18:04:31 -0800121 var name pref.Name
122 switch tkey.Type() {
123 case text.Name:
124 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -0800125 fd = fieldDescs.ByName(name)
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800126 if fd == nil {
127 // Check if this is a group field.
128 fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
129 }
Herbie Ongc525c972018-12-18 18:04:31 -0800130 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800131 // Handle extensions only. This code path is not for Any.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700132 if messageDesc.FullName() == "google.protobuf.Any" {
Herbie Ong66c365c2019-01-04 14:08:41 -0800133 break
134 }
135 // Extensions have to be registered first in the message's
Herbie Ongc525c972018-12-18 18:04:31 -0800136 // ExtensionTypes before setting a value to it.
137 xtName := pref.FullName(tkey.String())
Herbie Ong66c365c2019-01-04 14:08:41 -0800138 // Check first if it is already registered. This is the case for
139 // repeated fields.
Herbie Ongc525c972018-12-18 18:04:31 -0800140 xt := xtTypes.ByName(xtName)
141 if xt == nil {
142 var err error
Herbie Ong6470ea62019-01-07 18:56:57 -0800143 xt, err = o.findExtension(xtName)
Herbie Ongc525c972018-12-18 18:04:31 -0800144 if err != nil && err != protoregistry.NotFound {
Herbie Ong66c365c2019-01-04 14:08:41 -0800145 return errors.New("unable to resolve [%v]: %v", xtName, err)
Herbie Ongc525c972018-12-18 18:04:31 -0800146 }
147 if xt != nil {
148 xtTypes.Register(xt)
149 }
150 }
Joe Tsai0fc49f82019-05-01 12:29:25 -0700151 if xt != nil {
152 fd = xt.Descriptor()
153 }
Herbie Ong800c9902018-12-06 15:28:53 -0800154 }
Herbie Ongc525c972018-12-18 18:04:31 -0800155
Herbie Ong800c9902018-12-06 15:28:53 -0800156 if fd == nil {
Herbie Ong7c624e22018-12-13 14:41:22 -0800157 // Ignore reserved names.
158 if reservedNames.Has(name) {
159 continue
160 }
Herbie Ong800c9902018-12-06 15:28:53 -0800161 // TODO: Can provide option to ignore unknown message fields.
Joe Tsai0fc49f82019-05-01 12:29:25 -0700162 return errors.New("%v contains unknown field: %v", messageDesc.FullName(), tkey)
Herbie Ong800c9902018-12-06 15:28:53 -0800163 }
164
Joe Tsaiac31a352019-05-13 14:32:56 -0700165 switch {
166 case fd.IsList():
167 // If input is not a list, turn it into a list.
168 var items []text.Value
169 if tval.Type() != text.List {
170 items = []text.Value{tval}
171 } else {
172 items = tval.List()
173 }
174
175 list := knownFields.Get(fd.Number()).List()
176 if err := o.unmarshalList(items, fd, list); !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800177 return err
178 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700179 case fd.IsMap():
180 // If input is not a list, turn it into a list.
181 var items []text.Value
182 if tval.Type() != text.List {
183 items = []text.Value{tval}
184 } else {
185 items = tval.List()
186 }
187
188 mmap := knownFields.Get(fd.Number()).Map()
189 if err := o.unmarshalMap(items, fd, mmap); !nerr.Merge(err) {
190 return err
191 }
192 default:
Herbie Ong8a1d4602019-04-02 20:19:36 -0700193 // If field is a oneof, check if it has already been set.
Joe Tsaiac31a352019-05-13 14:32:56 -0700194 if od := fd.ContainingOneof(); od != nil {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700195 idx := uint64(od.Index())
196 if seenOneofs.Has(idx) {
197 return errors.New("oneof %v is already set", od.FullName())
198 }
199 seenOneofs.Set(idx)
200 }
201
Herbie Ong800c9902018-12-06 15:28:53 -0800202 // Required or optional fields.
203 num := uint64(fd.Number())
204 if seenNums.Has(num) {
205 return errors.New("non-repeated field %v is repeated", fd.FullName())
206 }
207 if err := o.unmarshalSingular(tval, fd, knownFields); !nerr.Merge(err) {
208 return err
209 }
Herbie Ong800c9902018-12-06 15:28:53 -0800210 seenNums.Set(num)
211 }
212 }
213
Herbie Ong800c9902018-12-06 15:28:53 -0800214 return nerr.E
215}
216
Herbie Ong6470ea62019-01-07 18:56:57 -0800217// findExtension returns protoreflect.ExtensionType from the Resolver if found.
218func (o UnmarshalOptions) findExtension(xtName pref.FullName) (pref.ExtensionType, error) {
219 xt, err := o.Resolver.FindExtensionByName(xtName)
220 if err == nil {
221 return xt, nil
222 }
223
224 // Check if this is a MessageSet extension field.
225 xt, err = o.Resolver.FindExtensionByName(xtName + ".message_set_extension")
226 if err == nil && isMessageSetExtension(xt) {
227 return xt, nil
228 }
229 return nil, protoregistry.NotFound
230}
231
Herbie Ong800c9902018-12-06 15:28:53 -0800232// unmarshalSingular unmarshals given text.Value into the non-repeated field.
233func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, knownFields pref.KnownFields) error {
234 num := fd.Number()
235
236 var nerr errors.NonFatal
237 var val pref.Value
238 switch fd.Kind() {
239 case pref.MessageKind, pref.GroupKind:
240 if input.Type() != text.Message {
241 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
242 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800243 m := knownFields.NewMessage(num)
Herbie Ong800c9902018-12-06 15:28:53 -0800244 if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
245 return err
246 }
247 val = pref.ValueOf(m)
248 default:
249 var err error
250 val, err = unmarshalScalar(input, fd)
251 if !nerr.Merge(err) {
252 return err
253 }
254 }
255 knownFields.Set(num, val)
256
257 return nerr.E
258}
259
Herbie Ong800c9902018-12-06 15:28:53 -0800260// unmarshalScalar converts the given text.Value to a scalar/enum protoreflect.Value specified in
261// the given FieldDescriptor. Caller should not pass in a FieldDescriptor for a message/group kind.
262func unmarshalScalar(input text.Value, fd pref.FieldDescriptor) (pref.Value, error) {
263 const b32 = false
264 const b64 = true
265
266 switch kind := fd.Kind(); kind {
267 case pref.BoolKind:
268 if b, ok := input.Bool(); ok {
269 return pref.ValueOf(bool(b)), nil
270 }
271 case pref.Int32Kind, pref.Sint32Kind, pref.Sfixed32Kind:
272 if n, ok := input.Int(b32); ok {
273 return pref.ValueOf(int32(n)), nil
274 }
275 case pref.Int64Kind, pref.Sint64Kind, pref.Sfixed64Kind:
276 if n, ok := input.Int(b64); ok {
277 return pref.ValueOf(int64(n)), nil
278 }
279 case pref.Uint32Kind, pref.Fixed32Kind:
280 if n, ok := input.Uint(b32); ok {
281 return pref.ValueOf(uint32(n)), nil
282 }
283 case pref.Uint64Kind, pref.Fixed64Kind:
284 if n, ok := input.Uint(b64); ok {
285 return pref.ValueOf(uint64(n)), nil
286 }
287 case pref.FloatKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700288 if n, ok := input.Float(b32); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800289 return pref.ValueOf(float32(n)), nil
290 }
291 case pref.DoubleKind:
Herbie Ong250c6ea2019-03-12 20:55:10 -0700292 if n, ok := input.Float(b64); ok {
Herbie Ong800c9902018-12-06 15:28:53 -0800293 return pref.ValueOf(float64(n)), nil
294 }
295 case pref.StringKind:
296 if input.Type() == text.String {
Herbie Ong21a39742019-04-08 17:32:44 -0700297 s := input.String()
298 if utf8.ValidString(s) {
299 return pref.ValueOf(s), nil
300 }
301 var nerr errors.NonFatal
302 nerr.AppendInvalidUTF8(string(fd.FullName()))
303 return pref.ValueOf(s), nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800304 }
305 case pref.BytesKind:
306 if input.Type() == text.String {
307 return pref.ValueOf([]byte(input.String())), nil
308 }
309 case pref.EnumKind:
310 // If input is int32, use directly.
311 if n, ok := input.Int(b32); ok {
312 return pref.ValueOf(pref.EnumNumber(n)), nil
Herbie Ong66c365c2019-01-04 14:08:41 -0800313 }
314 if name, ok := input.Name(); ok {
315 // Lookup EnumNumber based on name.
Joe Tsaid24bc722019-04-15 23:39:09 -0700316 if enumVal := fd.Enum().Values().ByName(name); enumVal != nil {
Herbie Ong66c365c2019-01-04 14:08:41 -0800317 return pref.ValueOf(enumVal.Number()), nil
Herbie Ong800c9902018-12-06 15:28:53 -0800318 }
319 }
320 default:
321 panic(fmt.Sprintf("invalid scalar kind %v", kind))
322 }
323
324 return pref.Value{}, errors.New("%v contains invalid scalar value: %v", fd.FullName(), input)
325}
326
327// unmarshalList unmarshals given []text.Value into given protoreflect.List.
328func (o UnmarshalOptions) unmarshalList(inputList []text.Value, fd pref.FieldDescriptor, list pref.List) error {
329 var nerr errors.NonFatal
330
331 switch fd.Kind() {
332 case pref.MessageKind, pref.GroupKind:
333 for _, input := range inputList {
334 if input.Type() != text.Message {
335 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
336 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800337 m := list.NewMessage()
Herbie Ong800c9902018-12-06 15:28:53 -0800338 if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
339 return err
340 }
341 list.Append(pref.ValueOf(m))
342 }
343 default:
344 for _, input := range inputList {
345 val, err := unmarshalScalar(input, fd)
346 if !nerr.Merge(err) {
347 return err
348 }
349 list.Append(val)
350 }
351 }
352
353 return nerr.E
354}
355
356// unmarshalMap unmarshals given []text.Value into given protoreflect.Map.
357func (o UnmarshalOptions) unmarshalMap(input []text.Value, fd pref.FieldDescriptor, mmap pref.Map) error {
358 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800359
360 // Determine ahead whether map entry is a scalar type or a message type in order to call the
361 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800362 unmarshalMapValue := unmarshalMapScalarValue
Joe Tsaiac31a352019-05-13 14:32:56 -0700363 switch fd.MapValue().Kind() {
Herbie Ong800c9902018-12-06 15:28:53 -0800364 case pref.MessageKind, pref.GroupKind:
365 unmarshalMapValue = o.unmarshalMapMessageValue
366 }
367
368 for _, entry := range input {
369 if entry.Type() != text.Message {
370 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
371 }
372 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
373 if !nerr.Merge(err) {
374 return err
375 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700376 pkey, err := unmarshalMapKey(tkey, fd.MapKey())
Herbie Ong800c9902018-12-06 15:28:53 -0800377 if !nerr.Merge(err) {
378 return err
379 }
Joe Tsaiac31a352019-05-13 14:32:56 -0700380 err = unmarshalMapValue(tval, pkey, fd.MapValue(), mmap)
Herbie Ong800c9902018-12-06 15:28:53 -0800381 if !nerr.Merge(err) {
382 return err
383 }
384 }
385
386 return nerr.E
387}
388
389// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
390// field values. If there are duplicate field names, the value for the last field is returned. If
391// the field name does not exist, it will return the zero value of text.Value. It will return an
392// error if there are unknown field names.
393func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
394 for _, field := range mapEntry {
395 keyStr, ok := field[0].Name()
396 if ok {
397 switch keyStr {
398 case "key":
399 if key.Type() != 0 {
400 return key, value, errors.New("%v contains duplicate key field", name)
401 }
402 key = field[1]
403 case "value":
404 if value.Type() != 0 {
405 return key, value, errors.New("%v contains duplicate value field", name)
406 }
407 value = field[1]
408 default:
409 ok = false
410 }
411 }
412 if !ok {
413 // TODO: Do not return error if ignore unknown option is added and enabled.
414 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
415 }
416 }
417 return key, value, nil
418}
419
420// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
421// integral or string type.
422func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
423 // If input is not set, use the zero value.
424 if input.Type() == 0 {
425 return fd.Default().MapKey(), nil
426 }
427
Herbie Ong21a39742019-04-08 17:32:44 -0700428 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800429 val, err := unmarshalScalar(input, fd)
Herbie Ong21a39742019-04-08 17:32:44 -0700430 if !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800431 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
432 }
Herbie Ong21a39742019-04-08 17:32:44 -0700433 return val.MapKey(), nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800434}
435
436// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
437// the given MapKey.
438func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
439 var nerr errors.NonFatal
440 var value [][2]text.Value
441 if input.Type() != 0 {
442 value = input.Message()
443 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800444 m := mmap.NewMessage()
Herbie Ong800c9902018-12-06 15:28:53 -0800445 if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
446 return err
447 }
448 mmap.Set(pkey, pref.ValueOf(m))
449 return nerr.E
450}
451
452// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
453// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800454func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong21a39742019-04-08 17:32:44 -0700455 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800456 var val pref.Value
457 if input.Type() == 0 {
458 val = fd.Default()
459 } else {
460 var err error
461 val, err = unmarshalScalar(input, fd)
Herbie Ong21a39742019-04-08 17:32:44 -0700462 if !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800463 return err
464 }
465 }
466 mmap.Set(pkey, val)
Herbie Ong21a39742019-04-08 17:32:44 -0700467 return nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800468}
Herbie Ong66c365c2019-01-04 14:08:41 -0800469
470// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
471// field with key type of text.String type and value type of text.Message.
472func isExpandedAny(tmsg [][2]text.Value) bool {
473 if len(tmsg) != 1 {
474 return false
475 }
476
477 field := tmsg[0]
478 return field[0].Type() == text.String && field[1].Type() == text.Message
479}
480
481// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
482// tfield has key type of text.String and value type of text.Message.
483func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, knownFields pref.KnownFields) error {
484 var nerr errors.NonFatal
485
486 typeURL := tfield[0].String()
487 value := tfield[1].Message()
488
489 mt, err := o.Resolver.FindMessageByURL(typeURL)
490 if !nerr.Merge(err) {
491 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
492 }
493 // Create new message for the embedded message type and unmarshal the
494 // value into it.
495 m := mt.New()
496 if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
497 return err
498 }
499 // Serialize the embedded message and assign the resulting bytes to the value field.
Herbie Ong9c100452019-05-06 18:45:33 -0700500 // TODO: If binary marshaling returns required not set error, need to
501 // return another required not set error that contains both the path to this
502 // field and the path inside the embedded message.
Damien Neil96c229a2019-04-03 12:17:24 -0700503 b, err := proto.MarshalOptions{
504 AllowPartial: o.AllowPartial,
505 Deterministic: true,
506 }.Marshal(m.Interface())
Herbie Ong66c365c2019-01-04 14:08:41 -0800507 if !nerr.Merge(err) {
508 return err
509 }
510
Herbie Onge1e34932019-03-29 01:05:57 -0700511 knownFields.Set(fieldnum.Any_TypeUrl, pref.ValueOf(typeURL))
512 knownFields.Set(fieldnum.Any_Value, pref.ValueOf(b))
Herbie Ong66c365c2019-01-04 14:08:41 -0800513
514 return nerr.E
515}