blob: d63732fcbda5eb4a66524064ef4db9119e487535 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// Protocol Buffers for Go with Gadgets
2//
3// Copyright (c) 2013, The GoGo Authors. All rights reserved.
4// http://github.com/gogo/protobuf
5//
6// Go support for Protocol Buffers - Google's data interchange format
7//
8// Copyright 2010 The Go Authors. All rights reserved.
9// https://github.com/golang/protobuf
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// * Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// * Redistributions in binary form must reproduce the above
18// copyright notice, this list of conditions and the following disclaimer
19// in the documentation and/or other materials provided with the
20// distribution.
21// * Neither the name of Google Inc. nor the names of its
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37package proto
38
39// Functions for writing the text protocol buffer format.
40
41import (
42 "bufio"
43 "bytes"
44 "encoding"
45 "errors"
46 "fmt"
47 "io"
48 "log"
49 "math"
50 "reflect"
51 "sort"
52 "strings"
53 "sync"
54 "time"
55)
56
57var (
58 newline = []byte("\n")
59 spaces = []byte(" ")
60 gtNewline = []byte(">\n")
61 endBraceNewline = []byte("}\n")
62 backslashN = []byte{'\\', 'n'}
63 backslashR = []byte{'\\', 'r'}
64 backslashT = []byte{'\\', 't'}
65 backslashDQ = []byte{'\\', '"'}
66 backslashBS = []byte{'\\', '\\'}
67 posInf = []byte("inf")
68 negInf = []byte("-inf")
69 nan = []byte("nan")
70)
71
72type writer interface {
73 io.Writer
74 WriteByte(byte) error
75}
76
77// textWriter is an io.Writer that tracks its indentation level.
78type textWriter struct {
79 ind int
80 complete bool // if the current position is a complete line
81 compact bool // whether to write out as a one-liner
82 w writer
83}
84
85func (w *textWriter) WriteString(s string) (n int, err error) {
86 if !strings.Contains(s, "\n") {
87 if !w.compact && w.complete {
88 w.writeIndent()
89 }
90 w.complete = false
91 return io.WriteString(w.w, s)
92 }
93 // WriteString is typically called without newlines, so this
94 // codepath and its copy are rare. We copy to avoid
95 // duplicating all of Write's logic here.
96 return w.Write([]byte(s))
97}
98
99func (w *textWriter) Write(p []byte) (n int, err error) {
100 newlines := bytes.Count(p, newline)
101 if newlines == 0 {
102 if !w.compact && w.complete {
103 w.writeIndent()
104 }
105 n, err = w.w.Write(p)
106 w.complete = false
107 return n, err
108 }
109
110 frags := bytes.SplitN(p, newline, newlines+1)
111 if w.compact {
112 for i, frag := range frags {
113 if i > 0 {
114 if err := w.w.WriteByte(' '); err != nil {
115 return n, err
116 }
117 n++
118 }
119 nn, err := w.w.Write(frag)
120 n += nn
121 if err != nil {
122 return n, err
123 }
124 }
125 return n, nil
126 }
127
128 for i, frag := range frags {
129 if w.complete {
130 w.writeIndent()
131 }
132 nn, err := w.w.Write(frag)
133 n += nn
134 if err != nil {
135 return n, err
136 }
137 if i+1 < len(frags) {
138 if err := w.w.WriteByte('\n'); err != nil {
139 return n, err
140 }
141 n++
142 }
143 }
144 w.complete = len(frags[len(frags)-1]) == 0
145 return n, nil
146}
147
148func (w *textWriter) WriteByte(c byte) error {
149 if w.compact && c == '\n' {
150 c = ' '
151 }
152 if !w.compact && w.complete {
153 w.writeIndent()
154 }
155 err := w.w.WriteByte(c)
156 w.complete = c == '\n'
157 return err
158}
159
160func (w *textWriter) indent() { w.ind++ }
161
162func (w *textWriter) unindent() {
163 if w.ind == 0 {
164 log.Print("proto: textWriter unindented too far")
165 return
166 }
167 w.ind--
168}
169
170func writeName(w *textWriter, props *Properties) error {
171 if _, err := w.WriteString(props.OrigName); err != nil {
172 return err
173 }
174 if props.Wire != "group" {
175 return w.WriteByte(':')
176 }
177 return nil
178}
179
180// raw is the interface satisfied by RawMessage.
181type raw interface {
182 Bytes() []byte
183}
184
185func requiresQuotes(u string) bool {
186 // When type URL contains any characters except [0-9A-Za-z./\-]*, it must be quoted.
187 for _, ch := range u {
188 switch {
189 case ch == '.' || ch == '/' || ch == '_':
190 continue
191 case '0' <= ch && ch <= '9':
192 continue
193 case 'A' <= ch && ch <= 'Z':
194 continue
195 case 'a' <= ch && ch <= 'z':
196 continue
197 default:
198 return true
199 }
200 }
201 return false
202}
203
204// isAny reports whether sv is a google.protobuf.Any message
205func isAny(sv reflect.Value) bool {
206 type wkt interface {
207 XXX_WellKnownType() string
208 }
209 t, ok := sv.Addr().Interface().(wkt)
210 return ok && t.XXX_WellKnownType() == "Any"
211}
212
213// writeProto3Any writes an expanded google.protobuf.Any message.
214//
215// It returns (false, nil) if sv value can't be unmarshaled (e.g. because
216// required messages are not linked in).
217//
218// It returns (true, error) when sv was written in expanded format or an error
219// was encountered.
220func (tm *TextMarshaler) writeProto3Any(w *textWriter, sv reflect.Value) (bool, error) {
221 turl := sv.FieldByName("TypeUrl")
222 val := sv.FieldByName("Value")
223 if !turl.IsValid() || !val.IsValid() {
224 return true, errors.New("proto: invalid google.protobuf.Any message")
225 }
226
227 b, ok := val.Interface().([]byte)
228 if !ok {
229 return true, errors.New("proto: invalid google.protobuf.Any message")
230 }
231
232 parts := strings.Split(turl.String(), "/")
233 mt := MessageType(parts[len(parts)-1])
234 if mt == nil {
235 return false, nil
236 }
237 m := reflect.New(mt.Elem())
238 if err := Unmarshal(b, m.Interface().(Message)); err != nil {
239 return false, nil
240 }
241 w.Write([]byte("["))
242 u := turl.String()
243 if requiresQuotes(u) {
244 writeString(w, u)
245 } else {
246 w.Write([]byte(u))
247 }
248 if w.compact {
249 w.Write([]byte("]:<"))
250 } else {
251 w.Write([]byte("]: <\n"))
252 w.ind++
253 }
254 if err := tm.writeStruct(w, m.Elem()); err != nil {
255 return true, err
256 }
257 if w.compact {
258 w.Write([]byte("> "))
259 } else {
260 w.ind--
261 w.Write([]byte(">\n"))
262 }
263 return true, nil
264}
265
266func (tm *TextMarshaler) writeStruct(w *textWriter, sv reflect.Value) error {
267 if tm.ExpandAny && isAny(sv) {
268 if canExpand, err := tm.writeProto3Any(w, sv); canExpand {
269 return err
270 }
271 }
272 st := sv.Type()
273 sprops := GetProperties(st)
274 for i := 0; i < sv.NumField(); i++ {
275 fv := sv.Field(i)
276 props := sprops.Prop[i]
277 name := st.Field(i).Name
278
279 if strings.HasPrefix(name, "XXX_") {
280 // There are two XXX_ fields:
281 // XXX_unrecognized []byte
282 // XXX_extensions map[int32]proto.Extension
283 // The first is handled here;
284 // the second is handled at the bottom of this function.
285 if name == "XXX_unrecognized" && !fv.IsNil() {
286 if err := writeUnknownStruct(w, fv.Interface().([]byte)); err != nil {
287 return err
288 }
289 }
290 continue
291 }
292 if fv.Kind() == reflect.Ptr && fv.IsNil() {
293 // Field not filled in. This could be an optional field or
294 // a required field that wasn't filled in. Either way, there
295 // isn't anything we can show for it.
296 continue
297 }
298 if fv.Kind() == reflect.Slice && fv.IsNil() {
299 // Repeated field that is empty, or a bytes field that is unused.
300 continue
301 }
302
303 if props.Repeated && fv.Kind() == reflect.Slice {
304 // Repeated field.
305 for j := 0; j < fv.Len(); j++ {
306 if err := writeName(w, props); err != nil {
307 return err
308 }
309 if !w.compact {
310 if err := w.WriteByte(' '); err != nil {
311 return err
312 }
313 }
314 v := fv.Index(j)
315 if v.Kind() == reflect.Ptr && v.IsNil() {
316 // A nil message in a repeated field is not valid,
317 // but we can handle that more gracefully than panicking.
318 if _, err := w.Write([]byte("<nil>\n")); err != nil {
319 return err
320 }
321 continue
322 }
323 if len(props.Enum) > 0 {
324 if err := tm.writeEnum(w, v, props); err != nil {
325 return err
326 }
327 } else if err := tm.writeAny(w, v, props); err != nil {
328 return err
329 }
330 if err := w.WriteByte('\n'); err != nil {
331 return err
332 }
333 }
334 continue
335 }
336 if fv.Kind() == reflect.Map {
337 // Map fields are rendered as a repeated struct with key/value fields.
338 keys := fv.MapKeys()
339 sort.Sort(mapKeys(keys))
340 for _, key := range keys {
341 val := fv.MapIndex(key)
342 if err := writeName(w, props); err != nil {
343 return err
344 }
345 if !w.compact {
346 if err := w.WriteByte(' '); err != nil {
347 return err
348 }
349 }
350 // open struct
351 if err := w.WriteByte('<'); err != nil {
352 return err
353 }
354 if !w.compact {
355 if err := w.WriteByte('\n'); err != nil {
356 return err
357 }
358 }
359 w.indent()
360 // key
361 if _, err := w.WriteString("key:"); err != nil {
362 return err
363 }
364 if !w.compact {
365 if err := w.WriteByte(' '); err != nil {
366 return err
367 }
368 }
369 if err := tm.writeAny(w, key, props.mkeyprop); err != nil {
370 return err
371 }
372 if err := w.WriteByte('\n'); err != nil {
373 return err
374 }
375 // nil values aren't legal, but we can avoid panicking because of them.
376 if val.Kind() != reflect.Ptr || !val.IsNil() {
377 // value
378 if _, err := w.WriteString("value:"); err != nil {
379 return err
380 }
381 if !w.compact {
382 if err := w.WriteByte(' '); err != nil {
383 return err
384 }
385 }
386 if err := tm.writeAny(w, val, props.mvalprop); err != nil {
387 return err
388 }
389 if err := w.WriteByte('\n'); err != nil {
390 return err
391 }
392 }
393 // close struct
394 w.unindent()
395 if err := w.WriteByte('>'); err != nil {
396 return err
397 }
398 if err := w.WriteByte('\n'); err != nil {
399 return err
400 }
401 }
402 continue
403 }
404 if props.proto3 && fv.Kind() == reflect.Slice && fv.Len() == 0 {
405 // empty bytes field
406 continue
407 }
408 if props.proto3 && fv.Kind() != reflect.Ptr && fv.Kind() != reflect.Slice {
409 // proto3 non-repeated scalar field; skip if zero value
410 if isProto3Zero(fv) {
411 continue
412 }
413 }
414
415 if fv.Kind() == reflect.Interface {
416 // Check if it is a oneof.
417 if st.Field(i).Tag.Get("protobuf_oneof") != "" {
418 // fv is nil, or holds a pointer to generated struct.
419 // That generated struct has exactly one field,
420 // which has a protobuf struct tag.
421 if fv.IsNil() {
422 continue
423 }
424 inner := fv.Elem().Elem() // interface -> *T -> T
425 tag := inner.Type().Field(0).Tag.Get("protobuf")
426 props = new(Properties) // Overwrite the outer props var, but not its pointee.
427 props.Parse(tag)
428 // Write the value in the oneof, not the oneof itself.
429 fv = inner.Field(0)
430
431 // Special case to cope with malformed messages gracefully:
432 // If the value in the oneof is a nil pointer, don't panic
433 // in writeAny.
434 if fv.Kind() == reflect.Ptr && fv.IsNil() {
435 // Use errors.New so writeAny won't render quotes.
436 msg := errors.New("/* nil */")
437 fv = reflect.ValueOf(&msg).Elem()
438 }
439 }
440 }
441
442 if err := writeName(w, props); err != nil {
443 return err
444 }
445 if !w.compact {
446 if err := w.WriteByte(' '); err != nil {
447 return err
448 }
449 }
450 if b, ok := fv.Interface().(raw); ok {
451 if err := writeRaw(w, b.Bytes()); err != nil {
452 return err
453 }
454 continue
455 }
456
457 if len(props.Enum) > 0 {
458 if err := tm.writeEnum(w, fv, props); err != nil {
459 return err
460 }
461 } else if err := tm.writeAny(w, fv, props); err != nil {
462 return err
463 }
464
465 if err := w.WriteByte('\n'); err != nil {
466 return err
467 }
468 }
469
470 // Extensions (the XXX_extensions field).
471 pv := sv
472 if pv.CanAddr() {
473 pv = sv.Addr()
474 } else {
475 pv = reflect.New(sv.Type())
476 pv.Elem().Set(sv)
477 }
478 if pv.Type().Implements(extensionRangeType) {
479 if err := tm.writeExtensions(w, pv); err != nil {
480 return err
481 }
482 }
483
484 return nil
485}
486
487// writeRaw writes an uninterpreted raw message.
488func writeRaw(w *textWriter, b []byte) error {
489 if err := w.WriteByte('<'); err != nil {
490 return err
491 }
492 if !w.compact {
493 if err := w.WriteByte('\n'); err != nil {
494 return err
495 }
496 }
497 w.indent()
498 if err := writeUnknownStruct(w, b); err != nil {
499 return err
500 }
501 w.unindent()
502 if err := w.WriteByte('>'); err != nil {
503 return err
504 }
505 return nil
506}
507
508// writeAny writes an arbitrary field.
509func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Properties) error {
510 v = reflect.Indirect(v)
511
512 if props != nil {
513 if len(props.CustomType) > 0 {
514 custom, ok := v.Interface().(Marshaler)
515 if ok {
516 data, err := custom.Marshal()
517 if err != nil {
518 return err
519 }
520 if err := writeString(w, string(data)); err != nil {
521 return err
522 }
523 return nil
524 }
525 } else if props.StdTime {
526 t, ok := v.Interface().(time.Time)
527 if !ok {
528 return fmt.Errorf("stdtime is not time.Time, but %T", v.Interface())
529 }
530 tproto, err := timestampProto(t)
531 if err != nil {
532 return err
533 }
534 props.StdTime = false
535 err = tm.writeAny(w, reflect.ValueOf(tproto), props)
536 props.StdTime = true
537 return err
538 } else if props.StdDuration {
539 d, ok := v.Interface().(time.Duration)
540 if !ok {
541 return fmt.Errorf("stdtime is not time.Duration, but %T", v.Interface())
542 }
543 dproto := durationProto(d)
544 props.StdDuration = false
545 err := tm.writeAny(w, reflect.ValueOf(dproto), props)
546 props.StdDuration = true
547 return err
548 }
549 }
550
551 // Floats have special cases.
552 if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
553 x := v.Float()
554 var b []byte
555 switch {
556 case math.IsInf(x, 1):
557 b = posInf
558 case math.IsInf(x, -1):
559 b = negInf
560 case math.IsNaN(x):
561 b = nan
562 }
563 if b != nil {
564 _, err := w.Write(b)
565 return err
566 }
567 // Other values are handled below.
568 }
569
570 // We don't attempt to serialise every possible value type; only those
571 // that can occur in protocol buffers.
572 switch v.Kind() {
573 case reflect.Slice:
574 // Should only be a []byte; repeated fields are handled in writeStruct.
575 if err := writeString(w, string(v.Bytes())); err != nil {
576 return err
577 }
578 case reflect.String:
579 if err := writeString(w, v.String()); err != nil {
580 return err
581 }
582 case reflect.Struct:
583 // Required/optional group/message.
584 var bra, ket byte = '<', '>'
585 if props != nil && props.Wire == "group" {
586 bra, ket = '{', '}'
587 }
588 if err := w.WriteByte(bra); err != nil {
589 return err
590 }
591 if !w.compact {
592 if err := w.WriteByte('\n'); err != nil {
593 return err
594 }
595 }
596 w.indent()
597 if etm, ok := v.Interface().(encoding.TextMarshaler); ok {
598 text, err := etm.MarshalText()
599 if err != nil {
600 return err
601 }
602 if _, err = w.Write(text); err != nil {
603 return err
604 }
605 } else if err := tm.writeStruct(w, v); err != nil {
606 return err
607 }
608 w.unindent()
609 if err := w.WriteByte(ket); err != nil {
610 return err
611 }
612 default:
613 _, err := fmt.Fprint(w, v.Interface())
614 return err
615 }
616 return nil
617}
618
619// equivalent to C's isprint.
620func isprint(c byte) bool {
621 return c >= 0x20 && c < 0x7f
622}
623
624// writeString writes a string in the protocol buffer text format.
625// It is similar to strconv.Quote except we don't use Go escape sequences,
626// we treat the string as a byte sequence, and we use octal escapes.
627// These differences are to maintain interoperability with the other
628// languages' implementations of the text format.
629func writeString(w *textWriter, s string) error {
630 // use WriteByte here to get any needed indent
631 if err := w.WriteByte('"'); err != nil {
632 return err
633 }
634 // Loop over the bytes, not the runes.
635 for i := 0; i < len(s); i++ {
636 var err error
637 // Divergence from C++: we don't escape apostrophes.
638 // There's no need to escape them, and the C++ parser
639 // copes with a naked apostrophe.
640 switch c := s[i]; c {
641 case '\n':
642 _, err = w.w.Write(backslashN)
643 case '\r':
644 _, err = w.w.Write(backslashR)
645 case '\t':
646 _, err = w.w.Write(backslashT)
647 case '"':
648 _, err = w.w.Write(backslashDQ)
649 case '\\':
650 _, err = w.w.Write(backslashBS)
651 default:
652 if isprint(c) {
653 err = w.w.WriteByte(c)
654 } else {
655 _, err = fmt.Fprintf(w.w, "\\%03o", c)
656 }
657 }
658 if err != nil {
659 return err
660 }
661 }
662 return w.WriteByte('"')
663}
664
665func writeUnknownStruct(w *textWriter, data []byte) (err error) {
666 if !w.compact {
667 if _, err := fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data)); err != nil {
668 return err
669 }
670 }
671 b := NewBuffer(data)
672 for b.index < len(b.buf) {
673 x, err := b.DecodeVarint()
674 if err != nil {
675 _, ferr := fmt.Fprintf(w, "/* %v */\n", err)
676 return ferr
677 }
678 wire, tag := x&7, x>>3
679 if wire == WireEndGroup {
680 w.unindent()
681 if _, werr := w.Write(endBraceNewline); werr != nil {
682 return werr
683 }
684 continue
685 }
686 if _, ferr := fmt.Fprint(w, tag); ferr != nil {
687 return ferr
688 }
689 if wire != WireStartGroup {
690 if err = w.WriteByte(':'); err != nil {
691 return err
692 }
693 }
694 if !w.compact || wire == WireStartGroup {
695 if err = w.WriteByte(' '); err != nil {
696 return err
697 }
698 }
699 switch wire {
700 case WireBytes:
701 buf, e := b.DecodeRawBytes(false)
702 if e == nil {
703 _, err = fmt.Fprintf(w, "%q", buf)
704 } else {
705 _, err = fmt.Fprintf(w, "/* %v */", e)
706 }
707 case WireFixed32:
708 x, err = b.DecodeFixed32()
709 err = writeUnknownInt(w, x, err)
710 case WireFixed64:
711 x, err = b.DecodeFixed64()
712 err = writeUnknownInt(w, x, err)
713 case WireStartGroup:
714 err = w.WriteByte('{')
715 w.indent()
716 case WireVarint:
717 x, err = b.DecodeVarint()
718 err = writeUnknownInt(w, x, err)
719 default:
720 _, err = fmt.Fprintf(w, "/* unknown wire type %d */", wire)
721 }
722 if err != nil {
723 return err
724 }
725 if err := w.WriteByte('\n'); err != nil {
726 return err
727 }
728 }
729 return nil
730}
731
732func writeUnknownInt(w *textWriter, x uint64, err error) error {
733 if err == nil {
734 _, err = fmt.Fprint(w, x)
735 } else {
736 _, err = fmt.Fprintf(w, "/* %v */", err)
737 }
738 return err
739}
740
741type int32Slice []int32
742
743func (s int32Slice) Len() int { return len(s) }
744func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
745func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
746
747// writeExtensions writes all the extensions in pv.
748// pv is assumed to be a pointer to a protocol message struct that is extendable.
749func (tm *TextMarshaler) writeExtensions(w *textWriter, pv reflect.Value) error {
750 emap := extensionMaps[pv.Type().Elem()]
751 e := pv.Interface().(Message)
752
753 var m map[int32]Extension
754 var mu sync.Locker
755 if em, ok := e.(extensionsBytes); ok {
756 eb := em.GetExtensions()
757 var err error
758 m, err = BytesToExtensionsMap(*eb)
759 if err != nil {
760 return err
761 }
762 mu = notLocker{}
763 } else if _, ok := e.(extendableProto); ok {
764 ep, _ := extendable(e)
765 m, mu = ep.extensionsRead()
766 if m == nil {
767 return nil
768 }
769 }
770
771 // Order the extensions by ID.
772 // This isn't strictly necessary, but it will give us
773 // canonical output, which will also make testing easier.
774
775 mu.Lock()
776 ids := make([]int32, 0, len(m))
777 for id := range m {
778 ids = append(ids, id)
779 }
780 sort.Sort(int32Slice(ids))
781 mu.Unlock()
782
783 for _, extNum := range ids {
784 ext := m[extNum]
785 var desc *ExtensionDesc
786 if emap != nil {
787 desc = emap[extNum]
788 }
789 if desc == nil {
790 // Unknown extension.
791 if err := writeUnknownStruct(w, ext.enc); err != nil {
792 return err
793 }
794 continue
795 }
796
797 pb, err := GetExtension(e, desc)
798 if err != nil {
799 return fmt.Errorf("failed getting extension: %v", err)
800 }
801
802 // Repeated extensions will appear as a slice.
803 if !desc.repeated() {
804 if err := tm.writeExtension(w, desc.Name, pb); err != nil {
805 return err
806 }
807 } else {
808 v := reflect.ValueOf(pb)
809 for i := 0; i < v.Len(); i++ {
810 if err := tm.writeExtension(w, desc.Name, v.Index(i).Interface()); err != nil {
811 return err
812 }
813 }
814 }
815 }
816 return nil
817}
818
819func (tm *TextMarshaler) writeExtension(w *textWriter, name string, pb interface{}) error {
820 if _, err := fmt.Fprintf(w, "[%s]:", name); err != nil {
821 return err
822 }
823 if !w.compact {
824 if err := w.WriteByte(' '); err != nil {
825 return err
826 }
827 }
828 if err := tm.writeAny(w, reflect.ValueOf(pb), nil); err != nil {
829 return err
830 }
831 if err := w.WriteByte('\n'); err != nil {
832 return err
833 }
834 return nil
835}
836
837func (w *textWriter) writeIndent() {
838 if !w.complete {
839 return
840 }
841 remain := w.ind * 2
842 for remain > 0 {
843 n := remain
844 if n > len(spaces) {
845 n = len(spaces)
846 }
847 w.w.Write(spaces[:n])
848 remain -= n
849 }
850 w.complete = false
851}
852
853// TextMarshaler is a configurable text format marshaler.
854type TextMarshaler struct {
855 Compact bool // use compact text format (one line).
856 ExpandAny bool // expand google.protobuf.Any messages of known types
857}
858
859// Marshal writes a given protocol buffer in text format.
860// The only errors returned are from w.
861func (tm *TextMarshaler) Marshal(w io.Writer, pb Message) error {
862 val := reflect.ValueOf(pb)
863 if pb == nil || val.IsNil() {
864 w.Write([]byte("<nil>"))
865 return nil
866 }
867 var bw *bufio.Writer
868 ww, ok := w.(writer)
869 if !ok {
870 bw = bufio.NewWriter(w)
871 ww = bw
872 }
873 aw := &textWriter{
874 w: ww,
875 complete: true,
876 compact: tm.Compact,
877 }
878
879 if etm, ok := pb.(encoding.TextMarshaler); ok {
880 text, err := etm.MarshalText()
881 if err != nil {
882 return err
883 }
884 if _, err = aw.Write(text); err != nil {
885 return err
886 }
887 if bw != nil {
888 return bw.Flush()
889 }
890 return nil
891 }
892 // Dereference the received pointer so we don't have outer < and >.
893 v := reflect.Indirect(val)
894 if err := tm.writeStruct(aw, v); err != nil {
895 return err
896 }
897 if bw != nil {
898 return bw.Flush()
899 }
900 return nil
901}
902
903// Text is the same as Marshal, but returns the string directly.
904func (tm *TextMarshaler) Text(pb Message) string {
905 var buf bytes.Buffer
906 tm.Marshal(&buf, pb)
907 return buf.String()
908}
909
910var (
911 defaultTextMarshaler = TextMarshaler{}
912 compactTextMarshaler = TextMarshaler{Compact: true}
913)
914
915// TODO: consider removing some of the Marshal functions below.
916
917// MarshalText writes a given protocol buffer in text format.
918// The only errors returned are from w.
919func MarshalText(w io.Writer, pb Message) error { return defaultTextMarshaler.Marshal(w, pb) }
920
921// MarshalTextString is the same as MarshalText, but returns the string directly.
922func MarshalTextString(pb Message) string { return defaultTextMarshaler.Text(pb) }
923
924// CompactText writes a given protocol buffer in compact text format (one line).
925func CompactText(w io.Writer, pb Message) error { return compactTextMarshaler.Marshal(w, pb) }
926
927// CompactTextString is the same as CompactText, but returns the string directly.
928func CompactTextString(pb Message) string { return compactTextMarshaler.Text(pb) }