blob: 7dfae091a6396daf7fdec53a3a4531f0ed04829a [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001package dynamic
2
3// JSON marshalling and unmarshalling for dynamic messages
4
5import (
6 "bytes"
7 "encoding/base64"
8 "encoding/json"
9 "fmt"
10 "io"
11 "io/ioutil"
12 "math"
13 "reflect"
14 "sort"
15 "strconv"
16 "strings"
17
18 "github.com/golang/protobuf/jsonpb"
19 "github.com/golang/protobuf/proto"
20 "github.com/golang/protobuf/protoc-gen-go/descriptor"
21 // link in the well-known-types that have a special JSON format
22 _ "github.com/golang/protobuf/ptypes/any"
23 _ "github.com/golang/protobuf/ptypes/duration"
24 _ "github.com/golang/protobuf/ptypes/empty"
25 _ "github.com/golang/protobuf/ptypes/struct"
26 _ "github.com/golang/protobuf/ptypes/timestamp"
27 _ "github.com/golang/protobuf/ptypes/wrappers"
28
29 "github.com/jhump/protoreflect/desc"
30)
31
32var wellKnownTypeNames = map[string]struct{}{
33 "google.protobuf.Any": {},
34 "google.protobuf.Empty": {},
35 "google.protobuf.Duration": {},
36 "google.protobuf.Timestamp": {},
37 // struct.proto
38 "google.protobuf.Struct": {},
39 "google.protobuf.Value": {},
40 "google.protobuf.ListValue": {},
41 // wrappers.proto
42 "google.protobuf.DoubleValue": {},
43 "google.protobuf.FloatValue": {},
44 "google.protobuf.Int64Value": {},
45 "google.protobuf.UInt64Value": {},
46 "google.protobuf.Int32Value": {},
47 "google.protobuf.UInt32Value": {},
48 "google.protobuf.BoolValue": {},
49 "google.protobuf.StringValue": {},
50 "google.protobuf.BytesValue": {},
51}
52
53// MarshalJSON serializes this message to bytes in JSON format, returning an
54// error if the operation fails. The resulting bytes will be a valid UTF8
55// string.
56//
57// This method uses a compact form: no newlines, and spaces between fields and
58// between field identifiers and values are elided.
59//
60// This method is convenient shorthand for invoking MarshalJSONPB with a default
61// (zero value) marshaler:
62//
63// m.MarshalJSONPB(&jsonpb.Marshaler{})
64//
65// So enums are serialized using enum value name strings, and values that are
66// not present (including those with default/zero value for messages defined in
67// "proto3" syntax) are omitted.
68func (m *Message) MarshalJSON() ([]byte, error) {
69 return m.MarshalJSONPB(&jsonpb.Marshaler{})
70}
71
72// MarshalJSONIndent serializes this message to bytes in JSON format, returning
73// an error if the operation fails. The resulting bytes will be a valid UTF8
74// string.
75//
76// This method uses a "pretty-printed" form, with each field on its own line and
77// spaces between field identifiers and values. Indentation of two spaces is
78// used.
79//
80// This method is convenient shorthand for invoking MarshalJSONPB with a default
81// (zero value) marshaler:
82//
83// m.MarshalJSONPB(&jsonpb.Marshaler{Indent: " "})
84//
85// So enums are serialized using enum value name strings, and values that are
86// not present (including those with default/zero value for messages defined in
87// "proto3" syntax) are omitted.
88func (m *Message) MarshalJSONIndent() ([]byte, error) {
89 return m.MarshalJSONPB(&jsonpb.Marshaler{Indent: " "})
90}
91
92// MarshalJSONPB serializes this message to bytes in JSON format, returning an
93// error if the operation fails. The resulting bytes will be a valid UTF8
94// string. The given marshaler is used to convey options used during marshaling.
95//
96// If this message contains nested messages that are generated message types (as
97// opposed to dynamic messages), the given marshaler is used to marshal it.
98//
99// When marshaling any nested messages, any jsonpb.AnyResolver configured in the
100// given marshaler is augmented with knowledge of message types known to this
101// message's descriptor (and its enclosing file and set of transitive
102// dependencies).
103func (m *Message) MarshalJSONPB(opts *jsonpb.Marshaler) ([]byte, error) {
104 var b indentBuffer
105 b.indent = opts.Indent
106 if len(opts.Indent) == 0 {
107 b.indentCount = -1
108 }
109 b.comma = true
110 if err := m.marshalJSON(&b, opts); err != nil {
111 return nil, err
112 }
113 return b.Bytes(), nil
114}
115
116func (m *Message) marshalJSON(b *indentBuffer, opts *jsonpb.Marshaler) error {
117 if r, changed := wrapResolver(opts.AnyResolver, m.mf, m.md.GetFile()); changed {
118 newOpts := *opts
119 newOpts.AnyResolver = r
120 opts = &newOpts
121 }
122
123 if ok, err := marshalWellKnownType(m, b, opts); ok {
124 return err
125 }
126
127 err := b.WriteByte('{')
128 if err != nil {
129 return err
130 }
131 err = b.start()
132 if err != nil {
133 return err
134 }
135
136 var tags []int
137 if opts.EmitDefaults {
138 tags = m.allKnownFieldTags()
139 } else {
140 tags = m.knownFieldTags()
141 }
142
143 first := true
144
145 for _, tag := range tags {
146 itag := int32(tag)
147 fd := m.FindFieldDescriptor(itag)
148
149 v, ok := m.values[itag]
150 if !ok {
151 if fd.GetOneOf() != nil {
152 // don't print defaults for fields in a oneof
153 continue
154 }
155 v = fd.GetDefaultValue()
156 }
157
158 err := b.maybeNext(&first)
159 if err != nil {
160 return err
161 }
162 err = marshalKnownFieldJSON(b, fd, v, opts)
163 if err != nil {
164 return err
165 }
166 }
167
168 err = b.end()
169 if err != nil {
170 return err
171 }
172 err = b.WriteByte('}')
173 if err != nil {
174 return err
175 }
176
177 return nil
178}
179
180func marshalWellKnownType(m *Message, b *indentBuffer, opts *jsonpb.Marshaler) (bool, error) {
181 fqn := m.md.GetFullyQualifiedName()
182 if _, ok := wellKnownTypeNames[fqn]; !ok {
183 return false, nil
184 }
185
186 msgType := proto.MessageType(fqn)
187 if msgType == nil {
188 // wtf?
189 panic(fmt.Sprintf("could not find registered message type for %q", fqn))
190 }
191
192 // convert dynamic message to well-known type and let jsonpb marshal it
193 msg := reflect.New(msgType.Elem()).Interface().(proto.Message)
194 if err := m.MergeInto(msg); err != nil {
195 return true, err
196 }
197 return true, opts.Marshal(b, msg)
198}
199
200func marshalKnownFieldJSON(b *indentBuffer, fd *desc.FieldDescriptor, v interface{}, opts *jsonpb.Marshaler) error {
201 var jsonName string
202 if opts.OrigName {
203 jsonName = fd.GetName()
204 } else {
205 jsonName = fd.AsFieldDescriptorProto().GetJsonName()
206 if jsonName == "" {
207 jsonName = fd.GetName()
208 }
209 }
210 if fd.IsExtension() {
211 var scope string
212 switch parent := fd.GetParent().(type) {
213 case *desc.FileDescriptor:
214 scope = parent.GetPackage()
215 default:
216 scope = parent.GetFullyQualifiedName()
217 }
218 if scope == "" {
219 jsonName = fmt.Sprintf("[%s]", jsonName)
220 } else {
221 jsonName = fmt.Sprintf("[%s.%s]", scope, jsonName)
222 }
223 }
224 err := writeJsonString(b, jsonName)
225 if err != nil {
226 return err
227 }
228 err = b.sep()
229 if err != nil {
230 return err
231 }
232
233 if isNil(v) {
234 _, err := b.WriteString("null")
235 return err
236 }
237
238 if fd.IsMap() {
239 err = b.WriteByte('{')
240 if err != nil {
241 return err
242 }
243 err = b.start()
244 if err != nil {
245 return err
246 }
247
248 md := fd.GetMessageType()
249 vfd := md.FindFieldByNumber(2)
250
251 mp := v.(map[interface{}]interface{})
252 keys := make([]interface{}, 0, len(mp))
253 for k := range mp {
254 keys = append(keys, k)
255 }
256 sort.Sort(sortable(keys))
257 first := true
258 for _, mk := range keys {
259 mv := mp[mk]
260 err := b.maybeNext(&first)
261 if err != nil {
262 return err
263 }
264
265 err = marshalKnownFieldMapEntryJSON(b, mk, vfd, mv, opts)
266 if err != nil {
267 return err
268 }
269 }
270
271 err = b.end()
272 if err != nil {
273 return err
274 }
275 return b.WriteByte('}')
276
277 } else if fd.IsRepeated() {
278 err = b.WriteByte('[')
279 if err != nil {
280 return err
281 }
282 err = b.start()
283 if err != nil {
284 return err
285 }
286
287 sl := v.([]interface{})
288 first := true
289 for _, slv := range sl {
290 err := b.maybeNext(&first)
291 if err != nil {
292 return err
293 }
294 err = marshalKnownFieldValueJSON(b, fd, slv, opts)
295 if err != nil {
296 return err
297 }
298 }
299
300 err = b.end()
301 if err != nil {
302 return err
303 }
304 return b.WriteByte(']')
305
306 } else {
307 return marshalKnownFieldValueJSON(b, fd, v, opts)
308 }
309}
310
Scott Baker4a35a702019-11-26 08:17:33 -0800311// sortable is used to sort map keys. Values will be integers (int32, int64, uint32, and uint64),
312// bools, or strings.
313type sortable []interface{}
314
315func (s sortable) Len() int {
316 return len(s)
317}
318
319func (s sortable) Less(i, j int) bool {
320 vi := s[i]
321 vj := s[j]
322 switch reflect.TypeOf(vi).Kind() {
323 case reflect.Int32:
324 return vi.(int32) < vj.(int32)
325 case reflect.Int64:
326 return vi.(int64) < vj.(int64)
327 case reflect.Uint32:
328 return vi.(uint32) < vj.(uint32)
329 case reflect.Uint64:
330 return vi.(uint64) < vj.(uint64)
331 case reflect.String:
332 return vi.(string) < vj.(string)
333 case reflect.Bool:
334 return !vi.(bool) && vj.(bool)
335 default:
336 panic(fmt.Sprintf("cannot compare keys of type %v", reflect.TypeOf(vi)))
337 }
338}
339
340func (s sortable) Swap(i, j int) {
341 s[i], s[j] = s[j], s[i]
342}
343
Zack Williamse940c7a2019-08-21 14:25:39 -0700344func isNil(v interface{}) bool {
345 if v == nil {
346 return true
347 }
348 rv := reflect.ValueOf(v)
349 return rv.Kind() == reflect.Ptr && rv.IsNil()
350}
351
352func marshalKnownFieldMapEntryJSON(b *indentBuffer, mk interface{}, vfd *desc.FieldDescriptor, mv interface{}, opts *jsonpb.Marshaler) error {
353 rk := reflect.ValueOf(mk)
354 var strkey string
355 switch rk.Kind() {
356 case reflect.Bool:
357 strkey = strconv.FormatBool(rk.Bool())
358 case reflect.Int32, reflect.Int64:
359 strkey = strconv.FormatInt(rk.Int(), 10)
360 case reflect.Uint32, reflect.Uint64:
361 strkey = strconv.FormatUint(rk.Uint(), 10)
362 case reflect.String:
363 strkey = rk.String()
364 default:
365 return fmt.Errorf("invalid map key value: %v (%v)", mk, rk.Type())
366 }
367 err := writeString(b, strkey)
368 if err != nil {
369 return err
370 }
371 err = b.sep()
372 if err != nil {
373 return err
374 }
375 return marshalKnownFieldValueJSON(b, vfd, mv, opts)
376}
377
378func marshalKnownFieldValueJSON(b *indentBuffer, fd *desc.FieldDescriptor, v interface{}, opts *jsonpb.Marshaler) error {
379 rv := reflect.ValueOf(v)
380 switch rv.Kind() {
Scott Baker4a35a702019-11-26 08:17:33 -0800381 case reflect.Int64:
382 return writeJsonString(b, strconv.FormatInt(rv.Int(), 10))
383 case reflect.Int32:
Zack Williamse940c7a2019-08-21 14:25:39 -0700384 ed := fd.GetEnumType()
385 if !opts.EnumsAsInts && ed != nil {
386 n := int32(rv.Int())
387 vd := ed.FindValueByNumber(n)
388 if vd == nil {
389 _, err := b.WriteString(strconv.FormatInt(rv.Int(), 10))
390 return err
391 } else {
392 return writeJsonString(b, vd.GetName())
393 }
394 } else {
395 _, err := b.WriteString(strconv.FormatInt(rv.Int(), 10))
396 return err
397 }
Scott Baker4a35a702019-11-26 08:17:33 -0800398 case reflect.Uint64:
399 return writeJsonString(b, strconv.FormatUint(rv.Uint(), 10))
400 case reflect.Uint32:
Zack Williamse940c7a2019-08-21 14:25:39 -0700401 _, err := b.WriteString(strconv.FormatUint(rv.Uint(), 10))
402 return err
403 case reflect.Float32, reflect.Float64:
404 f := rv.Float()
405 var str string
406 if math.IsNaN(f) {
407 str = `"NaN"`
408 } else if math.IsInf(f, 1) {
409 str = `"Infinity"`
410 } else if math.IsInf(f, -1) {
411 str = `"-Infinity"`
412 } else {
413 var bits int
414 if rv.Kind() == reflect.Float32 {
415 bits = 32
416 } else {
417 bits = 64
418 }
419 str = strconv.FormatFloat(rv.Float(), 'g', -1, bits)
420 }
421 _, err := b.WriteString(str)
422 return err
423 case reflect.Bool:
424 _, err := b.WriteString(strconv.FormatBool(rv.Bool()))
425 return err
426 case reflect.Slice:
427 bstr := base64.StdEncoding.EncodeToString(rv.Bytes())
428 return writeJsonString(b, bstr)
429 case reflect.String:
430 return writeJsonString(b, rv.String())
431 default:
432 // must be a message
433 if dm, ok := v.(*Message); ok {
434 return dm.marshalJSON(b, opts)
435 } else {
436 var err error
437 if b.indentCount <= 0 || len(b.indent) == 0 {
438 err = opts.Marshal(b, v.(proto.Message))
439 } else {
440 str, err := opts.MarshalToString(v.(proto.Message))
441 if err != nil {
442 return err
443 }
444 indent := strings.Repeat(b.indent, b.indentCount)
445 pos := 0
446 // add indention prefix to each line
447 for pos < len(str) {
448 start := pos
449 nextPos := strings.Index(str[pos:], "\n")
450 if nextPos == -1 {
451 nextPos = len(str)
452 } else {
453 nextPos = pos + nextPos + 1 // include newline
454 }
455 line := str[start:nextPos]
456 if pos > 0 {
457 _, err = b.WriteString(indent)
458 if err != nil {
459 return err
460 }
461 }
462 _, err = b.WriteString(line)
463 if err != nil {
464 return err
465 }
466 pos = nextPos
467 }
468 }
469 return err
470 }
471 }
472}
473
474func writeJsonString(b *indentBuffer, s string) error {
475 if sbytes, err := json.Marshal(s); err != nil {
476 return err
477 } else {
478 _, err := b.Write(sbytes)
479 return err
480 }
481}
482
483// UnmarshalJSON de-serializes the message that is present, in JSON format, in
484// the given bytes into this message. It first resets the current message. It
485// returns an error if the given bytes do not contain a valid encoding of this
486// message type in JSON format.
487//
488// This method is shorthand for invoking UnmarshalJSONPB with a default (zero
489// value) unmarshaler:
490//
491// m.UnmarshalMergeJSONPB(&jsonpb.Unmarshaler{}, js)
492//
493// So unknown fields will result in an error, and no provided jsonpb.AnyResolver
494// will be used when parsing google.protobuf.Any messages.
495func (m *Message) UnmarshalJSON(js []byte) error {
496 return m.UnmarshalJSONPB(&jsonpb.Unmarshaler{}, js)
497}
498
499// UnmarshalMergeJSON de-serializes the message that is present, in JSON format,
500// in the given bytes into this message. Unlike UnmarshalJSON, it does not first
501// reset the message, instead merging the data in the given bytes into the
502// existing data in this message.
503func (m *Message) UnmarshalMergeJSON(js []byte) error {
504 return m.UnmarshalMergeJSONPB(&jsonpb.Unmarshaler{}, js)
505}
506
507// UnmarshalJSONPB de-serializes the message that is present, in JSON format, in
508// the given bytes into this message. The given unmarshaler conveys options used
509// when parsing the JSON. This function first resets the current message. It
510// returns an error if the given bytes do not contain a valid encoding of this
511// message type in JSON format.
512//
513// The decoding is lenient:
514// 1. The JSON can refer to fields either by their JSON name or by their
515// declared name.
516// 2. The JSON can use either numeric values or string names for enum values.
517//
518// When instantiating nested messages, if this message's associated factory
519// returns a generated message type (as opposed to a dynamic message), the given
520// unmarshaler is used to unmarshal it.
521//
522// When unmarshaling any nested messages, any jsonpb.AnyResolver configured in
523// the given unmarshaler is augmented with knowledge of message types known to
524// this message's descriptor (and its enclosing file and set of transitive
525// dependencies).
526func (m *Message) UnmarshalJSONPB(opts *jsonpb.Unmarshaler, js []byte) error {
527 m.Reset()
528 if err := m.UnmarshalMergeJSONPB(opts, js); err != nil {
529 return err
530 }
531 return m.Validate()
532}
533
534// UnmarshalMergeJSONPB de-serializes the message that is present, in JSON
535// format, in the given bytes into this message. The given unmarshaler conveys
536// options used when parsing the JSON. Unlike UnmarshalJSONPB, it does not first
537// reset the message, instead merging the data in the given bytes into the
538// existing data in this message.
539func (m *Message) UnmarshalMergeJSONPB(opts *jsonpb.Unmarshaler, js []byte) error {
540 r := newJsReader(js)
541 err := m.unmarshalJson(r, opts)
542 if err != nil {
543 return err
544 }
545 if t, err := r.poll(); err != io.EOF {
546 b, _ := ioutil.ReadAll(r.unread())
547 s := fmt.Sprintf("%v%s", t, string(b))
548 return fmt.Errorf("superfluous data found after JSON object: %q", s)
549 }
550 return nil
551}
552
553func unmarshalWellKnownType(m *Message, r *jsReader, opts *jsonpb.Unmarshaler) (bool, error) {
554 fqn := m.md.GetFullyQualifiedName()
555 if _, ok := wellKnownTypeNames[fqn]; !ok {
556 return false, nil
557 }
558
559 msgType := proto.MessageType(fqn)
560 if msgType == nil {
561 // wtf?
562 panic(fmt.Sprintf("could not find registered message type for %q", fqn))
563 }
564
565 // extract json value from r
566 var js json.RawMessage
567 if err := json.NewDecoder(r.unread()).Decode(&js); err != nil {
568 return true, err
569 }
570 if err := r.skip(); err != nil {
571 return true, err
572 }
573
574 // unmarshal into well-known type and then convert to dynamic message
575 msg := reflect.New(msgType.Elem()).Interface().(proto.Message)
576 if err := opts.Unmarshal(bytes.NewReader(js), msg); err != nil {
577 return true, err
578 }
579 return true, m.MergeFrom(msg)
580}
581
582func (m *Message) unmarshalJson(r *jsReader, opts *jsonpb.Unmarshaler) error {
583 if r, changed := wrapResolver(opts.AnyResolver, m.mf, m.md.GetFile()); changed {
584 newOpts := *opts
585 newOpts.AnyResolver = r
586 opts = &newOpts
587 }
588
589 if ok, err := unmarshalWellKnownType(m, r, opts); ok {
590 return err
591 }
592
593 t, err := r.peek()
594 if err != nil {
595 return err
596 }
597 if t == nil {
598 // if json is simply "null" we do nothing
599 r.poll()
600 return nil
601 }
602
603 if err := r.beginObject(); err != nil {
604 return err
605 }
606
607 for r.hasNext() {
608 f, err := r.nextObjectKey()
609 if err != nil {
610 return err
611 }
612 fd := m.FindFieldDescriptorByJSONName(f)
613 if fd == nil {
614 if opts.AllowUnknownFields {
615 r.skip()
616 continue
617 }
618 return fmt.Errorf("message type %s has no known field named %s", m.md.GetFullyQualifiedName(), f)
619 }
620 v, err := unmarshalJsField(fd, r, m.mf, opts)
621 if err != nil {
622 return err
623 }
624 if v != nil {
625 if err := mergeField(m, fd, v); err != nil {
626 return err
627 }
628 } else if fd.GetOneOf() != nil {
629 // preserve explicit null for oneof fields (this is a little odd but
630 // mimics the behavior of jsonpb with oneofs in generated message types)
631 if fd.GetMessageType() != nil {
632 typ := m.mf.GetKnownTypeRegistry().GetKnownType(fd.GetMessageType().GetFullyQualifiedName())
633 if typ != nil {
634 // typed nil
635 if typ.Kind() != reflect.Ptr {
636 typ = reflect.PtrTo(typ)
637 }
638 v = reflect.Zero(typ).Interface()
639 } else {
640 // can't use nil dynamic message, so we just use empty one instead
641 v = m.mf.NewDynamicMessage(fd.GetMessageType())
642 }
643 if err := m.setField(fd, v); err != nil {
644 return err
645 }
646 } else {
647 // not a message... explicit null makes no sense
648 return fmt.Errorf("message type %s cannot set field %s to null: it is not a message type", m.md.GetFullyQualifiedName(), f)
649 }
650 } else {
651 m.clearField(fd)
652 }
653 }
654
655 if err := r.endObject(); err != nil {
656 return err
657 }
658
659 return nil
660}
661
662func isWellKnownValue(fd *desc.FieldDescriptor) bool {
663 return !fd.IsRepeated() && fd.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE &&
664 fd.GetMessageType().GetFullyQualifiedName() == "google.protobuf.Value"
665}
666
667func isWellKnownListValue(fd *desc.FieldDescriptor) bool {
668 return !fd.IsRepeated() && fd.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE &&
669 fd.GetMessageType().GetFullyQualifiedName() == "google.protobuf.ListValue"
670}
671
672func unmarshalJsField(fd *desc.FieldDescriptor, r *jsReader, mf *MessageFactory, opts *jsonpb.Unmarshaler) (interface{}, error) {
673 t, err := r.peek()
674 if err != nil {
675 return nil, err
676 }
677 if t == nil && !isWellKnownValue(fd) {
678 // if value is null, just return nil
679 // (unless field is google.protobuf.Value, in which case
680 // we fall through to parse it as an instance where its
681 // underlying value is set to a NullValue)
682 r.poll()
683 return nil, nil
684 }
685
686 if t == json.Delim('{') && fd.IsMap() {
687 entryType := fd.GetMessageType()
688 keyType := entryType.FindFieldByNumber(1)
689 valueType := entryType.FindFieldByNumber(2)
690 mp := map[interface{}]interface{}{}
691
692 // TODO: if there are just two map keys "key" and "value" and they have the right type of values,
693 // treat this JSON object as a single map entry message. (In keeping with support of map fields as
694 // if they were normal repeated field of entry messages as well as supporting a transition from
695 // optional to repeated...)
696
697 if err := r.beginObject(); err != nil {
698 return nil, err
699 }
700 for r.hasNext() {
701 kk, err := unmarshalJsFieldElement(keyType, r, mf, opts)
702 if err != nil {
703 return nil, err
704 }
705 vv, err := unmarshalJsFieldElement(valueType, r, mf, opts)
706 if err != nil {
707 return nil, err
708 }
709 mp[kk] = vv
710 }
711 if err := r.endObject(); err != nil {
712 return nil, err
713 }
714
715 return mp, nil
716 } else if t == json.Delim('[') && !isWellKnownListValue(fd) {
717 // We support parsing an array, even if field is not repeated, to mimic support in proto
718 // binary wire format that supports changing an optional field to repeated and vice versa.
719 // If the field is not repeated, we only keep the last value in the array.
720
721 if err := r.beginArray(); err != nil {
722 return nil, err
723 }
724 var sl []interface{}
725 var v interface{}
726 for r.hasNext() {
727 var err error
728 v, err = unmarshalJsFieldElement(fd, r, mf, opts)
729 if err != nil {
730 return nil, err
731 }
732 if fd.IsRepeated() && v != nil {
733 sl = append(sl, v)
734 }
735 }
736 if err := r.endArray(); err != nil {
737 return nil, err
738 }
739 if fd.IsMap() {
740 mp := map[interface{}]interface{}{}
741 for _, m := range sl {
742 msg := m.(*Message)
743 kk, err := msg.TryGetFieldByNumber(1)
744 if err != nil {
745 return nil, err
746 }
747 vv, err := msg.TryGetFieldByNumber(2)
748 if err != nil {
749 return nil, err
750 }
751 mp[kk] = vv
752 }
753 return mp, nil
754 } else if fd.IsRepeated() {
755 return sl, nil
756 } else {
757 return v, nil
758 }
759 } else {
760 // We support parsing a singular value, even if field is repeated, to mimic support in proto
761 // binary wire format that supports changing an optional field to repeated and vice versa.
762 // If the field is repeated, we store value as singleton slice of that one value.
763
764 v, err := unmarshalJsFieldElement(fd, r, mf, opts)
765 if err != nil {
766 return nil, err
767 }
768 if v == nil {
769 return nil, nil
770 }
771 if fd.IsRepeated() {
772 return []interface{}{v}, nil
773 } else {
774 return v, nil
775 }
776 }
777}
778
779func unmarshalJsFieldElement(fd *desc.FieldDescriptor, r *jsReader, mf *MessageFactory, opts *jsonpb.Unmarshaler) (interface{}, error) {
780 t, err := r.peek()
781 if err != nil {
782 return nil, err
783 }
784
785 switch fd.GetType() {
786 case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
787 descriptor.FieldDescriptorProto_TYPE_GROUP:
788 m := mf.NewMessage(fd.GetMessageType())
789 if dm, ok := m.(*Message); ok {
790 if err := dm.unmarshalJson(r, opts); err != nil {
791 return nil, err
792 }
793 } else {
794 var msg json.RawMessage
795 if err := json.NewDecoder(r.unread()).Decode(&msg); err != nil {
796 return nil, err
797 }
798 if err := r.skip(); err != nil {
799 return nil, err
800 }
801 if err := opts.Unmarshal(bytes.NewReader([]byte(msg)), m); err != nil {
802 return nil, err
803 }
804 }
805 return m, nil
806
807 case descriptor.FieldDescriptorProto_TYPE_ENUM:
808 if e, err := r.nextNumber(); err != nil {
809 return nil, err
810 } else {
811 // value could be string or number
812 if i, err := e.Int64(); err != nil {
813 // number cannot be parsed, so see if it's an enum value name
814 vd := fd.GetEnumType().FindValueByName(string(e))
815 if vd != nil {
816 return vd.GetNumber(), nil
817 } else {
818 return nil, fmt.Errorf("enum %q does not have value named %q", fd.GetEnumType().GetFullyQualifiedName(), e)
819 }
820 } else if i > math.MaxInt32 || i < math.MinInt32 {
821 return nil, NumericOverflowError
822 } else {
823 return int32(i), err
824 }
825 }
826
827 case descriptor.FieldDescriptorProto_TYPE_INT32,
828 descriptor.FieldDescriptorProto_TYPE_SINT32,
829 descriptor.FieldDescriptorProto_TYPE_SFIXED32:
830 if i, err := r.nextInt(); err != nil {
831 return nil, err
832 } else if i > math.MaxInt32 || i < math.MinInt32 {
833 return nil, NumericOverflowError
834 } else {
835 return int32(i), err
836 }
837
838 case descriptor.FieldDescriptorProto_TYPE_INT64,
839 descriptor.FieldDescriptorProto_TYPE_SINT64,
840 descriptor.FieldDescriptorProto_TYPE_SFIXED64:
841 return r.nextInt()
842
843 case descriptor.FieldDescriptorProto_TYPE_UINT32,
844 descriptor.FieldDescriptorProto_TYPE_FIXED32:
845 if i, err := r.nextUint(); err != nil {
846 return nil, err
847 } else if i > math.MaxUint32 {
848 return nil, NumericOverflowError
849 } else {
850 return uint32(i), err
851 }
852
853 case descriptor.FieldDescriptorProto_TYPE_UINT64,
854 descriptor.FieldDescriptorProto_TYPE_FIXED64:
855 return r.nextUint()
856
857 case descriptor.FieldDescriptorProto_TYPE_BOOL:
858 if str, ok := t.(string); ok {
859 if str == "true" {
860 r.poll() // consume token
861 return true, err
862 } else if str == "false" {
863 r.poll() // consume token
864 return false, err
865 }
866 }
867 return r.nextBool()
868
869 case descriptor.FieldDescriptorProto_TYPE_FLOAT:
870 if f, err := r.nextFloat(); err != nil {
871 return nil, err
872 } else {
873 return float32(f), nil
874 }
875
876 case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
877 return r.nextFloat()
878
879 case descriptor.FieldDescriptorProto_TYPE_BYTES:
880 return r.nextBytes()
881
882 case descriptor.FieldDescriptorProto_TYPE_STRING:
883 return r.nextString()
884
885 default:
886 return nil, fmt.Errorf("unknown field type: %v", fd.GetType())
887 }
888}
889
890type jsReader struct {
891 reader *bytes.Reader
892 dec *json.Decoder
893 current json.Token
894 peeked bool
895}
896
897func newJsReader(b []byte) *jsReader {
898 reader := bytes.NewReader(b)
899 dec := json.NewDecoder(reader)
900 dec.UseNumber()
901 return &jsReader{reader: reader, dec: dec}
902}
903
904func (r *jsReader) unread() io.Reader {
905 bufs := make([]io.Reader, 3)
906 var peeked []byte
907 if r.peeked {
908 if _, ok := r.current.(json.Delim); ok {
909 peeked = []byte(fmt.Sprintf("%v", r.current))
910 } else {
911 peeked, _ = json.Marshal(r.current)
912 }
913 }
914 readerCopy := *r.reader
915 decCopy := *r.dec
916
917 bufs[0] = bytes.NewReader(peeked)
918 bufs[1] = decCopy.Buffered()
919 bufs[2] = &readerCopy
920 return &concatReader{bufs: bufs}
921}
922
923func (r *jsReader) hasNext() bool {
924 return r.dec.More()
925}
926
927func (r *jsReader) peek() (json.Token, error) {
928 if r.peeked {
929 return r.current, nil
930 }
931 t, err := r.dec.Token()
932 if err != nil {
933 return nil, err
934 }
935 r.peeked = true
936 r.current = t
937 return t, nil
938}
939
940func (r *jsReader) poll() (json.Token, error) {
941 if r.peeked {
942 ret := r.current
943 r.current = nil
944 r.peeked = false
945 return ret, nil
946 }
947 return r.dec.Token()
948}
949
950func (r *jsReader) beginObject() error {
951 _, err := r.expect(func(t json.Token) bool { return t == json.Delim('{') }, nil, "start of JSON object: '{'")
952 return err
953}
954
955func (r *jsReader) endObject() error {
956 _, err := r.expect(func(t json.Token) bool { return t == json.Delim('}') }, nil, "end of JSON object: '}'")
957 return err
958}
959
960func (r *jsReader) beginArray() error {
961 _, err := r.expect(func(t json.Token) bool { return t == json.Delim('[') }, nil, "start of array: '['")
962 return err
963}
964
965func (r *jsReader) endArray() error {
966 _, err := r.expect(func(t json.Token) bool { return t == json.Delim(']') }, nil, "end of array: ']'")
967 return err
968}
969
970func (r *jsReader) nextObjectKey() (string, error) {
971 return r.nextString()
972}
973
974func (r *jsReader) nextString() (string, error) {
975 t, err := r.expect(func(t json.Token) bool { _, ok := t.(string); return ok }, "", "string")
976 if err != nil {
977 return "", err
978 }
979 return t.(string), nil
980}
981
982func (r *jsReader) nextBytes() ([]byte, error) {
983 str, err := r.nextString()
984 if err != nil {
985 return nil, err
986 }
987 return base64.StdEncoding.DecodeString(str)
988}
989
990func (r *jsReader) nextBool() (bool, error) {
991 t, err := r.expect(func(t json.Token) bool { _, ok := t.(bool); return ok }, false, "boolean")
992 if err != nil {
993 return false, err
994 }
995 return t.(bool), nil
996}
997
998func (r *jsReader) nextInt() (int64, error) {
999 n, err := r.nextNumber()
1000 if err != nil {
1001 return 0, err
1002 }
1003 return n.Int64()
1004}
1005
1006func (r *jsReader) nextUint() (uint64, error) {
1007 n, err := r.nextNumber()
1008 if err != nil {
1009 return 0, err
1010 }
1011 return strconv.ParseUint(string(n), 10, 64)
1012}
1013
1014func (r *jsReader) nextFloat() (float64, error) {
1015 n, err := r.nextNumber()
1016 if err != nil {
1017 return 0, err
1018 }
1019 return n.Float64()
1020}
1021
1022func (r *jsReader) nextNumber() (json.Number, error) {
1023 t, err := r.expect(func(t json.Token) bool { return reflect.TypeOf(t).Kind() == reflect.String }, "0", "number")
1024 if err != nil {
1025 return "", err
1026 }
1027 switch t := t.(type) {
1028 case json.Number:
1029 return t, nil
1030 case string:
1031 return json.Number(t), nil
1032 }
1033 return "", fmt.Errorf("expecting a number but got %v", t)
1034}
1035
1036func (r *jsReader) skip() error {
1037 t, err := r.poll()
1038 if err != nil {
1039 return err
1040 }
1041 if t == json.Delim('[') {
1042 if err := r.skipArray(); err != nil {
1043 return err
1044 }
1045 } else if t == json.Delim('{') {
1046 if err := r.skipObject(); err != nil {
1047 return err
1048 }
1049 }
1050 return nil
1051}
1052
1053func (r *jsReader) skipArray() error {
1054 for r.hasNext() {
1055 if err := r.skip(); err != nil {
1056 return err
1057 }
1058 }
1059 if err := r.endArray(); err != nil {
1060 return err
1061 }
1062 return nil
1063}
1064
1065func (r *jsReader) skipObject() error {
1066 for r.hasNext() {
1067 // skip object key
1068 if err := r.skip(); err != nil {
1069 return err
1070 }
1071 // and value
1072 if err := r.skip(); err != nil {
1073 return err
1074 }
1075 }
1076 if err := r.endObject(); err != nil {
1077 return err
1078 }
1079 return nil
1080}
1081
1082func (r *jsReader) expect(predicate func(json.Token) bool, ifNil interface{}, expected string) (interface{}, error) {
1083 t, err := r.poll()
1084 if err != nil {
1085 return nil, err
1086 }
1087 if t == nil && ifNil != nil {
1088 return ifNil, nil
1089 }
1090 if !predicate(t) {
1091 return t, fmt.Errorf("bad input: expecting %s ; instead got %v", expected, t)
1092 }
1093 return t, nil
1094}
1095
1096type concatReader struct {
1097 bufs []io.Reader
1098 curr int
1099}
1100
1101func (r *concatReader) Read(p []byte) (n int, err error) {
1102 for {
1103 if r.curr >= len(r.bufs) {
1104 err = io.EOF
1105 return
1106 }
1107 var c int
1108 c, err = r.bufs[r.curr].Read(p)
1109 n += c
1110 if err != io.EOF {
1111 return
1112 }
1113 r.curr++
1114 p = p[c:]
1115 }
1116}
1117
1118// AnyResolver returns a jsonpb.AnyResolver that uses the given file descriptors
1119// to resolve message names. It uses the given factory, which may be nil, to
1120// instantiate messages. The messages that it returns when resolving a type name
1121// may often be dynamic messages.
1122func AnyResolver(mf *MessageFactory, files ...*desc.FileDescriptor) jsonpb.AnyResolver {
1123 return &anyResolver{mf: mf, files: files}
1124}
1125
1126type anyResolver struct {
1127 mf *MessageFactory
1128 files []*desc.FileDescriptor
1129 ignored map[*desc.FileDescriptor]struct{}
1130 other jsonpb.AnyResolver
1131}
1132
1133func wrapResolver(r jsonpb.AnyResolver, mf *MessageFactory, f *desc.FileDescriptor) (jsonpb.AnyResolver, bool) {
1134 if r, ok := r.(*anyResolver); ok {
1135 if _, ok := r.ignored[f]; ok {
1136 // if the current resolver is ignoring this file, it's because another
1137 // (upstream) resolver is already handling it, so nothing to do
1138 return r, false
1139 }
1140 for _, file := range r.files {
1141 if file == f {
1142 // no need to wrap!
1143 return r, false
1144 }
1145 }
1146 // ignore files that will be checked by the resolver we're wrapping
1147 // (we'll just delegate and let it search those files)
1148 ignored := map[*desc.FileDescriptor]struct{}{}
1149 for i := range r.ignored {
1150 ignored[i] = struct{}{}
1151 }
1152 ignore(r.files, ignored)
1153 return &anyResolver{mf: mf, files: []*desc.FileDescriptor{f}, ignored: ignored, other: r}, true
1154 }
1155 return &anyResolver{mf: mf, files: []*desc.FileDescriptor{f}, other: r}, true
1156}
1157
1158func ignore(files []*desc.FileDescriptor, ignored map[*desc.FileDescriptor]struct{}) {
1159 for _, f := range files {
1160 if _, ok := ignored[f]; ok {
1161 continue
1162 }
1163 ignored[f] = struct{}{}
1164 ignore(f.GetDependencies(), ignored)
1165 }
1166}
1167
1168func (r *anyResolver) Resolve(typeUrl string) (proto.Message, error) {
1169 mname := typeUrl
1170 if slash := strings.LastIndex(mname, "/"); slash >= 0 {
1171 mname = mname[slash+1:]
1172 }
1173
1174 // see if the user-specified resolver is able to do the job
1175 if r.other != nil {
1176 msg, err := r.other.Resolve(typeUrl)
1177 if err == nil {
1178 return msg, nil
1179 }
1180 }
1181
1182 // try to find the message in our known set of files
1183 checked := map[*desc.FileDescriptor]struct{}{}
1184 for _, f := range r.files {
1185 md := r.findMessage(f, mname, checked)
1186 if md != nil {
1187 return r.mf.NewMessage(md), nil
1188 }
1189 }
1190 // failing that, see if the message factory knows about this type
1191 var ktr *KnownTypeRegistry
1192 if r.mf != nil {
1193 ktr = r.mf.ktr
1194 } else {
1195 ktr = (*KnownTypeRegistry)(nil)
1196 }
1197 m := ktr.CreateIfKnown(mname)
1198 if m != nil {
1199 return m, nil
1200 }
1201
1202 // no other resolver to fallback to? mimic default behavior
1203 mt := proto.MessageType(mname)
1204 if mt == nil {
1205 return nil, fmt.Errorf("unknown message type %q", mname)
1206 }
1207 return reflect.New(mt.Elem()).Interface().(proto.Message), nil
1208}
1209
1210func (r *anyResolver) findMessage(fd *desc.FileDescriptor, msgName string, checked map[*desc.FileDescriptor]struct{}) *desc.MessageDescriptor {
1211 // if this is an ignored descriptor, skip
1212 if _, ok := r.ignored[fd]; ok {
1213 return nil
1214 }
1215
1216 // bail if we've already checked this file
1217 if _, ok := checked[fd]; ok {
1218 return nil
1219 }
1220 checked[fd] = struct{}{}
1221
1222 // see if this file has the message
1223 md := fd.FindMessage(msgName)
1224 if md != nil {
1225 return md
1226 }
1227
1228 // if not, recursively search the file's imports
1229 for _, dep := range fd.GetDependencies() {
1230 md = r.findMessage(dep, msgName, checked)
1231 if md != nil {
1232 return md
1233 }
1234 }
1235 return nil
1236}
1237
1238var _ jsonpb.AnyResolver = (*anyResolver)(nil)