You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

text.go 21KB

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