text.go 16 KB

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