pack.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package pack enables manual encoding and decoding of protobuf wire data.
  5. //
  6. // This package is intended for use in debugging and/or creation of test data.
  7. // Proper usage of this package requires knowledge of the wire format.
  8. //
  9. // See https://developers.google.com/protocol-buffers/docs/encoding.
  10. package pack
  11. import (
  12. "fmt"
  13. "io"
  14. "math"
  15. "path"
  16. "reflect"
  17. "strconv"
  18. "strings"
  19. "unicode"
  20. "unicode/utf8"
  21. "google.golang.org/proto/internal/encoding/wire"
  22. "google.golang.org/proto/reflect/protoreflect"
  23. )
  24. // Number is the field number; aliased from the wire package for convenience.
  25. type Number = wire.Number
  26. // Number type constants; copied from the wire package for convenience.
  27. const (
  28. MinValidNumber Number = wire.MinValidNumber
  29. FirstReservedNumber Number = wire.FirstReservedNumber
  30. LastReservedNumber Number = wire.LastReservedNumber
  31. MaxValidNumber Number = wire.MaxValidNumber
  32. )
  33. // Type is the wire type; aliased from the wire package for convenience.
  34. type Type = wire.Type
  35. // Wire type constants; copied from the wire package for convenience.
  36. const (
  37. VarintType Type = wire.VarintType
  38. Fixed32Type Type = wire.Fixed32Type
  39. Fixed64Type Type = wire.Fixed64Type
  40. BytesType Type = wire.BytesType
  41. StartGroupType Type = wire.StartGroupType
  42. EndGroupType Type = wire.EndGroupType
  43. )
  44. type (
  45. // Token is any other type (e.g., Message, Tag, Varint, Float32, etc).
  46. Token token
  47. // Message is a ordered sequence Tokens.
  48. Message []Token
  49. // Tag is a tuple of the field number and the wire type.
  50. Tag struct {
  51. Number Number
  52. Type Type
  53. }
  54. // Bool is a boolean.
  55. Bool bool
  56. // Varint is a signed varint using 64-bit two's complement encoding.
  57. Varint int64
  58. // Svarint is a signed varint using zig-zag encoding.
  59. Svarint int64
  60. // Uvarint is a unsigned varint.
  61. Uvarint uint64
  62. // Int32 is a signed 32-bit fixed-width integer.
  63. Int32 int32
  64. // Uint32 is an unsigned 32-bit fixed-width integer.
  65. Uint32 uint32
  66. // Float32 is a 32-bit fixed-width floating point number.
  67. Float32 float32
  68. // Int64 is a signed 64-bit fixed-width integer.
  69. Int64 int64
  70. // Uint64 is an unsigned 64-bit fixed-width integer.
  71. Uint64 uint64
  72. // Float64 is a 64-bit fixed-width floating point number.
  73. Float64 float64
  74. // String is a length-prefixed string.
  75. String string
  76. // Bytes is a length-prefixed bytes.
  77. Bytes []byte
  78. // LengthPrefix is a length-prefixed message.
  79. LengthPrefix Message
  80. // Denormalized is a denormalized varint value, where a varint is encoded
  81. // using more bytes than is strictly necessary. The number of extra bytes
  82. // alone is sufficient to losslessly represent the denormalized varint.
  83. //
  84. // The value may be one of Tag, Bool, Varint, Svarint, or Uvarint,
  85. // where the varint representation of each token is denormalized.
  86. //
  87. // Alternatively, the value may be one of String, Bytes, or LengthPrefix,
  88. // where the varint representation of the length-prefix is denormalized.
  89. Denormalized struct {
  90. Count uint // number of extra bytes
  91. Value Token
  92. }
  93. // Raw are bytes directly appended to output.
  94. Raw []byte
  95. )
  96. type token interface {
  97. isToken()
  98. }
  99. func (Message) isToken() {}
  100. func (Tag) isToken() {}
  101. func (Bool) isToken() {}
  102. func (Varint) isToken() {}
  103. func (Svarint) isToken() {}
  104. func (Uvarint) isToken() {}
  105. func (Int32) isToken() {}
  106. func (Uint32) isToken() {}
  107. func (Float32) isToken() {}
  108. func (Int64) isToken() {}
  109. func (Uint64) isToken() {}
  110. func (Float64) isToken() {}
  111. func (String) isToken() {}
  112. func (Bytes) isToken() {}
  113. func (LengthPrefix) isToken() {}
  114. func (Denormalized) isToken() {}
  115. func (Raw) isToken() {}
  116. // Size reports the size in bytes of the marshaled message.
  117. func (m Message) Size() int {
  118. var n int
  119. for _, v := range m {
  120. switch v := v.(type) {
  121. case Message:
  122. n += v.Size()
  123. case Tag:
  124. n += wire.SizeTag(v.Number)
  125. case Bool:
  126. n += wire.SizeVarint(wire.EncodeBool(false))
  127. case Varint:
  128. n += wire.SizeVarint(uint64(v))
  129. case Svarint:
  130. n += wire.SizeVarint(wire.EncodeZigZag(int64(v)))
  131. case Uvarint:
  132. n += wire.SizeVarint(uint64(v))
  133. case Int32, Uint32, Float32:
  134. n += wire.SizeFixed32()
  135. case Int64, Uint64, Float64:
  136. n += wire.SizeFixed64()
  137. case String:
  138. n += wire.SizeBytes(len(v))
  139. case Bytes:
  140. n += wire.SizeBytes(len(v))
  141. case LengthPrefix:
  142. n += wire.SizeBytes(Message(v).Size())
  143. case Denormalized:
  144. n += int(v.Count) + Message{v.Value}.Size()
  145. case Raw:
  146. n += len(v)
  147. default:
  148. panic(fmt.Sprintf("unknown type: %T", v))
  149. }
  150. }
  151. return n
  152. }
  153. // Message encodes an AST into the protobuf wire format.
  154. //
  155. // Example message definition:
  156. // message MyMessage {
  157. // string field1 = 1;
  158. // int64 field2 = 2;
  159. // repeated float32 field3 = 3;
  160. // }
  161. //
  162. // Example encoded message:
  163. // b := Message{
  164. // Tag{1, BytesType}, String("Hello, world!"),
  165. // Tag{2, VarintType}, Varint(-10),
  166. // Tag{3, BytesType}, LengthPrefix{
  167. // Float32(1.1), Float32(2.2), Float32(3.3),
  168. // },
  169. // }.Marshal()
  170. //
  171. // Resulting wire data:
  172. // 0x0000 0a 0d 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 10 |..Hello, world!.|
  173. // 0x0010 f6 ff ff ff ff ff ff ff ff 01 1a 0c cd cc 8c 3f |...............?|
  174. // 0x0020 cd cc 0c 40 33 33 53 40 |...@33S@|
  175. func (m Message) Marshal() []byte {
  176. var out []byte
  177. for _, v := range m {
  178. switch v := v.(type) {
  179. case Message:
  180. out = append(out, v.Marshal()...)
  181. case Tag:
  182. out = wire.AppendTag(out, v.Number, v.Type)
  183. case Bool:
  184. out = wire.AppendVarint(out, wire.EncodeBool(bool(v)))
  185. case Varint:
  186. out = wire.AppendVarint(out, uint64(v))
  187. case Svarint:
  188. out = wire.AppendVarint(out, wire.EncodeZigZag(int64(v)))
  189. case Uvarint:
  190. out = wire.AppendVarint(out, uint64(v))
  191. case Int32:
  192. out = wire.AppendFixed32(out, uint32(v))
  193. case Uint32:
  194. out = wire.AppendFixed32(out, uint32(v))
  195. case Float32:
  196. out = wire.AppendFixed32(out, math.Float32bits(float32(v)))
  197. case Int64:
  198. out = wire.AppendFixed64(out, uint64(v))
  199. case Uint64:
  200. out = wire.AppendFixed64(out, uint64(v))
  201. case Float64:
  202. out = wire.AppendFixed64(out, math.Float64bits(float64(v)))
  203. case String:
  204. out = wire.AppendBytes(out, []byte(v))
  205. case Bytes:
  206. out = wire.AppendBytes(out, []byte(v))
  207. case LengthPrefix:
  208. out = wire.AppendBytes(out, Message(v).Marshal())
  209. case Denormalized:
  210. b := Message{v.Value}.Marshal()
  211. _, n := wire.ConsumeVarint(b)
  212. out = append(out, b[:n]...)
  213. for i := uint(0); i < v.Count; i++ {
  214. out[len(out)-1] |= 0x80 // set continuation bit on previous
  215. out = append(out, 0)
  216. }
  217. out = append(out, b[n:]...)
  218. case Raw:
  219. return append(out, v...)
  220. default:
  221. panic(fmt.Sprintf("unknown type: %T", v))
  222. }
  223. }
  224. return out
  225. }
  226. // Unmarshal parses the input protobuf wire data as a Message AST.
  227. // Any parsing error results in the remainder of the input being
  228. // concatenated to the message as a Raw type.
  229. //
  230. // Each tag (a tuple of the field number and wire type) encountered is
  231. // appended to the AST as a Tag.
  232. //
  233. // The contents of each wire type is mapped to the following Go types:
  234. // VarintType => Uvarint
  235. // Fixed32Type => Uint32
  236. // Fixed64Type => Uint64
  237. // BytesType => Bytes
  238. // GroupType => Message
  239. //
  240. // Since the wire format is not self-describing, this function cannot parse
  241. // sub-messages and will leave them as the Bytes type. Further manual parsing
  242. // can be performed as such:
  243. // var m, m1, m2 Message
  244. // m.Unmarshal(b)
  245. // m1.Unmarshal(m[3].(Bytes))
  246. // m[3] = LengthPrefix(m1)
  247. // m2.Unmarshal(m[3].(LengthPrefix)[1].(Bytes))
  248. // m[3].(LengthPrefix)[1] = LengthPrefix(m2)
  249. //
  250. // Unmarshal is useful for debugging the protobuf wire format.
  251. func (m *Message) Unmarshal(in []byte) {
  252. m.UnmarshalDescriptor(in, nil)
  253. }
  254. // UnmarshalDescriptor parses the input protobuf wire data as a Message AST
  255. // using the provided message descriptor for more accurate parsing of fields.
  256. // It operates like Unmarshal, but may use a wider range of Go types to
  257. // represent the wire data.
  258. //
  259. // The contents of each wire type is mapped to one of the following Go types:
  260. // VarintType => Bool, Varint, Svarint, Uvarint
  261. // Fixed32Type => Int32, Uint32, Float32
  262. // Fixed64Type => Uint32, Uint64, Float64
  263. // BytesType => String, Bytes, LengthPrefix
  264. // GroupType => Message
  265. //
  266. // If the field is unknown, it uses the same mapping as Unmarshal.
  267. // Known sub-messages are parsed as a Message and packed repeated fields are
  268. // parsed as a LengthPrefix.
  269. func (m *Message) UnmarshalDescriptor(in []byte, desc protoreflect.MessageDescriptor) {
  270. p := parser{in: in, out: *m}
  271. p.parseMessage(desc, false)
  272. *m = p.out
  273. }
  274. type parser struct {
  275. in []byte
  276. out []Token
  277. }
  278. func (p *parser) parseMessage(msgDesc protoreflect.MessageDescriptor, group bool) {
  279. for len(p.in) > 0 {
  280. v, n := wire.ConsumeVarint(p.in)
  281. num, typ := wire.DecodeTag(v)
  282. if n < 0 || num < 0 || v > math.MaxUint32 {
  283. p.out, p.in = append(p.out, Raw(p.in)), nil
  284. return
  285. }
  286. if typ == EndGroupType && group {
  287. return // if inside a group, then stop
  288. }
  289. p.out, p.in = append(p.out, Tag{num, typ}), p.in[n:]
  290. if m := n - wire.SizeVarint(v); m > 0 {
  291. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  292. }
  293. // If descriptor is available, use it for more accurate parsing.
  294. var isPacked bool
  295. var kind protoreflect.Kind
  296. var subDesc protoreflect.MessageDescriptor
  297. if msgDesc != nil && !msgDesc.IsPlaceholder() {
  298. if fieldDesc := msgDesc.Fields().ByNumber(num); fieldDesc != nil {
  299. isPacked = fieldDesc.IsPacked()
  300. kind = fieldDesc.Kind()
  301. switch kind {
  302. case protoreflect.MessageKind, protoreflect.GroupKind:
  303. subDesc = fieldDesc.MessageType()
  304. if subDesc == nil || subDesc.IsPlaceholder() {
  305. kind = 0
  306. }
  307. }
  308. }
  309. }
  310. switch typ {
  311. case VarintType:
  312. p.parseVarint(kind)
  313. case Fixed32Type:
  314. p.parseFixed32(kind)
  315. case Fixed64Type:
  316. p.parseFixed64(kind)
  317. case BytesType:
  318. p.parseBytes(isPacked, kind, subDesc)
  319. case StartGroupType:
  320. p.parseGroup(subDesc)
  321. case EndGroupType:
  322. // Handled above.
  323. default:
  324. p.out, p.in = append(p.out, Raw(p.in)), nil
  325. }
  326. }
  327. }
  328. func (p *parser) parseVarint(kind protoreflect.Kind) {
  329. v, n := wire.ConsumeVarint(p.in)
  330. if n < 0 {
  331. p.out, p.in = append(p.out, Raw(p.in)), nil
  332. return
  333. }
  334. switch kind {
  335. case protoreflect.BoolKind:
  336. switch v {
  337. case 0:
  338. p.out, p.in = append(p.out, Bool(false)), p.in[n:]
  339. case 1:
  340. p.out, p.in = append(p.out, Bool(true)), p.in[n:]
  341. default:
  342. p.out, p.in = append(p.out, Uvarint(v)), p.in[n:]
  343. }
  344. case protoreflect.Int32Kind, protoreflect.Int64Kind:
  345. p.out, p.in = append(p.out, Varint(v)), p.in[n:]
  346. case protoreflect.Sint32Kind, protoreflect.Sint64Kind:
  347. p.out, p.in = append(p.out, Svarint(wire.DecodeZigZag(v))), p.in[n:]
  348. default:
  349. p.out, p.in = append(p.out, Uvarint(v)), p.in[n:]
  350. }
  351. if m := n - wire.SizeVarint(v); m > 0 {
  352. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  353. }
  354. }
  355. func (p *parser) parseFixed32(kind protoreflect.Kind) {
  356. v, n := wire.ConsumeFixed32(p.in)
  357. if n < 0 {
  358. p.out, p.in = append(p.out, Raw(p.in)), nil
  359. return
  360. }
  361. switch kind {
  362. case protoreflect.FloatKind:
  363. p.out, p.in = append(p.out, Float32(math.Float32frombits(v))), p.in[n:]
  364. case protoreflect.Sfixed32Kind:
  365. p.out, p.in = append(p.out, Int32(v)), p.in[n:]
  366. default:
  367. p.out, p.in = append(p.out, Uint32(v)), p.in[n:]
  368. }
  369. }
  370. func (p *parser) parseFixed64(kind protoreflect.Kind) {
  371. v, n := wire.ConsumeFixed64(p.in)
  372. if n < 0 {
  373. p.out, p.in = append(p.out, Raw(p.in)), nil
  374. return
  375. }
  376. switch kind {
  377. case protoreflect.DoubleKind:
  378. p.out, p.in = append(p.out, Float64(math.Float64frombits(v))), p.in[n:]
  379. case protoreflect.Sfixed64Kind:
  380. p.out, p.in = append(p.out, Int64(v)), p.in[n:]
  381. default:
  382. p.out, p.in = append(p.out, Uint64(v)), p.in[n:]
  383. }
  384. }
  385. func (p *parser) parseBytes(isPacked bool, kind protoreflect.Kind, desc protoreflect.MessageDescriptor) {
  386. v, n := wire.ConsumeVarint(p.in)
  387. if n < 0 {
  388. p.out, p.in = append(p.out, Raw(p.in)), nil
  389. return
  390. }
  391. p.out, p.in = append(p.out, Uvarint(v)), p.in[n:]
  392. if m := n - wire.SizeVarint(v); m > 0 {
  393. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  394. }
  395. if v > uint64(len(p.in)) {
  396. p.out, p.in = append(p.out, Raw(p.in)), nil
  397. return
  398. }
  399. p.out = p.out[:len(p.out)-1] // subsequent tokens contain prefix-length
  400. if isPacked {
  401. p.parsePacked(int(v), kind)
  402. } else {
  403. switch kind {
  404. case protoreflect.MessageKind:
  405. p2 := parser{in: p.in[:v]}
  406. p2.parseMessage(desc, false)
  407. p.out, p.in = append(p.out, LengthPrefix(p2.out)), p.in[v:]
  408. case protoreflect.StringKind:
  409. p.out, p.in = append(p.out, String(p.in[:v])), p.in[v:]
  410. default:
  411. p.out, p.in = append(p.out, Bytes(p.in[:v])), p.in[v:]
  412. }
  413. }
  414. if m := n - wire.SizeVarint(v); m > 0 {
  415. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  416. }
  417. }
  418. func (p *parser) parsePacked(n int, kind protoreflect.Kind) {
  419. p2 := parser{in: p.in[:n]}
  420. for len(p2.in) > 0 {
  421. switch kind {
  422. case protoreflect.BoolKind, protoreflect.EnumKind,
  423. protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind,
  424. protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind:
  425. p2.parseVarint(kind)
  426. case protoreflect.Fixed32Kind, protoreflect.Sfixed32Kind, protoreflect.FloatKind:
  427. p2.parseFixed32(kind)
  428. case protoreflect.Fixed64Kind, protoreflect.Sfixed64Kind, protoreflect.DoubleKind:
  429. p2.parseFixed64(kind)
  430. default:
  431. panic(fmt.Sprintf("invalid packed kind: %v", kind))
  432. }
  433. }
  434. p.out, p.in = append(p.out, LengthPrefix(p2.out)), p.in[n:]
  435. }
  436. func (p *parser) parseGroup(desc protoreflect.MessageDescriptor) {
  437. p2 := parser{in: p.in}
  438. p2.parseMessage(desc, true)
  439. if len(p2.out) > 0 {
  440. p.out = append(p.out, Message(p2.out))
  441. }
  442. p.in = p2.in
  443. // Append the trailing end group.
  444. v, n := wire.ConsumeVarint(p.in)
  445. if num, typ := wire.DecodeTag(v); typ == EndGroupType {
  446. p.out, p.in = append(p.out, Tag{num, typ}), p.in[n:]
  447. if m := n - wire.SizeVarint(v); m > 0 {
  448. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  449. }
  450. }
  451. }
  452. // Format implements a custom formatter to visualize the Message AST.
  453. // Using "%#v" formats the Message in Go source code.
  454. func (m Message) Format(s fmt.State, r rune) {
  455. switch r {
  456. case 'x':
  457. io.WriteString(s, fmt.Sprintf("%x", m.Marshal()))
  458. case 'X':
  459. io.WriteString(s, fmt.Sprintf("%X", m.Marshal()))
  460. case 'v':
  461. switch {
  462. case s.Flag('#'):
  463. io.WriteString(s, m.format(true, true))
  464. case s.Flag('+'):
  465. io.WriteString(s, m.format(false, true))
  466. default:
  467. io.WriteString(s, m.format(false, false))
  468. }
  469. default:
  470. panic("invalid verb: " + string(r))
  471. }
  472. }
  473. // format formats the message.
  474. // If source is enabled, this emits valid Go source.
  475. // If multi is enabled, the output may span multiple lines.
  476. func (m Message) format(source, multi bool) string {
  477. var ss []string
  478. var prefix, nextPrefix string
  479. for _, v := range m {
  480. // Ensure certain tokens have preceding or succeeding newlines.
  481. prefix, nextPrefix = nextPrefix, " "
  482. if multi {
  483. switch v := v.(type) {
  484. case Tag: // only has preceding newline
  485. prefix = "\n"
  486. case Denormalized: // only has preceding newline
  487. if _, ok := v.Value.(Tag); ok {
  488. prefix = "\n"
  489. }
  490. case Message, Raw: // has preceding and succeeding newlines
  491. prefix, nextPrefix = "\n", "\n"
  492. }
  493. }
  494. s := formatToken(v, source, multi)
  495. ss = append(ss, prefix+s+",")
  496. }
  497. var s string
  498. if len(ss) > 0 {
  499. s = strings.TrimSpace(strings.Join(ss, ""))
  500. if multi {
  501. s = "\n\t" + strings.Join(strings.Split(s, "\n"), "\n\t") + "\n"
  502. } else {
  503. s = strings.TrimSuffix(s, ",")
  504. }
  505. }
  506. s = fmt.Sprintf("%T{%s}", m, s)
  507. if !source {
  508. s = trimPackage(s)
  509. }
  510. return s
  511. }
  512. // formatToken formats a single token.
  513. func formatToken(t Token, source, multi bool) (s string) {
  514. switch v := t.(type) {
  515. case Message:
  516. s = v.format(source, multi)
  517. case LengthPrefix:
  518. s = formatPacked(v, source, multi)
  519. if s == "" {
  520. ms := Message(v).format(source, multi)
  521. s = fmt.Sprintf("%T(%s)", v, ms)
  522. }
  523. case Tag:
  524. s = fmt.Sprintf("%T{%d, %s}", v, v.Number, formatType(v.Type, source))
  525. case Bool, Varint, Svarint, Uvarint, Int32, Uint32, Float32, Int64, Uint64, Float64:
  526. if source {
  527. // Print floats in a way that preserves exact precision.
  528. if f, _ := v.(Float32); math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) {
  529. switch {
  530. case f > 0:
  531. s = fmt.Sprintf("%T(math.Inf(+1))", v)
  532. case f < 0:
  533. s = fmt.Sprintf("%T(math.Inf(-1))", v)
  534. case math.Float32bits(float32(math.NaN())) == math.Float32bits(float32(f)):
  535. s = fmt.Sprintf("%T(math.NaN())", v)
  536. default:
  537. s = fmt.Sprintf("%T(math.Float32frombits(0x%08x))", v, math.Float32bits(float32(f)))
  538. }
  539. break
  540. }
  541. if f, _ := v.(Float64); math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) {
  542. switch {
  543. case f > 0:
  544. s = fmt.Sprintf("%T(math.Inf(+1))", v)
  545. case f < 0:
  546. s = fmt.Sprintf("%T(math.Inf(-1))", v)
  547. case math.Float64bits(float64(math.NaN())) == math.Float64bits(float64(f)):
  548. s = fmt.Sprintf("%T(math.NaN())", v)
  549. default:
  550. s = fmt.Sprintf("%T(math.Float64frombits(0x%08x))", v, math.Float64bits(float64(f)))
  551. }
  552. break
  553. }
  554. }
  555. s = fmt.Sprintf("%T(%v)", v, v)
  556. case String, Bytes, Raw:
  557. s = fmt.Sprintf("%s", v)
  558. s = fmt.Sprintf("%T(%s)", v, formatString(s))
  559. case Denormalized:
  560. s = fmt.Sprintf("%T{+%d, %v}", v, v.Count, formatToken(v.Value, source, multi))
  561. default:
  562. panic(fmt.Sprintf("unknown type: %T", v))
  563. }
  564. if !source {
  565. s = trimPackage(s)
  566. }
  567. return s
  568. }
  569. // formatPacked returns a non-empty string if LengthPrefix looks like a packed
  570. // repeated field of primitives.
  571. func formatPacked(v LengthPrefix, source, multi bool) string {
  572. var ss []string
  573. for _, v := range v {
  574. switch v.(type) {
  575. case Bool, Varint, Svarint, Uvarint, Int32, Uint32, Float32, Int64, Uint64, Float64, Denormalized, Raw:
  576. if v, ok := v.(Denormalized); ok {
  577. switch v.Value.(type) {
  578. case Bool, Varint, Svarint, Uvarint:
  579. default:
  580. return ""
  581. }
  582. }
  583. ss = append(ss, formatToken(v, source, multi))
  584. default:
  585. return ""
  586. }
  587. }
  588. s := fmt.Sprintf("%T{%s}", v, strings.Join(ss, ", "))
  589. if !source {
  590. s = trimPackage(s)
  591. }
  592. return s
  593. }
  594. // formatType returns the name for Type.
  595. func formatType(t Type, source bool) (s string) {
  596. switch t {
  597. case VarintType:
  598. s = pkg + ".VarintType"
  599. case Fixed32Type:
  600. s = pkg + ".Fixed32Type"
  601. case Fixed64Type:
  602. s = pkg + ".Fixed64Type"
  603. case BytesType:
  604. s = pkg + ".BytesType"
  605. case StartGroupType:
  606. s = pkg + ".StartGroupType"
  607. case EndGroupType:
  608. s = pkg + ".EndGroupType"
  609. default:
  610. s = fmt.Sprintf("Type(%d)", t)
  611. }
  612. if !source {
  613. s = strings.TrimSuffix(trimPackage(s), "Type")
  614. }
  615. return s
  616. }
  617. // formatString returns a quoted string for s.
  618. func formatString(s string) string {
  619. // Use quoted string if it the same length as a raw string literal.
  620. // Otherwise, attempt to use the raw string form.
  621. qs := strconv.Quote(s)
  622. if len(qs) == 1+len(s)+1 {
  623. return qs
  624. }
  625. // Disallow newlines to ensure output is a single line.
  626. // Disallow non-printable runes for readability purposes.
  627. rawInvalid := func(r rune) bool {
  628. return r == '`' || r == '\n' || r == utf8.RuneError || !unicode.IsPrint(r)
  629. }
  630. if strings.IndexFunc(s, rawInvalid) < 0 {
  631. return "`" + s + "`"
  632. }
  633. return qs
  634. }
  635. var pkg = path.Base(reflect.TypeOf(Tag{}).PkgPath())
  636. func trimPackage(s string) string {
  637. return strings.TrimPrefix(strings.TrimPrefix(s, pkg), ".")
  638. }