text.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. var textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
  449. // writeAny writes an arbitrary field.
  450. func (tm *TextMarshaler) writeAny(w *textWriter, v reflect.Value, props *Properties) error {
  451. v = reflect.Indirect(v)
  452. if props != nil {
  453. if len(props.CustomType) > 0 {
  454. custom, ok := v.Interface().(Marshaler)
  455. if ok {
  456. data, err := custom.Marshal()
  457. if err != nil {
  458. return err
  459. }
  460. if err := writeString(w, string(data)); err != nil {
  461. return err
  462. }
  463. return nil
  464. }
  465. } else if len(props.CastType) > 0 {
  466. if _, ok := v.Interface().(interface {
  467. String() string
  468. }); ok {
  469. switch v.Kind() {
  470. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  471. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  472. _, err := fmt.Fprintf(w, "%d", v.Interface())
  473. return err
  474. }
  475. }
  476. } else if props.StdTime {
  477. t, ok := v.Interface().(time.Time)
  478. if !ok {
  479. return fmt.Errorf("stdtime is not time.Time, but %T", v.Interface())
  480. }
  481. tproto, err := timestampProto(t)
  482. if err != nil {
  483. return err
  484. }
  485. propsCopy := *props // Make a copy so that this is goroutine-safe
  486. propsCopy.StdTime = false
  487. err = tm.writeAny(w, reflect.ValueOf(tproto), &propsCopy)
  488. return err
  489. } else if props.StdDuration {
  490. d, ok := v.Interface().(time.Duration)
  491. if !ok {
  492. return fmt.Errorf("stdtime is not time.Duration, but %T", v.Interface())
  493. }
  494. dproto := durationProto(d)
  495. propsCopy := *props // Make a copy so that this is goroutine-safe
  496. propsCopy.StdDuration = false
  497. err := tm.writeAny(w, reflect.ValueOf(dproto), &propsCopy)
  498. return err
  499. }
  500. }
  501. // Floats have special cases.
  502. if v.Kind() == reflect.Float32 || v.Kind() == reflect.Float64 {
  503. x := v.Float()
  504. var b []byte
  505. switch {
  506. case math.IsInf(x, 1):
  507. b = posInf
  508. case math.IsInf(x, -1):
  509. b = negInf
  510. case math.IsNaN(x):
  511. b = nan
  512. }
  513. if b != nil {
  514. _, err := w.Write(b)
  515. return err
  516. }
  517. // Other values are handled below.
  518. }
  519. // We don't attempt to serialise every possible value type; only those
  520. // that can occur in protocol buffers.
  521. switch v.Kind() {
  522. case reflect.Slice:
  523. // Should only be a []byte; repeated fields are handled in writeStruct.
  524. if err := writeString(w, string(v.Bytes())); err != nil {
  525. return err
  526. }
  527. case reflect.String:
  528. if err := writeString(w, v.String()); err != nil {
  529. return err
  530. }
  531. case reflect.Struct:
  532. // Required/optional group/message.
  533. var bra, ket byte = '<', '>'
  534. if props != nil && props.Wire == "group" {
  535. bra, ket = '{', '}'
  536. }
  537. if err := w.WriteByte(bra); err != nil {
  538. return err
  539. }
  540. if !w.compact {
  541. if err := w.WriteByte('\n'); err != nil {
  542. return err
  543. }
  544. }
  545. w.indent()
  546. if v.CanAddr() {
  547. // Calling v.Interface on a struct causes the reflect package to
  548. // copy the entire struct. This is racy with the new Marshaler
  549. // since we atomically update the XXX_sizecache.
  550. //
  551. // Thus, we retrieve a pointer to the struct if possible to avoid
  552. // a race since v.Interface on the pointer doesn't copy the struct.
  553. //
  554. // If v is not addressable, then we are not worried about a race
  555. // since it implies that the binary Marshaler cannot possibly be
  556. // mutating this value.
  557. v = v.Addr()
  558. }
  559. if v.Type().Implements(textMarshalerType) {
  560. text, err := v.Interface().(encoding.TextMarshaler).MarshalText()
  561. if err != nil {
  562. return err
  563. }
  564. if _, err = w.Write(text); err != nil {
  565. return err
  566. }
  567. } else {
  568. if v.Kind() == reflect.Ptr {
  569. v = v.Elem()
  570. }
  571. if err := tm.writeStruct(w, v); err != nil {
  572. return err
  573. }
  574. }
  575. w.unindent()
  576. if err := w.WriteByte(ket); err != nil {
  577. return err
  578. }
  579. default:
  580. _, err := fmt.Fprint(w, v.Interface())
  581. return err
  582. }
  583. return nil
  584. }
  585. // equivalent to C's isprint.
  586. func isprint(c byte) bool {
  587. return c >= 0x20 && c < 0x7f
  588. }
  589. // writeString writes a string in the protocol buffer text format.
  590. // It is similar to strconv.Quote except we don't use Go escape sequences,
  591. // we treat the string as a byte sequence, and we use octal escapes.
  592. // These differences are to maintain interoperability with the other
  593. // languages' implementations of the text format.
  594. func writeString(w *textWriter, s string) error {
  595. // use WriteByte here to get any needed indent
  596. if err := w.WriteByte('"'); err != nil {
  597. return err
  598. }
  599. // Loop over the bytes, not the runes.
  600. for i := 0; i < len(s); i++ {
  601. var err error
  602. // Divergence from C++: we don't escape apostrophes.
  603. // There's no need to escape them, and the C++ parser
  604. // copes with a naked apostrophe.
  605. switch c := s[i]; c {
  606. case '\n':
  607. _, err = w.w.Write(backslashN)
  608. case '\r':
  609. _, err = w.w.Write(backslashR)
  610. case '\t':
  611. _, err = w.w.Write(backslashT)
  612. case '"':
  613. _, err = w.w.Write(backslashDQ)
  614. case '\\':
  615. _, err = w.w.Write(backslashBS)
  616. default:
  617. if isprint(c) {
  618. err = w.w.WriteByte(c)
  619. } else {
  620. _, err = fmt.Fprintf(w.w, "\\%03o", c)
  621. }
  622. }
  623. if err != nil {
  624. return err
  625. }
  626. }
  627. return w.WriteByte('"')
  628. }
  629. func writeUnknownStruct(w *textWriter, data []byte) (err error) {
  630. if !w.compact {
  631. if _, err := fmt.Fprintf(w, "/* %d unknown bytes */\n", len(data)); err != nil {
  632. return err
  633. }
  634. }
  635. b := NewBuffer(data)
  636. for b.index < len(b.buf) {
  637. x, err := b.DecodeVarint()
  638. if err != nil {
  639. _, ferr := fmt.Fprintf(w, "/* %v */\n", err)
  640. return ferr
  641. }
  642. wire, tag := x&7, x>>3
  643. if wire == WireEndGroup {
  644. w.unindent()
  645. if _, werr := w.Write(endBraceNewline); werr != nil {
  646. return werr
  647. }
  648. continue
  649. }
  650. if _, ferr := fmt.Fprint(w, tag); ferr != nil {
  651. return ferr
  652. }
  653. if wire != WireStartGroup {
  654. if err = w.WriteByte(':'); err != nil {
  655. return err
  656. }
  657. }
  658. if !w.compact || wire == WireStartGroup {
  659. if err = w.WriteByte(' '); err != nil {
  660. return err
  661. }
  662. }
  663. switch wire {
  664. case WireBytes:
  665. buf, e := b.DecodeRawBytes(false)
  666. if e == nil {
  667. _, err = fmt.Fprintf(w, "%q", buf)
  668. } else {
  669. _, err = fmt.Fprintf(w, "/* %v */", e)
  670. }
  671. case WireFixed32:
  672. x, err = b.DecodeFixed32()
  673. err = writeUnknownInt(w, x, err)
  674. case WireFixed64:
  675. x, err = b.DecodeFixed64()
  676. err = writeUnknownInt(w, x, err)
  677. case WireStartGroup:
  678. err = w.WriteByte('{')
  679. w.indent()
  680. case WireVarint:
  681. x, err = b.DecodeVarint()
  682. err = writeUnknownInt(w, x, err)
  683. default:
  684. _, err = fmt.Fprintf(w, "/* unknown wire type %d */", wire)
  685. }
  686. if err != nil {
  687. return err
  688. }
  689. if err := w.WriteByte('\n'); err != nil {
  690. return err
  691. }
  692. }
  693. return nil
  694. }
  695. func writeUnknownInt(w *textWriter, x uint64, err error) error {
  696. if err == nil {
  697. _, err = fmt.Fprint(w, x)
  698. } else {
  699. _, err = fmt.Fprintf(w, "/* %v */", err)
  700. }
  701. return err
  702. }
  703. type int32Slice []int32
  704. func (s int32Slice) Len() int { return len(s) }
  705. func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
  706. func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  707. // writeExtensions writes all the extensions in pv.
  708. // pv is assumed to be a pointer to a protocol message struct that is extendable.
  709. func (tm *TextMarshaler) writeExtensions(w *textWriter, pv reflect.Value) error {
  710. emap := extensionMaps[pv.Type().Elem()]
  711. e := pv.Interface().(Message)
  712. var m map[int32]Extension
  713. var mu sync.Locker
  714. if em, ok := e.(extensionsBytes); ok {
  715. eb := em.GetExtensions()
  716. var err error
  717. m, err = BytesToExtensionsMap(*eb)
  718. if err != nil {
  719. return err
  720. }
  721. mu = notLocker{}
  722. } else if _, ok := e.(extendableProto); ok {
  723. ep, _ := extendable(e)
  724. m, mu = ep.extensionsRead()
  725. if m == nil {
  726. return nil
  727. }
  728. }
  729. // Order the extensions by ID.
  730. // This isn't strictly necessary, but it will give us
  731. // canonical output, which will also make testing easier.
  732. mu.Lock()
  733. ids := make([]int32, 0, len(m))
  734. for id := range m {
  735. ids = append(ids, id)
  736. }
  737. sort.Sort(int32Slice(ids))
  738. mu.Unlock()
  739. for _, extNum := range ids {
  740. ext := m[extNum]
  741. var desc *ExtensionDesc
  742. if emap != nil {
  743. desc = emap[extNum]
  744. }
  745. if desc == nil {
  746. // Unknown extension.
  747. if err := writeUnknownStruct(w, ext.enc); err != nil {
  748. return err
  749. }
  750. continue
  751. }
  752. pb, err := GetExtension(e, desc)
  753. if err != nil {
  754. return fmt.Errorf("failed getting extension: %v", err)
  755. }
  756. // Repeated extensions will appear as a slice.
  757. if !desc.repeated() {
  758. if err := tm.writeExtension(w, desc.Name, pb); err != nil {
  759. return err
  760. }
  761. } else {
  762. v := reflect.ValueOf(pb)
  763. for i := 0; i < v.Len(); i++ {
  764. if err := tm.writeExtension(w, desc.Name, v.Index(i).Interface()); err != nil {
  765. return err
  766. }
  767. }
  768. }
  769. }
  770. return nil
  771. }
  772. func (tm *TextMarshaler) writeExtension(w *textWriter, name string, pb interface{}) error {
  773. if _, err := fmt.Fprintf(w, "[%s]:", name); err != nil {
  774. return err
  775. }
  776. if !w.compact {
  777. if err := w.WriteByte(' '); err != nil {
  778. return err
  779. }
  780. }
  781. if err := tm.writeAny(w, reflect.ValueOf(pb), nil); err != nil {
  782. return err
  783. }
  784. if err := w.WriteByte('\n'); err != nil {
  785. return err
  786. }
  787. return nil
  788. }
  789. func (w *textWriter) writeIndent() {
  790. if !w.complete {
  791. return
  792. }
  793. remain := w.ind * 2
  794. for remain > 0 {
  795. n := remain
  796. if n > len(spaces) {
  797. n = len(spaces)
  798. }
  799. w.w.Write(spaces[:n])
  800. remain -= n
  801. }
  802. w.complete = false
  803. }
  804. // TextMarshaler is a configurable text format marshaler.
  805. type TextMarshaler struct {
  806. Compact bool // use compact text format (one line).
  807. ExpandAny bool // expand google.protobuf.Any messages of known types
  808. }
  809. // Marshal writes a given protocol buffer in text format.
  810. // The only errors returned are from w.
  811. func (tm *TextMarshaler) Marshal(w io.Writer, pb Message) error {
  812. val := reflect.ValueOf(pb)
  813. if pb == nil || val.IsNil() {
  814. w.Write([]byte("<nil>"))
  815. return nil
  816. }
  817. var bw *bufio.Writer
  818. ww, ok := w.(writer)
  819. if !ok {
  820. bw = bufio.NewWriter(w)
  821. ww = bw
  822. }
  823. aw := &textWriter{
  824. w: ww,
  825. complete: true,
  826. compact: tm.Compact,
  827. }
  828. if etm, ok := pb.(encoding.TextMarshaler); ok {
  829. text, err := etm.MarshalText()
  830. if err != nil {
  831. return err
  832. }
  833. if _, err = aw.Write(text); err != nil {
  834. return err
  835. }
  836. if bw != nil {
  837. return bw.Flush()
  838. }
  839. return nil
  840. }
  841. // Dereference the received pointer so we don't have outer < and >.
  842. v := reflect.Indirect(val)
  843. if err := tm.writeStruct(aw, v); err != nil {
  844. return err
  845. }
  846. if bw != nil {
  847. return bw.Flush()
  848. }
  849. return nil
  850. }
  851. // Text is the same as Marshal, but returns the string directly.
  852. func (tm *TextMarshaler) Text(pb Message) string {
  853. var buf bytes.Buffer
  854. tm.Marshal(&buf, pb)
  855. return buf.String()
  856. }
  857. var (
  858. defaultTextMarshaler = TextMarshaler{}
  859. compactTextMarshaler = TextMarshaler{Compact: true}
  860. )
  861. // TODO: consider removing some of the Marshal functions below.
  862. // MarshalText writes a given protocol buffer in text format.
  863. // The only errors returned are from w.
  864. func MarshalText(w io.Writer, pb Message) error { return defaultTextMarshaler.Marshal(w, pb) }
  865. // MarshalTextString is the same as MarshalText, but returns the string directly.
  866. func MarshalTextString(pb Message) string { return defaultTextMarshaler.Text(pb) }
  867. // CompactText writes a given protocol buffer in compact text format (one line).
  868. func CompactText(w io.Writer, pb Message) error { return compactTextMarshaler.Marshal(w, pb) }
  869. // CompactTextString is the same as CompactText, but returns the string directly.
  870. func CompactTextString(pb Message) string { return compactTextMarshaler.Text(pb) }