text.go 23 KB

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