text.go 23 KB

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