blob: f79b4ac8edb2a69cb5f467a5a9714dd3fc070216 [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
311func isNil(v interface{}) bool {
312 if v == nil {
313 return true
314 }
315 rv := reflect.ValueOf(v)
316 return rv.Kind() == reflect.Ptr && rv.IsNil()
317}
318
319func marshalKnownFieldMapEntryJSON(b *indentBuffer, mk interface{}, vfd *desc.FieldDescriptor, mv interface{}, opts *jsonpb.Marshaler) error {
320 rk := reflect.ValueOf(mk)
321 var strkey string
322 switch rk.Kind() {
323 case reflect.Bool:
324 strkey = strconv.FormatBool(rk.Bool())
325 case reflect.Int32, reflect.Int64:
326 strkey = strconv.FormatInt(rk.Int(), 10)
327 case reflect.Uint32, reflect.Uint64:
328 strkey = strconv.FormatUint(rk.Uint(), 10)
329 case reflect.String:
330 strkey = rk.String()
331 default:
332 return fmt.Errorf("invalid map key value: %v (%v)", mk, rk.Type())
333 }
334 err := writeString(b, strkey)
335 if err != nil {
336 return err
337 }
338 err = b.sep()
339 if err != nil {
340 return err
341 }
342 return marshalKnownFieldValueJSON(b, vfd, mv, opts)
343}
344
345func marshalKnownFieldValueJSON(b *indentBuffer, fd *desc.FieldDescriptor, v interface{}, opts *jsonpb.Marshaler) error {
346 rv := reflect.ValueOf(v)
347 switch rv.Kind() {
348 case reflect.Int32, reflect.Int64:
349 ed := fd.GetEnumType()
350 if !opts.EnumsAsInts && ed != nil {
351 n := int32(rv.Int())
352 vd := ed.FindValueByNumber(n)
353 if vd == nil {
354 _, err := b.WriteString(strconv.FormatInt(rv.Int(), 10))
355 return err
356 } else {
357 return writeJsonString(b, vd.GetName())
358 }
359 } else {
360 _, err := b.WriteString(strconv.FormatInt(rv.Int(), 10))
361 return err
362 }
363 case reflect.Uint32, reflect.Uint64:
364 _, err := b.WriteString(strconv.FormatUint(rv.Uint(), 10))
365 return err
366 case reflect.Float32, reflect.Float64:
367 f := rv.Float()
368 var str string
369 if math.IsNaN(f) {
370 str = `"NaN"`
371 } else if math.IsInf(f, 1) {
372 str = `"Infinity"`
373 } else if math.IsInf(f, -1) {
374 str = `"-Infinity"`
375 } else {
376 var bits int
377 if rv.Kind() == reflect.Float32 {
378 bits = 32
379 } else {
380 bits = 64
381 }
382 str = strconv.FormatFloat(rv.Float(), 'g', -1, bits)
383 }
384 _, err := b.WriteString(str)
385 return err
386 case reflect.Bool:
387 _, err := b.WriteString(strconv.FormatBool(rv.Bool()))
388 return err
389 case reflect.Slice:
390 bstr := base64.StdEncoding.EncodeToString(rv.Bytes())
391 return writeJsonString(b, bstr)
392 case reflect.String:
393 return writeJsonString(b, rv.String())
394 default:
395 // must be a message
396 if dm, ok := v.(*Message); ok {
397 return dm.marshalJSON(b, opts)
398 } else {
399 var err error
400 if b.indentCount <= 0 || len(b.indent) == 0 {
401 err = opts.Marshal(b, v.(proto.Message))
402 } else {
403 str, err := opts.MarshalToString(v.(proto.Message))
404 if err != nil {
405 return err
406 }
407 indent := strings.Repeat(b.indent, b.indentCount)
408 pos := 0
409 // add indention prefix to each line
410 for pos < len(str) {
411 start := pos
412 nextPos := strings.Index(str[pos:], "\n")
413 if nextPos == -1 {
414 nextPos = len(str)
415 } else {
416 nextPos = pos + nextPos + 1 // include newline
417 }
418 line := str[start:nextPos]
419 if pos > 0 {
420 _, err = b.WriteString(indent)
421 if err != nil {
422 return err
423 }
424 }
425 _, err = b.WriteString(line)
426 if err != nil {
427 return err
428 }
429 pos = nextPos
430 }
431 }
432 return err
433 }
434 }
435}
436
437func writeJsonString(b *indentBuffer, s string) error {
438 if sbytes, err := json.Marshal(s); err != nil {
439 return err
440 } else {
441 _, err := b.Write(sbytes)
442 return err
443 }
444}
445
446// UnmarshalJSON de-serializes the message that is present, in JSON format, in
447// the given bytes into this message. It first resets the current message. It
448// returns an error if the given bytes do not contain a valid encoding of this
449// message type in JSON format.
450//
451// This method is shorthand for invoking UnmarshalJSONPB with a default (zero
452// value) unmarshaler:
453//
454// m.UnmarshalMergeJSONPB(&jsonpb.Unmarshaler{}, js)
455//
456// So unknown fields will result in an error, and no provided jsonpb.AnyResolver
457// will be used when parsing google.protobuf.Any messages.
458func (m *Message) UnmarshalJSON(js []byte) error {
459 return m.UnmarshalJSONPB(&jsonpb.Unmarshaler{}, js)
460}
461
462// UnmarshalMergeJSON de-serializes the message that is present, in JSON format,
463// in the given bytes into this message. Unlike UnmarshalJSON, it does not first
464// reset the message, instead merging the data in the given bytes into the
465// existing data in this message.
466func (m *Message) UnmarshalMergeJSON(js []byte) error {
467 return m.UnmarshalMergeJSONPB(&jsonpb.Unmarshaler{}, js)
468}
469
470// UnmarshalJSONPB de-serializes the message that is present, in JSON format, in
471// the given bytes into this message. The given unmarshaler conveys options used
472// when parsing the JSON. This function first resets the current message. It
473// returns an error if the given bytes do not contain a valid encoding of this
474// message type in JSON format.
475//
476// The decoding is lenient:
477// 1. The JSON can refer to fields either by their JSON name or by their
478// declared name.
479// 2. The JSON can use either numeric values or string names for enum values.
480//
481// When instantiating nested messages, if this message's associated factory
482// returns a generated message type (as opposed to a dynamic message), the given
483// unmarshaler is used to unmarshal it.
484//
485// When unmarshaling any nested messages, any jsonpb.AnyResolver configured in
486// the given unmarshaler is augmented with knowledge of message types known to
487// this message's descriptor (and its enclosing file and set of transitive
488// dependencies).
489func (m *Message) UnmarshalJSONPB(opts *jsonpb.Unmarshaler, js []byte) error {
490 m.Reset()
491 if err := m.UnmarshalMergeJSONPB(opts, js); err != nil {
492 return err
493 }
494 return m.Validate()
495}
496
497// UnmarshalMergeJSONPB de-serializes the message that is present, in JSON
498// format, in the given bytes into this message. The given unmarshaler conveys
499// options used when parsing the JSON. Unlike UnmarshalJSONPB, it does not first
500// reset the message, instead merging the data in the given bytes into the
501// existing data in this message.
502func (m *Message) UnmarshalMergeJSONPB(opts *jsonpb.Unmarshaler, js []byte) error {
503 r := newJsReader(js)
504 err := m.unmarshalJson(r, opts)
505 if err != nil {
506 return err
507 }
508 if t, err := r.poll(); err != io.EOF {
509 b, _ := ioutil.ReadAll(r.unread())
510 s := fmt.Sprintf("%v%s", t, string(b))
511 return fmt.Errorf("superfluous data found after JSON object: %q", s)
512 }
513 return nil
514}
515
516func unmarshalWellKnownType(m *Message, r *jsReader, opts *jsonpb.Unmarshaler) (bool, error) {
517 fqn := m.md.GetFullyQualifiedName()
518 if _, ok := wellKnownTypeNames[fqn]; !ok {
519 return false, nil
520 }
521
522 msgType := proto.MessageType(fqn)
523 if msgType == nil {
524 // wtf?
525 panic(fmt.Sprintf("could not find registered message type for %q", fqn))
526 }
527
528 // extract json value from r
529 var js json.RawMessage
530 if err := json.NewDecoder(r.unread()).Decode(&js); err != nil {
531 return true, err
532 }
533 if err := r.skip(); err != nil {
534 return true, err
535 }
536
537 // unmarshal into well-known type and then convert to dynamic message
538 msg := reflect.New(msgType.Elem()).Interface().(proto.Message)
539 if err := opts.Unmarshal(bytes.NewReader(js), msg); err != nil {
540 return true, err
541 }
542 return true, m.MergeFrom(msg)
543}
544
545func (m *Message) unmarshalJson(r *jsReader, opts *jsonpb.Unmarshaler) error {
546 if r, changed := wrapResolver(opts.AnyResolver, m.mf, m.md.GetFile()); changed {
547 newOpts := *opts
548 newOpts.AnyResolver = r
549 opts = &newOpts
550 }
551
552 if ok, err := unmarshalWellKnownType(m, r, opts); ok {
553 return err
554 }
555
556 t, err := r.peek()
557 if err != nil {
558 return err
559 }
560 if t == nil {
561 // if json is simply "null" we do nothing
562 r.poll()
563 return nil
564 }
565
566 if err := r.beginObject(); err != nil {
567 return err
568 }
569
570 for r.hasNext() {
571 f, err := r.nextObjectKey()
572 if err != nil {
573 return err
574 }
575 fd := m.FindFieldDescriptorByJSONName(f)
576 if fd == nil {
577 if opts.AllowUnknownFields {
578 r.skip()
579 continue
580 }
581 return fmt.Errorf("message type %s has no known field named %s", m.md.GetFullyQualifiedName(), f)
582 }
583 v, err := unmarshalJsField(fd, r, m.mf, opts)
584 if err != nil {
585 return err
586 }
587 if v != nil {
588 if err := mergeField(m, fd, v); err != nil {
589 return err
590 }
591 } else if fd.GetOneOf() != nil {
592 // preserve explicit null for oneof fields (this is a little odd but
593 // mimics the behavior of jsonpb with oneofs in generated message types)
594 if fd.GetMessageType() != nil {
595 typ := m.mf.GetKnownTypeRegistry().GetKnownType(fd.GetMessageType().GetFullyQualifiedName())
596 if typ != nil {
597 // typed nil
598 if typ.Kind() != reflect.Ptr {
599 typ = reflect.PtrTo(typ)
600 }
601 v = reflect.Zero(typ).Interface()
602 } else {
603 // can't use nil dynamic message, so we just use empty one instead
604 v = m.mf.NewDynamicMessage(fd.GetMessageType())
605 }
606 if err := m.setField(fd, v); err != nil {
607 return err
608 }
609 } else {
610 // not a message... explicit null makes no sense
611 return fmt.Errorf("message type %s cannot set field %s to null: it is not a message type", m.md.GetFullyQualifiedName(), f)
612 }
613 } else {
614 m.clearField(fd)
615 }
616 }
617
618 if err := r.endObject(); err != nil {
619 return err
620 }
621
622 return nil
623}
624
625func isWellKnownValue(fd *desc.FieldDescriptor) bool {
626 return !fd.IsRepeated() && fd.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE &&
627 fd.GetMessageType().GetFullyQualifiedName() == "google.protobuf.Value"
628}
629
630func isWellKnownListValue(fd *desc.FieldDescriptor) bool {
631 return !fd.IsRepeated() && fd.GetType() == descriptor.FieldDescriptorProto_TYPE_MESSAGE &&
632 fd.GetMessageType().GetFullyQualifiedName() == "google.protobuf.ListValue"
633}
634
635func unmarshalJsField(fd *desc.FieldDescriptor, r *jsReader, mf *MessageFactory, opts *jsonpb.Unmarshaler) (interface{}, error) {
636 t, err := r.peek()
637 if err != nil {
638 return nil, err
639 }
640 if t == nil && !isWellKnownValue(fd) {
641 // if value is null, just return nil
642 // (unless field is google.protobuf.Value, in which case
643 // we fall through to parse it as an instance where its
644 // underlying value is set to a NullValue)
645 r.poll()
646 return nil, nil
647 }
648
649 if t == json.Delim('{') && fd.IsMap() {
650 entryType := fd.GetMessageType()
651 keyType := entryType.FindFieldByNumber(1)
652 valueType := entryType.FindFieldByNumber(2)
653 mp := map[interface{}]interface{}{}
654
655 // TODO: if there are just two map keys "key" and "value" and they have the right type of values,
656 // treat this JSON object as a single map entry message. (In keeping with support of map fields as
657 // if they were normal repeated field of entry messages as well as supporting a transition from
658 // optional to repeated...)
659
660 if err := r.beginObject(); err != nil {
661 return nil, err
662 }
663 for r.hasNext() {
664 kk, err := unmarshalJsFieldElement(keyType, r, mf, opts)
665 if err != nil {
666 return nil, err
667 }
668 vv, err := unmarshalJsFieldElement(valueType, r, mf, opts)
669 if err != nil {
670 return nil, err
671 }
672 mp[kk] = vv
673 }
674 if err := r.endObject(); err != nil {
675 return nil, err
676 }
677
678 return mp, nil
679 } else if t == json.Delim('[') && !isWellKnownListValue(fd) {
680 // We support parsing an array, even if field is not repeated, to mimic support in proto
681 // binary wire format that supports changing an optional field to repeated and vice versa.
682 // If the field is not repeated, we only keep the last value in the array.
683
684 if err := r.beginArray(); err != nil {
685 return nil, err
686 }
687 var sl []interface{}
688 var v interface{}
689 for r.hasNext() {
690 var err error
691 v, err = unmarshalJsFieldElement(fd, r, mf, opts)
692 if err != nil {
693 return nil, err
694 }
695 if fd.IsRepeated() && v != nil {
696 sl = append(sl, v)
697 }
698 }
699 if err := r.endArray(); err != nil {
700 return nil, err
701 }
702 if fd.IsMap() {
703 mp := map[interface{}]interface{}{}
704 for _, m := range sl {
705 msg := m.(*Message)
706 kk, err := msg.TryGetFieldByNumber(1)
707 if err != nil {
708 return nil, err
709 }
710 vv, err := msg.TryGetFieldByNumber(2)
711 if err != nil {
712 return nil, err
713 }
714 mp[kk] = vv
715 }
716 return mp, nil
717 } else if fd.IsRepeated() {
718 return sl, nil
719 } else {
720 return v, nil
721 }
722 } else {
723 // We support parsing a singular value, even if field is repeated, to mimic support in proto
724 // binary wire format that supports changing an optional field to repeated and vice versa.
725 // If the field is repeated, we store value as singleton slice of that one value.
726
727 v, err := unmarshalJsFieldElement(fd, r, mf, opts)
728 if err != nil {
729 return nil, err
730 }
731 if v == nil {
732 return nil, nil
733 }
734 if fd.IsRepeated() {
735 return []interface{}{v}, nil
736 } else {
737 return v, nil
738 }
739 }
740}
741
742func unmarshalJsFieldElement(fd *desc.FieldDescriptor, r *jsReader, mf *MessageFactory, opts *jsonpb.Unmarshaler) (interface{}, error) {
743 t, err := r.peek()
744 if err != nil {
745 return nil, err
746 }
747
748 switch fd.GetType() {
749 case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
750 descriptor.FieldDescriptorProto_TYPE_GROUP:
751 m := mf.NewMessage(fd.GetMessageType())
752 if dm, ok := m.(*Message); ok {
753 if err := dm.unmarshalJson(r, opts); err != nil {
754 return nil, err
755 }
756 } else {
757 var msg json.RawMessage
758 if err := json.NewDecoder(r.unread()).Decode(&msg); err != nil {
759 return nil, err
760 }
761 if err := r.skip(); err != nil {
762 return nil, err
763 }
764 if err := opts.Unmarshal(bytes.NewReader([]byte(msg)), m); err != nil {
765 return nil, err
766 }
767 }
768 return m, nil
769
770 case descriptor.FieldDescriptorProto_TYPE_ENUM:
771 if e, err := r.nextNumber(); err != nil {
772 return nil, err
773 } else {
774 // value could be string or number
775 if i, err := e.Int64(); err != nil {
776 // number cannot be parsed, so see if it's an enum value name
777 vd := fd.GetEnumType().FindValueByName(string(e))
778 if vd != nil {
779 return vd.GetNumber(), nil
780 } else {
781 return nil, fmt.Errorf("enum %q does not have value named %q", fd.GetEnumType().GetFullyQualifiedName(), e)
782 }
783 } else if i > math.MaxInt32 || i < math.MinInt32 {
784 return nil, NumericOverflowError
785 } else {
786 return int32(i), err
787 }
788 }
789
790 case descriptor.FieldDescriptorProto_TYPE_INT32,
791 descriptor.FieldDescriptorProto_TYPE_SINT32,
792 descriptor.FieldDescriptorProto_TYPE_SFIXED32:
793 if i, err := r.nextInt(); err != nil {
794 return nil, err
795 } else if i > math.MaxInt32 || i < math.MinInt32 {
796 return nil, NumericOverflowError
797 } else {
798 return int32(i), err
799 }
800
801 case descriptor.FieldDescriptorProto_TYPE_INT64,
802 descriptor.FieldDescriptorProto_TYPE_SINT64,
803 descriptor.FieldDescriptorProto_TYPE_SFIXED64:
804 return r.nextInt()
805
806 case descriptor.FieldDescriptorProto_TYPE_UINT32,
807 descriptor.FieldDescriptorProto_TYPE_FIXED32:
808 if i, err := r.nextUint(); err != nil {
809 return nil, err
810 } else if i > math.MaxUint32 {
811 return nil, NumericOverflowError
812 } else {
813 return uint32(i), err
814 }
815
816 case descriptor.FieldDescriptorProto_TYPE_UINT64,
817 descriptor.FieldDescriptorProto_TYPE_FIXED64:
818 return r.nextUint()
819
820 case descriptor.FieldDescriptorProto_TYPE_BOOL:
821 if str, ok := t.(string); ok {
822 if str == "true" {
823 r.poll() // consume token
824 return true, err
825 } else if str == "false" {
826 r.poll() // consume token
827 return false, err
828 }
829 }
830 return r.nextBool()
831
832 case descriptor.FieldDescriptorProto_TYPE_FLOAT:
833 if f, err := r.nextFloat(); err != nil {
834 return nil, err
835 } else {
836 return float32(f), nil
837 }
838
839 case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
840 return r.nextFloat()
841
842 case descriptor.FieldDescriptorProto_TYPE_BYTES:
843 return r.nextBytes()
844
845 case descriptor.FieldDescriptorProto_TYPE_STRING:
846 return r.nextString()
847
848 default:
849 return nil, fmt.Errorf("unknown field type: %v", fd.GetType())
850 }
851}
852
853type jsReader struct {
854 reader *bytes.Reader
855 dec *json.Decoder
856 current json.Token
857 peeked bool
858}
859
860func newJsReader(b []byte) *jsReader {
861 reader := bytes.NewReader(b)
862 dec := json.NewDecoder(reader)
863 dec.UseNumber()
864 return &jsReader{reader: reader, dec: dec}
865}
866
867func (r *jsReader) unread() io.Reader {
868 bufs := make([]io.Reader, 3)
869 var peeked []byte
870 if r.peeked {
871 if _, ok := r.current.(json.Delim); ok {
872 peeked = []byte(fmt.Sprintf("%v", r.current))
873 } else {
874 peeked, _ = json.Marshal(r.current)
875 }
876 }
877 readerCopy := *r.reader
878 decCopy := *r.dec
879
880 bufs[0] = bytes.NewReader(peeked)
881 bufs[1] = decCopy.Buffered()
882 bufs[2] = &readerCopy
883 return &concatReader{bufs: bufs}
884}
885
886func (r *jsReader) hasNext() bool {
887 return r.dec.More()
888}
889
890func (r *jsReader) peek() (json.Token, error) {
891 if r.peeked {
892 return r.current, nil
893 }
894 t, err := r.dec.Token()
895 if err != nil {
896 return nil, err
897 }
898 r.peeked = true
899 r.current = t
900 return t, nil
901}
902
903func (r *jsReader) poll() (json.Token, error) {
904 if r.peeked {
905 ret := r.current
906 r.current = nil
907 r.peeked = false
908 return ret, nil
909 }
910 return r.dec.Token()
911}
912
913func (r *jsReader) beginObject() error {
914 _, err := r.expect(func(t json.Token) bool { return t == json.Delim('{') }, nil, "start of JSON object: '{'")
915 return err
916}
917
918func (r *jsReader) endObject() error {
919 _, err := r.expect(func(t json.Token) bool { return t == json.Delim('}') }, nil, "end of JSON object: '}'")
920 return err
921}
922
923func (r *jsReader) beginArray() error {
924 _, err := r.expect(func(t json.Token) bool { return t == json.Delim('[') }, nil, "start of array: '['")
925 return err
926}
927
928func (r *jsReader) endArray() error {
929 _, err := r.expect(func(t json.Token) bool { return t == json.Delim(']') }, nil, "end of array: ']'")
930 return err
931}
932
933func (r *jsReader) nextObjectKey() (string, error) {
934 return r.nextString()
935}
936
937func (r *jsReader) nextString() (string, error) {
938 t, err := r.expect(func(t json.Token) bool { _, ok := t.(string); return ok }, "", "string")
939 if err != nil {
940 return "", err
941 }
942 return t.(string), nil
943}
944
945func (r *jsReader) nextBytes() ([]byte, error) {
946 str, err := r.nextString()
947 if err != nil {
948 return nil, err
949 }
950 return base64.StdEncoding.DecodeString(str)
951}
952
953func (r *jsReader) nextBool() (bool, error) {
954 t, err := r.expect(func(t json.Token) bool { _, ok := t.(bool); return ok }, false, "boolean")
955 if err != nil {
956 return false, err
957 }
958 return t.(bool), nil
959}
960
961func (r *jsReader) nextInt() (int64, error) {
962 n, err := r.nextNumber()
963 if err != nil {
964 return 0, err
965 }
966 return n.Int64()
967}
968
969func (r *jsReader) nextUint() (uint64, error) {
970 n, err := r.nextNumber()
971 if err != nil {
972 return 0, err
973 }
974 return strconv.ParseUint(string(n), 10, 64)
975}
976
977func (r *jsReader) nextFloat() (float64, error) {
978 n, err := r.nextNumber()
979 if err != nil {
980 return 0, err
981 }
982 return n.Float64()
983}
984
985func (r *jsReader) nextNumber() (json.Number, error) {
986 t, err := r.expect(func(t json.Token) bool { return reflect.TypeOf(t).Kind() == reflect.String }, "0", "number")
987 if err != nil {
988 return "", err
989 }
990 switch t := t.(type) {
991 case json.Number:
992 return t, nil
993 case string:
994 return json.Number(t), nil
995 }
996 return "", fmt.Errorf("expecting a number but got %v", t)
997}
998
999func (r *jsReader) skip() error {
1000 t, err := r.poll()
1001 if err != nil {
1002 return err
1003 }
1004 if t == json.Delim('[') {
1005 if err := r.skipArray(); err != nil {
1006 return err
1007 }
1008 } else if t == json.Delim('{') {
1009 if err := r.skipObject(); err != nil {
1010 return err
1011 }
1012 }
1013 return nil
1014}
1015
1016func (r *jsReader) skipArray() error {
1017 for r.hasNext() {
1018 if err := r.skip(); err != nil {
1019 return err
1020 }
1021 }
1022 if err := r.endArray(); err != nil {
1023 return err
1024 }
1025 return nil
1026}
1027
1028func (r *jsReader) skipObject() error {
1029 for r.hasNext() {
1030 // skip object key
1031 if err := r.skip(); err != nil {
1032 return err
1033 }
1034 // and value
1035 if err := r.skip(); err != nil {
1036 return err
1037 }
1038 }
1039 if err := r.endObject(); err != nil {
1040 return err
1041 }
1042 return nil
1043}
1044
1045func (r *jsReader) expect(predicate func(json.Token) bool, ifNil interface{}, expected string) (interface{}, error) {
1046 t, err := r.poll()
1047 if err != nil {
1048 return nil, err
1049 }
1050 if t == nil && ifNil != nil {
1051 return ifNil, nil
1052 }
1053 if !predicate(t) {
1054 return t, fmt.Errorf("bad input: expecting %s ; instead got %v", expected, t)
1055 }
1056 return t, nil
1057}
1058
1059type concatReader struct {
1060 bufs []io.Reader
1061 curr int
1062}
1063
1064func (r *concatReader) Read(p []byte) (n int, err error) {
1065 for {
1066 if r.curr >= len(r.bufs) {
1067 err = io.EOF
1068 return
1069 }
1070 var c int
1071 c, err = r.bufs[r.curr].Read(p)
1072 n += c
1073 if err != io.EOF {
1074 return
1075 }
1076 r.curr++
1077 p = p[c:]
1078 }
1079}
1080
1081// AnyResolver returns a jsonpb.AnyResolver that uses the given file descriptors
1082// to resolve message names. It uses the given factory, which may be nil, to
1083// instantiate messages. The messages that it returns when resolving a type name
1084// may often be dynamic messages.
1085func AnyResolver(mf *MessageFactory, files ...*desc.FileDescriptor) jsonpb.AnyResolver {
1086 return &anyResolver{mf: mf, files: files}
1087}
1088
1089type anyResolver struct {
1090 mf *MessageFactory
1091 files []*desc.FileDescriptor
1092 ignored map[*desc.FileDescriptor]struct{}
1093 other jsonpb.AnyResolver
1094}
1095
1096func wrapResolver(r jsonpb.AnyResolver, mf *MessageFactory, f *desc.FileDescriptor) (jsonpb.AnyResolver, bool) {
1097 if r, ok := r.(*anyResolver); ok {
1098 if _, ok := r.ignored[f]; ok {
1099 // if the current resolver is ignoring this file, it's because another
1100 // (upstream) resolver is already handling it, so nothing to do
1101 return r, false
1102 }
1103 for _, file := range r.files {
1104 if file == f {
1105 // no need to wrap!
1106 return r, false
1107 }
1108 }
1109 // ignore files that will be checked by the resolver we're wrapping
1110 // (we'll just delegate and let it search those files)
1111 ignored := map[*desc.FileDescriptor]struct{}{}
1112 for i := range r.ignored {
1113 ignored[i] = struct{}{}
1114 }
1115 ignore(r.files, ignored)
1116 return &anyResolver{mf: mf, files: []*desc.FileDescriptor{f}, ignored: ignored, other: r}, true
1117 }
1118 return &anyResolver{mf: mf, files: []*desc.FileDescriptor{f}, other: r}, true
1119}
1120
1121func ignore(files []*desc.FileDescriptor, ignored map[*desc.FileDescriptor]struct{}) {
1122 for _, f := range files {
1123 if _, ok := ignored[f]; ok {
1124 continue
1125 }
1126 ignored[f] = struct{}{}
1127 ignore(f.GetDependencies(), ignored)
1128 }
1129}
1130
1131func (r *anyResolver) Resolve(typeUrl string) (proto.Message, error) {
1132 mname := typeUrl
1133 if slash := strings.LastIndex(mname, "/"); slash >= 0 {
1134 mname = mname[slash+1:]
1135 }
1136
1137 // see if the user-specified resolver is able to do the job
1138 if r.other != nil {
1139 msg, err := r.other.Resolve(typeUrl)
1140 if err == nil {
1141 return msg, nil
1142 }
1143 }
1144
1145 // try to find the message in our known set of files
1146 checked := map[*desc.FileDescriptor]struct{}{}
1147 for _, f := range r.files {
1148 md := r.findMessage(f, mname, checked)
1149 if md != nil {
1150 return r.mf.NewMessage(md), nil
1151 }
1152 }
1153 // failing that, see if the message factory knows about this type
1154 var ktr *KnownTypeRegistry
1155 if r.mf != nil {
1156 ktr = r.mf.ktr
1157 } else {
1158 ktr = (*KnownTypeRegistry)(nil)
1159 }
1160 m := ktr.CreateIfKnown(mname)
1161 if m != nil {
1162 return m, nil
1163 }
1164
1165 // no other resolver to fallback to? mimic default behavior
1166 mt := proto.MessageType(mname)
1167 if mt == nil {
1168 return nil, fmt.Errorf("unknown message type %q", mname)
1169 }
1170 return reflect.New(mt.Elem()).Interface().(proto.Message), nil
1171}
1172
1173func (r *anyResolver) findMessage(fd *desc.FileDescriptor, msgName string, checked map[*desc.FileDescriptor]struct{}) *desc.MessageDescriptor {
1174 // if this is an ignored descriptor, skip
1175 if _, ok := r.ignored[fd]; ok {
1176 return nil
1177 }
1178
1179 // bail if we've already checked this file
1180 if _, ok := checked[fd]; ok {
1181 return nil
1182 }
1183 checked[fd] = struct{}{}
1184
1185 // see if this file has the message
1186 md := fd.FindMessage(msgName)
1187 if md != nil {
1188 return md
1189 }
1190
1191 // if not, recursively search the file's imports
1192 for _, dep := range fd.GetDependencies() {
1193 md = r.findMessage(dep, msgName, checked)
1194 if md != nil {
1195 return md
1196 }
1197 }
1198 return nil
1199}
1200
1201var _ jsonpb.AnyResolver = (*anyResolver)(nil)