text.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. // Extensions for Protocol Buffers to create more go like structures.
  2. //
  3. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  4. // http://code.google.com/p/gogoprotobuf/gogoproto
  5. //
  6. // Go support for Protocol Buffers - Google's data interchange format
  7. //
  8. // Copyright 2010 The Go Authors. All rights reserved.
  9. // http://code.google.com/p/goprotobuf/
  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. "fmt"
  42. "io"
  43. "log"
  44. "math"
  45. "os"
  46. "reflect"
  47. "sort"
  48. "strings"
  49. )
  50. var (
  51. newline = []byte("\n")
  52. spaces = []byte(" ")
  53. gtNewline = []byte(">\n")
  54. endBraceNewline = []byte("}\n")
  55. backslashN = []byte{'\\', 'n'}
  56. backslashR = []byte{'\\', 'r'}
  57. backslashT = []byte{'\\', 't'}
  58. backslashDQ = []byte{'\\', '"'}
  59. backslashBS = []byte{'\\', '\\'}
  60. posInf = []byte("inf")
  61. negInf = []byte("-inf")
  62. nan = []byte("nan")
  63. )
  64. type writer interface {
  65. io.Writer
  66. WriteByte(byte) error
  67. }
  68. // textWriter is an io.Writer that tracks its indentation level.
  69. type textWriter struct {
  70. ind int
  71. complete bool // if the current position is a complete line
  72. compact bool // whether to write out as a one-liner
  73. w writer
  74. }
  75. func (w *textWriter) WriteString(s string) (n int, err error) {
  76. if !strings.Contains(s, "\n") {
  77. if !w.compact && w.complete {
  78. w.writeIndent()
  79. }
  80. w.complete = false
  81. return io.WriteString(w.w, s)
  82. }
  83. // WriteString is typically called without newlines, so this
  84. // codepath and its copy are rare. We copy to avoid
  85. // duplicating all of Write's logic here.
  86. return w.Write([]byte(s))
  87. }
  88. func (w *textWriter) Write(p []byte) (n int, err error) {
  89. newlines := bytes.Count(p, newline)
  90. if newlines == 0 {
  91. if !w.compact && w.complete {
  92. w.writeIndent()
  93. }
  94. n, err = w.w.Write(p)
  95. w.complete = false
  96. return n, err
  97. }
  98. frags := bytes.SplitN(p, newline, newlines+1)
  99. if w.compact {
  100. for i, frag := range frags {
  101. if i > 0 {
  102. if err := w.w.WriteByte(' '); err != nil {
  103. return n, err
  104. }
  105. n++
  106. }
  107. nn, err := w.w.Write(frag)
  108. n += nn
  109. if err != nil {
  110. return n, err
  111. }
  112. }
  113. return n, nil
  114. }
  115. for i, frag := range frags {
  116. if w.complete {
  117. w.writeIndent()
  118. }
  119. nn, err := w.w.Write(frag)
  120. n += nn
  121. if err != nil {
  122. return n, err
  123. }
  124. if i+1 < len(frags) {
  125. if err := w.w.WriteByte('\n'); err != nil {
  126. return n, err
  127. }
  128. n++
  129. }
  130. }
  131. w.complete = len(frags[len(frags)-1]) == 0
  132. return n, nil
  133. }
  134. func (w *textWriter) WriteByte(c byte) error {
  135. if w.compact && c == '\n' {
  136. c = ' '
  137. }
  138. if !w.compact && w.complete {
  139. w.writeIndent()
  140. }
  141. err := w.w.WriteByte(c)
  142. w.complete = c == '\n'
  143. return err
  144. }
  145. func (w *textWriter) indent() { w.ind++ }
  146. func (w *textWriter) unindent() {
  147. if w.ind == 0 {
  148. log.Printf("proto: textWriter unindented too far")
  149. return
  150. }
  151. w.ind--
  152. }
  153. func writeName(w *textWriter, props *Properties) error {
  154. if _, err := w.WriteString(props.OrigName); err != nil {
  155. return err
  156. }
  157. if props.Wire != "group" {
  158. return w.WriteByte(':')
  159. }
  160. return nil
  161. }
  162. var (
  163. messageSetType = reflect.TypeOf((*MessageSet)(nil)).Elem()
  164. )
  165. // raw is the interface satisfied by RawMessage.
  166. type raw interface {
  167. Bytes() []byte
  168. }
  169. func writeStruct(w *textWriter, sv reflect.Value) error {
  170. if sv.Type() == messageSetType {
  171. return writeMessageSet(w, sv.Addr().Interface().(*MessageSet))
  172. }
  173. st := sv.Type()
  174. sprops := GetProperties(st)
  175. for i := 0; i < sv.NumField(); i++ {
  176. fv := sv.Field(i)
  177. props := sprops.Prop[i]
  178. name := st.Field(i).Name
  179. if strings.HasPrefix(name, "XXX_") {
  180. // There are two XXX_ fields:
  181. // XXX_unrecognized []byte
  182. // XXX_extensions map[int32]proto.Extension
  183. // The first is handled here;
  184. // the second is handled at the bottom of this function.
  185. if name == "XXX_unrecognized" && !fv.IsNil() {
  186. if err := writeUnknownStruct(w, fv.Interface().([]byte)); err != nil {
  187. return err
  188. }
  189. }
  190. continue
  191. }
  192. if fv.Kind() == reflect.Ptr && fv.IsNil() {
  193. // Field not filled in. This could be an optional field or
  194. // a required field that wasn't filled in. Either way, there
  195. // isn't anything we can show for it.
  196. continue
  197. }
  198. if fv.Kind() == reflect.Slice && fv.IsNil() {
  199. // Repeated field that is empty, or a bytes field that is unused.
  200. continue
  201. }
  202. if props.Repeated && fv.Kind() == reflect.Slice {
  203. // Repeated field.
  204. for j := 0; j < fv.Len(); j++ {
  205. if err := writeName(w, props); err != nil {
  206. return err
  207. }
  208. if !w.compact {
  209. if err := w.WriteByte(' '); err != nil {
  210. return err
  211. }
  212. }
  213. if len(props.Enum) > 0 {
  214. if err := writeEnum(w, fv.Index(j), props); err != nil {
  215. return err
  216. }
  217. } else if err := writeAny(w, fv.Index(j), props); err != nil {
  218. return err
  219. }
  220. if err := w.WriteByte('\n'); err != nil {
  221. return err
  222. }
  223. }
  224. continue
  225. }
  226. if err := writeName(w, props); err != nil {
  227. return err
  228. }
  229. if !w.compact {
  230. if err := w.WriteByte(' '); err != nil {
  231. return err
  232. }
  233. }
  234. if b, ok := fv.Interface().(raw); ok {
  235. if err := writeRaw(w, b.Bytes()); err != nil {
  236. return err
  237. }
  238. continue
  239. }
  240. if len(props.Enum) > 0 {
  241. if err := writeEnum(w, fv, props); err != nil {
  242. return err
  243. }
  244. } else if err := writeAny(w, fv, props); err != nil {
  245. return err
  246. }
  247. if err := w.WriteByte('\n'); err != nil {
  248. return err
  249. }
  250. }
  251. // Extensions (the XXX_extensions field).
  252. pv := sv.Addr()
  253. if pv.Type().Implements(extendableProtoType) {
  254. if err := writeExtensions(w, pv); err != nil {
  255. return err
  256. }
  257. }
  258. return nil
  259. }
  260. // writeRaw writes an uninterpreted raw message.
  261. func writeRaw(w *textWriter, b []byte) error {
  262. if err := w.WriteByte('<'); err != nil {
  263. return err
  264. }
  265. if !w.compact {
  266. if err := w.WriteByte('\n'); err != nil {
  267. return err
  268. }
  269. }
  270. w.indent()
  271. if err := writeUnknownStruct(w, b); err != nil {
  272. return err
  273. }
  274. w.unindent()
  275. if err := w.WriteByte('>'); err != nil {
  276. return err
  277. }
  278. return nil
  279. }
  280. // writeAny writes an arbitrary field.
  281. func writeAny(w *textWriter, v reflect.Value, props *Properties) error {
  282. v = reflect.Indirect(v)
  283. if props != nil && len(props.CustomType) > 0 {
  284. var custom Marshaler = v.Interface().(Marshaler)
  285. data, err := custom.Marshal()
  286. if err != nil {
  287. return err
  288. }
  289. if err := writeString(w, string(data)); err != nil {
  290. return err
  291. }
  292. return nil
  293. }
  294. // Floats have special cases.
  295. if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
  296. x := v.Float()
  297. var b []byte
  298. switch {
  299. case math.IsInf(x, 1):
  300. b = posInf
  301. case math.IsInf(x, -1):
  302. b = negInf
  303. case math.IsNaN(x):
  304. b = nan
  305. }
  306. if b != nil {
  307. _, err := w.Write(b)
  308. return err
  309. }
  310. // Other values are handled below.
  311. }
  312. // We don't attempt to serialise every possible value type; only those
  313. // that can occur in protocol buffers.
  314. switch v.Kind() {
  315. case reflect.Slice:
  316. // Should only be a []byte; repeated fields are handled in writeStruct.
  317. if err := writeString(w, string(v.Interface().([]byte))); err != nil {
  318. return err
  319. }
  320. case reflect.String:
  321. if err := writeString(w, v.String()); err != nil {
  322. return err
  323. }
  324. case reflect.Struct:
  325. // Required/optional group/message.
  326. var bra, ket byte = '<', '>'
  327. if props != nil && props.Wire == "group" {
  328. bra, ket = '{', '}'
  329. }
  330. if err := w.WriteByte(bra); err != nil {
  331. return err
  332. }
  333. if !w.compact {
  334. if err := w.WriteByte('\n'); err != nil {
  335. return err
  336. }
  337. }
  338. w.indent()
  339. if err := writeStruct(w, v); err != nil {
  340. return err
  341. }
  342. w.unindent()
  343. if err := w.WriteByte(ket); err != nil {
  344. return err
  345. }
  346. default:
  347. _, err := fmt.Fprint(w, v.Interface())
  348. return err
  349. }
  350. return nil
  351. }
  352. // equivalent to C's isprint.
  353. func isprint(c byte) bool {
  354. return c >= 0x20 && c < 0x7f
  355. }
  356. // writeString writes a string in the protocol buffer text format.
  357. // It is similar to strconv.Quote except we don't use Go escape sequences,
  358. // we treat the string as a byte sequence, and we use octal escapes.
  359. // These differences are to maintain interoperability with the other
  360. // languages' implementations of the text format.
  361. func writeString(w *textWriter, s string) error {
  362. // use WriteByte here to get any needed indent
  363. if err := w.WriteByte('"'); err != nil {
  364. return err
  365. }
  366. // Loop over the bytes, not the runes.
  367. for i := 0; i < len(s); i++ {
  368. var err error
  369. // Divergence from C++: we don't escape apostrophes.
  370. // There's no need to escape them, and the C++ parser
  371. // copes with a naked apostrophe.
  372. switch c := s[i]; c {
  373. case '\n':
  374. _, err = w.w.Write(backslashN)
  375. case '\r':
  376. _, err = w.w.Write(backslashR)
  377. case '\t':
  378. _, err = w.w.Write(backslashT)
  379. case '"':
  380. _, err = w.w.Write(backslashDQ)
  381. case '\\':
  382. _, err = w.w.Write(backslashBS)
  383. default:
  384. if isprint(c) {
  385. err = w.w.WriteByte(c)
  386. } else {
  387. _, err = fmt.Fprintf(w.w, "\\%03o", c)
  388. }
  389. }
  390. if err != nil {
  391. return err
  392. }
  393. }
  394. return w.WriteByte('"')
  395. }
  396. func writeMessageSet(w *textWriter, ms *MessageSet) error {
  397. for _, item := range ms.Item {
  398. id := *item.TypeId
  399. if msd, ok := messageSetMap[id]; ok {
  400. // Known message set type.
  401. if _, err := fmt.Fprintf(w, "[%s]: <\n", msd.name); err != nil {
  402. return err
  403. }
  404. w.indent()
  405. pb := reflect.New(msd.t.Elem())
  406. if err := Unmarshal(item.Message, pb.Interface().(Message)); err != nil {
  407. if _, err := fmt.Fprintf(w, "/* bad message: %v */\n", err); err != nil {
  408. return err
  409. }
  410. } else {
  411. if err := writeStruct(w, pb.Elem()); err != nil {
  412. return err
  413. }
  414. }
  415. } else {
  416. // Unknown type.
  417. if _, err := fmt.Fprintf(w, "[%d]: <\n", id); err != nil {
  418. return err
  419. }
  420. w.indent()
  421. if err := writeUnknownStruct(w, item.Message); err != nil {
  422. return err
  423. }
  424. }
  425. w.unindent()
  426. if _, err := w.Write(gtNewline); err != nil {
  427. return err
  428. }
  429. }
  430. return nil
  431. }
  432. func writeUnknownStruct(w *textWriter, data []byte) (err error) {
  433. if !w.compact {
  434. if _, err := fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data)); err != nil {
  435. return err
  436. }
  437. }
  438. b := NewBuffer(data)
  439. for b.index < len(b.buf) {
  440. x, err := b.DecodeVarint()
  441. if err != nil {
  442. _, err := fmt.Fprintf(w, "/* %v */\n", err)
  443. return err
  444. }
  445. wire, tag := x&7, x>>3
  446. if wire == WireEndGroup {
  447. w.unindent()
  448. if _, err := w.Write(endBraceNewline); err != nil {
  449. return err
  450. }
  451. continue
  452. }
  453. if _, err := fmt.Fprint(w, tag); err != nil {
  454. return err
  455. }
  456. if wire != WireStartGroup {
  457. if err := w.WriteByte(':'); err != nil {
  458. return err
  459. }
  460. }
  461. if !w.compact || wire == WireStartGroup {
  462. if err := w.WriteByte(' '); err != nil {
  463. return err
  464. }
  465. }
  466. switch wire {
  467. case WireBytes:
  468. buf, e := b.DecodeRawBytes(false)
  469. if e == nil {
  470. _, err = fmt.Fprintf(w, "%q", buf)
  471. } else {
  472. _, err = fmt.Fprintf(w, "/* %v */", e)
  473. }
  474. case WireFixed32:
  475. x, err = b.DecodeFixed32()
  476. err = writeUnknownInt(w, x, err)
  477. case WireFixed64:
  478. x, err = b.DecodeFixed64()
  479. err = writeUnknownInt(w, x, err)
  480. case WireStartGroup:
  481. err = w.WriteByte('{')
  482. w.indent()
  483. case WireVarint:
  484. x, err = b.DecodeVarint()
  485. err = writeUnknownInt(w, x, err)
  486. default:
  487. _, err = fmt.Fprintf(w, "/* unknown wire type %d */", wire)
  488. }
  489. if err != nil {
  490. return err
  491. }
  492. if err = w.WriteByte('\n'); err != nil {
  493. return err
  494. }
  495. }
  496. return nil
  497. }
  498. func writeUnknownInt(w *textWriter, x uint64, err error) error {
  499. if err == nil {
  500. _, err = fmt.Fprint(w, x)
  501. } else {
  502. _, err = fmt.Fprintf(w, "/* %v */", err)
  503. }
  504. return err
  505. }
  506. type int32Slice []int32
  507. func (s int32Slice) Len() int { return len(s) }
  508. func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
  509. func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  510. // writeExtensions writes all the extensions in pv.
  511. // pv is assumed to be a pointer to a protocol message struct that is extendable.
  512. func writeExtensions(w *textWriter, pv reflect.Value) error {
  513. emap := extensionMaps[pv.Type().Elem()]
  514. ep := pv.Interface().(extendableProto)
  515. // Order the extensions by ID.
  516. // This isn't strictly necessary, but it will give us
  517. // canonical output, which will also make testing easier.
  518. m := ep.ExtensionMap()
  519. ids := make([]int32, 0, len(m))
  520. for id := range m {
  521. ids = append(ids, id)
  522. }
  523. sort.Sort(int32Slice(ids))
  524. for _, extNum := range ids {
  525. ext := m[extNum]
  526. var desc *ExtensionDesc
  527. if emap != nil {
  528. desc = emap[extNum]
  529. }
  530. if desc == nil {
  531. // Unknown extension.
  532. if err := writeUnknownStruct(w, ext.enc); err != nil {
  533. return err
  534. }
  535. continue
  536. }
  537. pb, err := GetExtension(ep, desc)
  538. if err != nil {
  539. if _, err := fmt.Fprintln(os.Stderr, "proto: failed getting extension: ", err); err != nil {
  540. return err
  541. }
  542. continue
  543. }
  544. // Repeated extensions will appear as a slice.
  545. if !desc.repeated() {
  546. if err := writeExtension(w, desc.Name, pb); err != nil {
  547. return err
  548. }
  549. } else {
  550. v := reflect.ValueOf(pb)
  551. for i := 0; i < v.Len(); i++ {
  552. if err := writeExtension(w, desc.Name, v.Index(i).Interface()); err != nil {
  553. return err
  554. }
  555. }
  556. }
  557. }
  558. return nil
  559. }
  560. func writeExtension(w *textWriter, name string, pb interface{}) error {
  561. if _, err := fmt.Fprintf(w, "[%s]:", name); err != nil {
  562. return err
  563. }
  564. if !w.compact {
  565. if err := w.WriteByte(' '); err != nil {
  566. return err
  567. }
  568. }
  569. if err := writeAny(w, reflect.ValueOf(pb), nil); err != nil {
  570. return err
  571. }
  572. if err := w.WriteByte('\n'); err != nil {
  573. return err
  574. }
  575. return nil
  576. }
  577. func (w *textWriter) writeIndent() {
  578. if !w.complete {
  579. return
  580. }
  581. remain := w.ind * 2
  582. for remain > 0 {
  583. n := remain
  584. if n > len(spaces) {
  585. n = len(spaces)
  586. }
  587. w.w.Write(spaces[:n])
  588. remain -= n
  589. }
  590. w.complete = false
  591. }
  592. func marshalText(w io.Writer, pb Message, compact bool) error {
  593. val := reflect.ValueOf(pb)
  594. if pb == nil || val.IsNil() {
  595. w.Write([]byte("<nil>"))
  596. return nil
  597. }
  598. var bw *bufio.Writer
  599. ww, ok := w.(writer)
  600. if !ok {
  601. bw = bufio.NewWriter(w)
  602. ww = bw
  603. }
  604. aw := &textWriter{
  605. w: ww,
  606. complete: true,
  607. compact: compact,
  608. }
  609. // Dereference the received pointer so we don't have outer < and >.
  610. v := reflect.Indirect(val)
  611. if err := writeStruct(aw, v); err != nil {
  612. return err
  613. }
  614. if bw != nil {
  615. return bw.Flush()
  616. }
  617. return nil
  618. }
  619. // MarshalText writes a given protocol buffer in text format.
  620. // The only errors returned are from w.
  621. func MarshalText(w io.Writer, pb Message) error { return marshalText(w, pb, false) }
  622. // MarshalTextString is the same as MarshalText, but returns the string directly.
  623. func MarshalTextString(pb Message) string {
  624. var buf bytes.Buffer
  625. marshalText(&buf, pb, false)
  626. return buf.String()
  627. }
  628. // CompactText writes a given protocol buffer in compact text format (one line).
  629. func CompactText(w io.Writer, pb Message) error { return marshalText(w, pb, true) }
  630. // CompactTextString is the same as CompactText, but returns the string directly.
  631. func CompactTextString(pb Message) string {
  632. var buf bytes.Buffer
  633. marshalText(&buf, pb, true)
  634. return buf.String()
  635. }