blob: 218d95f30f002d477348b5f5f59c6031c6ccc49e [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
5package textpb
6
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
12 "github.com/golang/protobuf/v2/internal/encoding/text"
13 "github.com/golang/protobuf/v2/internal/errors"
Herbie Onge1e34932019-03-29 01:05:57 -070014 "github.com/golang/protobuf/v2/internal/fieldnum"
Herbie Ong800c9902018-12-06 15:28:53 -080015 "github.com/golang/protobuf/v2/internal/pragma"
16 "github.com/golang/protobuf/v2/internal/set"
17 "github.com/golang/protobuf/v2/proto"
18 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
Herbie Ongc525c972018-12-18 18:04:31 -080019 "github.com/golang/protobuf/v2/reflect/protoregistry"
Herbie Ong800c9902018-12-06 15:28:53 -080020)
21
22// Unmarshal reads the given []byte into the given proto.Message.
Herbie Ong800c9902018-12-06 15:28:53 -080023func Unmarshal(m proto.Message, b []byte) error {
24 return UnmarshalOptions{}.Unmarshal(m, b)
25}
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
Herbie Ongc525c972018-12-18 18:04:31 -080036 // Resolver is the registry used for type lookups when unmarshaling extensions
37 // and processing Any. If Resolver is not set, unmarshaling will default to
38 // using protoregistry.GlobalTypes.
39 Resolver *protoregistry.Types
Herbie Ong800c9902018-12-06 15:28:53 -080040}
41
42// Unmarshal reads the given []byte and populates the given proto.Message using options in
43// UnmarshalOptions object.
44func (o UnmarshalOptions) Unmarshal(m proto.Message, b []byte) error {
45 var nerr errors.NonFatal
46
47 mr := m.ProtoReflect()
48 // Clear all fields before populating it.
49 // TODO: Determine if this needs to be consistent with jsonpb and binary unmarshal where
50 // 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.
52 resetMessage(mr)
53
54 // Parse into text.Value of message type.
55 val, err := text.Unmarshal(b)
56 if !nerr.Merge(err) {
57 return err
58 }
59
Herbie Ongc525c972018-12-18 18:04:31 -080060 if o.Resolver == nil {
61 o.Resolver = protoregistry.GlobalTypes
62 }
Herbie Ong800c9902018-12-06 15:28:53 -080063 err = o.unmarshalMessage(val.Message(), mr)
64 if !nerr.Merge(err) {
65 return err
66 }
67
Damien Neil4686e232019-04-05 13:31:40 -070068 if !o.AllowPartial {
69 nerr.Merge(proto.IsInitialized(m))
70 }
71
Herbie Ong800c9902018-12-06 15:28:53 -080072 return nerr.E
73}
74
75// resetMessage clears all fields of given protoreflect.Message.
76// TODO: This should go into the proto package.
77func resetMessage(m pref.Message) {
78 knownFields := m.KnownFields()
79 knownFields.Range(func(num pref.FieldNumber, _ pref.Value) bool {
80 knownFields.Clear(num)
81 return true
82 })
83 unknownFields := m.UnknownFields()
84 unknownFields.Range(func(num pref.FieldNumber, _ pref.RawFields) bool {
85 unknownFields.Set(num, nil)
86 return true
87 })
Herbie Ong800c9902018-12-06 15:28:53 -080088 extTypes := knownFields.ExtensionTypes()
89 extTypes.Range(func(xt pref.ExtensionType) bool {
90 extTypes.Remove(xt)
91 return true
92 })
93}
94
95// unmarshalMessage unmarshals a [][2]text.Value message into the given protoreflect.Message.
96func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) error {
97 var nerr errors.NonFatal
98
99 msgType := m.Type()
Herbie Ong66c365c2019-01-04 14:08:41 -0800100 knownFields := m.KnownFields()
101
102 // Handle expanded Any message.
103 if msgType.FullName() == "google.protobuf.Any" && isExpandedAny(tmsg) {
104 return o.unmarshalAny(tmsg[0], knownFields)
105 }
106
Herbie Ong800c9902018-12-06 15:28:53 -0800107 fieldDescs := msgType.Fields()
Herbie Ong7c624e22018-12-13 14:41:22 -0800108 reservedNames := msgType.ReservedNames()
Herbie Ongc525c972018-12-18 18:04:31 -0800109 xtTypes := knownFields.ExtensionTypes()
Herbie Ong800c9902018-12-06 15:28:53 -0800110 var seenNums set.Ints
Herbie Ong8a1d4602019-04-02 20:19:36 -0700111 var seenOneofs set.Ints
Herbie Ong800c9902018-12-06 15:28:53 -0800112
113 for _, tfield := range tmsg {
114 tkey := tfield[0]
115 tval := tfield[1]
116
117 var fd pref.FieldDescriptor
Herbie Ongc525c972018-12-18 18:04:31 -0800118 var name pref.Name
119 switch tkey.Type() {
120 case text.Name:
121 name, _ = tkey.Name()
Herbie Ong800c9902018-12-06 15:28:53 -0800122 fd = fieldDescs.ByName(name)
Herbie Ong0dcfb9a2019-01-14 15:32:26 -0800123 if fd == nil {
124 // Check if this is a group field.
125 fd = fieldDescs.ByName(pref.Name(strings.ToLower(string(name))))
126 }
Herbie Ongc525c972018-12-18 18:04:31 -0800127 case text.String:
Herbie Ong66c365c2019-01-04 14:08:41 -0800128 // Handle extensions only. This code path is not for Any.
129 if msgType.FullName() == "google.protobuf.Any" {
130 break
131 }
132 // Extensions have to be registered first in the message's
Herbie Ongc525c972018-12-18 18:04:31 -0800133 // ExtensionTypes before setting a value to it.
134 xtName := pref.FullName(tkey.String())
Herbie Ong66c365c2019-01-04 14:08:41 -0800135 // Check first if it is already registered. This is the case for
136 // repeated fields.
Herbie Ongc525c972018-12-18 18:04:31 -0800137 xt := xtTypes.ByName(xtName)
138 if xt == nil {
139 var err error
Herbie Ong6470ea62019-01-07 18:56:57 -0800140 xt, err = o.findExtension(xtName)
Herbie Ongc525c972018-12-18 18:04:31 -0800141 if err != nil && err != protoregistry.NotFound {
Herbie Ong66c365c2019-01-04 14:08:41 -0800142 return errors.New("unable to resolve [%v]: %v", xtName, err)
Herbie Ongc525c972018-12-18 18:04:31 -0800143 }
144 if xt != nil {
145 xtTypes.Register(xt)
146 }
147 }
148 fd = xt
Herbie Ong800c9902018-12-06 15:28:53 -0800149 }
Herbie Ongc525c972018-12-18 18:04:31 -0800150
Herbie Ong800c9902018-12-06 15:28:53 -0800151 if fd == nil {
Herbie Ong7c624e22018-12-13 14:41:22 -0800152 // Ignore reserved names.
153 if reservedNames.Has(name) {
154 continue
155 }
Herbie Ong800c9902018-12-06 15:28:53 -0800156 // TODO: Can provide option to ignore unknown message fields.
Herbie Ong800c9902018-12-06 15:28:53 -0800157 return errors.New("%v contains unknown field: %v", msgType.FullName(), tkey)
158 }
159
160 if cardinality := fd.Cardinality(); cardinality == pref.Repeated {
161 // Map or list fields have cardinality of repeated.
162 if err := o.unmarshalRepeated(tval, fd, knownFields); !nerr.Merge(err) {
163 return err
164 }
165 } else {
Herbie Ong8a1d4602019-04-02 20:19:36 -0700166 // If field is a oneof, check if it has already been set.
167 if od := fd.OneofType(); od != nil {
168 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 }
180 if err := o.unmarshalSingular(tval, fd, knownFields); !nerr.Merge(err) {
181 return err
182 }
Herbie Ong800c9902018-12-06 15:28:53 -0800183 seenNums.Set(num)
184 }
185 }
186
Herbie Ong800c9902018-12-06 15:28:53 -0800187 return nerr.E
188}
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.
206func (o UnmarshalOptions) unmarshalSingular(input text.Value, fd pref.FieldDescriptor, knownFields pref.KnownFields) error {
207 num := fd.Number()
208
209 var nerr errors.NonFatal
210 var val pref.Value
211 switch fd.Kind() {
212 case pref.MessageKind, pref.GroupKind:
213 if input.Type() != text.Message {
214 return errors.New("%v contains invalid message/group value: %v", fd.FullName(), input)
215 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800216 m := knownFields.NewMessage(num)
Herbie Ong800c9902018-12-06 15:28:53 -0800217 if err := o.unmarshalMessage(input.Message(), m); !nerr.Merge(err) {
218 return err
219 }
220 val = pref.ValueOf(m)
221 default:
222 var err error
223 val, err = unmarshalScalar(input, fd)
224 if !nerr.Merge(err) {
225 return err
226 }
227 }
228 knownFields.Set(num, val)
229
230 return nerr.E
231}
232
233// unmarshalRepeated unmarshals given text.Value into a repeated field. Caller should only
234// call this for cardinality=repeated.
235func (o UnmarshalOptions) unmarshalRepeated(input text.Value, fd pref.FieldDescriptor, knownFields pref.KnownFields) error {
236 var items []text.Value
237 // If input is not a list, turn it into a list.
238 if input.Type() != text.List {
239 items = []text.Value{input}
240 } else {
241 items = input.List()
242 }
243
244 var nerr errors.NonFatal
245 num := fd.Number()
246 val := knownFields.Get(num)
247 if !fd.IsMap() {
248 if err := o.unmarshalList(items, fd, val.List()); !nerr.Merge(err) {
249 return err
250 }
251 } else {
252 if err := o.unmarshalMap(items, fd, val.Map()); !nerr.Merge(err) {
253 return err
254 }
255 }
256
257 return nerr.E
258}
259
260// 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.
316 if enumVal := fd.EnumType().Values().ByName(name); enumVal != nil {
317 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
359 fields := fd.MessageType().Fields()
360 keyDesc := fields.ByNumber(1)
361 valDesc := fields.ByNumber(2)
362
363 // Determine ahead whether map entry is a scalar type or a message type in order to call the
364 // appropriate unmarshalMapValue func inside the for loop below.
Herbie Ong66c365c2019-01-04 14:08:41 -0800365 unmarshalMapValue := unmarshalMapScalarValue
Herbie Ong800c9902018-12-06 15:28:53 -0800366 switch valDesc.Kind() {
367 case pref.MessageKind, pref.GroupKind:
368 unmarshalMapValue = o.unmarshalMapMessageValue
369 }
370
371 for _, entry := range input {
372 if entry.Type() != text.Message {
373 return errors.New("%v contains invalid map entry: %v", fd.FullName(), entry)
374 }
375 tkey, tval, err := parseMapEntry(entry.Message(), fd.FullName())
376 if !nerr.Merge(err) {
377 return err
378 }
379 pkey, err := unmarshalMapKey(tkey, keyDesc)
380 if !nerr.Merge(err) {
381 return err
382 }
383 err = unmarshalMapValue(tval, pkey, valDesc, mmap)
384 if !nerr.Merge(err) {
385 return err
386 }
387 }
388
389 return nerr.E
390}
391
392// parseMapEntry parses [][2]text.Value for field names key and value, and return corresponding
393// field values. If there are duplicate field names, the value for the last field is returned. If
394// the field name does not exist, it will return the zero value of text.Value. It will return an
395// error if there are unknown field names.
396func parseMapEntry(mapEntry [][2]text.Value, name pref.FullName) (key text.Value, value text.Value, err error) {
397 for _, field := range mapEntry {
398 keyStr, ok := field[0].Name()
399 if ok {
400 switch keyStr {
401 case "key":
402 if key.Type() != 0 {
403 return key, value, errors.New("%v contains duplicate key field", name)
404 }
405 key = field[1]
406 case "value":
407 if value.Type() != 0 {
408 return key, value, errors.New("%v contains duplicate value field", name)
409 }
410 value = field[1]
411 default:
412 ok = false
413 }
414 }
415 if !ok {
416 // TODO: Do not return error if ignore unknown option is added and enabled.
417 return key, value, errors.New("%v contains unknown map entry name: %v", name, field[0])
418 }
419 }
420 return key, value, nil
421}
422
423// unmarshalMapKey converts given text.Value into a protoreflect.MapKey. A map key type is any
424// integral or string type.
425func unmarshalMapKey(input text.Value, fd pref.FieldDescriptor) (pref.MapKey, error) {
426 // If input is not set, use the zero value.
427 if input.Type() == 0 {
428 return fd.Default().MapKey(), nil
429 }
430
Herbie Ong21a39742019-04-08 17:32:44 -0700431 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800432 val, err := unmarshalScalar(input, fd)
Herbie Ong21a39742019-04-08 17:32:44 -0700433 if !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800434 return pref.MapKey{}, errors.New("%v contains invalid key: %v", fd.FullName(), input)
435 }
Herbie Ong21a39742019-04-08 17:32:44 -0700436 return val.MapKey(), nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800437}
438
439// unmarshalMapMessageValue unmarshals given message-type text.Value into a protoreflect.Map for
440// the given MapKey.
441func (o UnmarshalOptions) unmarshalMapMessageValue(input text.Value, pkey pref.MapKey, _ pref.FieldDescriptor, mmap pref.Map) error {
442 var nerr errors.NonFatal
443 var value [][2]text.Value
444 if input.Type() != 0 {
445 value = input.Message()
446 }
Joe Tsai3bc7d6f2019-01-09 02:57:13 -0800447 m := mmap.NewMessage()
Herbie Ong800c9902018-12-06 15:28:53 -0800448 if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
449 return err
450 }
451 mmap.Set(pkey, pref.ValueOf(m))
452 return nerr.E
453}
454
455// unmarshalMapScalarValue unmarshals given scalar-type text.Value into a protoreflect.Map
456// for the given MapKey.
Herbie Ong66c365c2019-01-04 14:08:41 -0800457func unmarshalMapScalarValue(input text.Value, pkey pref.MapKey, fd pref.FieldDescriptor, mmap pref.Map) error {
Herbie Ong21a39742019-04-08 17:32:44 -0700458 var nerr errors.NonFatal
Herbie Ong800c9902018-12-06 15:28:53 -0800459 var val pref.Value
460 if input.Type() == 0 {
461 val = fd.Default()
462 } else {
463 var err error
464 val, err = unmarshalScalar(input, fd)
Herbie Ong21a39742019-04-08 17:32:44 -0700465 if !nerr.Merge(err) {
Herbie Ong800c9902018-12-06 15:28:53 -0800466 return err
467 }
468 }
469 mmap.Set(pkey, val)
Herbie Ong21a39742019-04-08 17:32:44 -0700470 return nerr.E
Herbie Ong800c9902018-12-06 15:28:53 -0800471}
Herbie Ong66c365c2019-01-04 14:08:41 -0800472
473// isExpandedAny returns true if given [][2]text.Value may be an expanded Any that contains only one
474// field with key type of text.String type and value type of text.Message.
475func isExpandedAny(tmsg [][2]text.Value) bool {
476 if len(tmsg) != 1 {
477 return false
478 }
479
480 field := tmsg[0]
481 return field[0].Type() == text.String && field[1].Type() == text.Message
482}
483
484// unmarshalAny unmarshals an expanded Any textproto. This method assumes that the given
485// tfield has key type of text.String and value type of text.Message.
486func (o UnmarshalOptions) unmarshalAny(tfield [2]text.Value, knownFields pref.KnownFields) error {
487 var nerr errors.NonFatal
488
489 typeURL := tfield[0].String()
490 value := tfield[1].Message()
491
492 mt, err := o.Resolver.FindMessageByURL(typeURL)
493 if !nerr.Merge(err) {
494 return errors.New("unable to resolve message [%v]: %v", typeURL, err)
495 }
496 // Create new message for the embedded message type and unmarshal the
497 // value into it.
498 m := mt.New()
499 if err := o.unmarshalMessage(value, m); !nerr.Merge(err) {
500 return err
501 }
502 // Serialize the embedded message and assign the resulting bytes to the value field.
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}