properties.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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. /*
  38. * Routines for encoding data into the wire format for protocol buffers.
  39. */
  40. import (
  41. "fmt"
  42. "log"
  43. "os"
  44. "reflect"
  45. "sort"
  46. "strconv"
  47. "strings"
  48. "sync"
  49. )
  50. const debug bool = false
  51. // Constants that identify the encoding of a value on the wire.
  52. const (
  53. WireVarint = 0
  54. WireFixed64 = 1
  55. WireBytes = 2
  56. WireStartGroup = 3
  57. WireEndGroup = 4
  58. WireFixed32 = 5
  59. )
  60. const startSize = 10 // initial slice/string sizes
  61. // Encoders are defined in encode.go
  62. // An encoder outputs the full representation of a field, including its
  63. // tag and encoder type.
  64. type encoder func(p *Buffer, prop *Properties, base structPointer) error
  65. // A valueEncoder encodes a single integer in a particular encoding.
  66. type valueEncoder func(o *Buffer, x uint64) error
  67. // Sizers are defined in encode.go
  68. // A sizer returns the encoded size of a field, including its tag and encoder
  69. // type.
  70. type sizer func(prop *Properties, base structPointer) int
  71. // A valueSizer returns the encoded size of a single integer in a particular
  72. // encoding.
  73. type valueSizer func(x uint64) int
  74. // Decoders are defined in decode.go
  75. // A decoder creates a value from its wire representation.
  76. // Unrecognized subelements are saved in unrec.
  77. type decoder func(p *Buffer, prop *Properties, base structPointer) error
  78. // A valueDecoder decodes a single integer in a particular encoding.
  79. type valueDecoder func(o *Buffer) (x uint64, err error)
  80. // A oneofMarshaler does the marshaling for all oneof fields in a message.
  81. type oneofMarshaler func(Message, *Buffer) error
  82. // A oneofUnmarshaler does the unmarshaling for a oneof field in a message.
  83. type oneofUnmarshaler func(Message, int, int, *Buffer) (bool, error)
  84. // A oneofSizer does the sizing for all oneof fields in a message.
  85. type oneofSizer func(Message) int
  86. // tagMap is an optimization over map[int]int for typical protocol buffer
  87. // use-cases. Encoded protocol buffers are often in tag order with small tag
  88. // numbers.
  89. type tagMap struct {
  90. fastTags []int
  91. slowTags map[int]int
  92. }
  93. // tagMapFastLimit is the upper bound on the tag number that will be stored in
  94. // the tagMap slice rather than its map.
  95. const tagMapFastLimit = 1024
  96. func (p *tagMap) get(t int) (int, bool) {
  97. if t > 0 && t < tagMapFastLimit {
  98. if t >= len(p.fastTags) {
  99. return 0, false
  100. }
  101. fi := p.fastTags[t]
  102. return fi, fi >= 0
  103. }
  104. fi, ok := p.slowTags[t]
  105. return fi, ok
  106. }
  107. func (p *tagMap) put(t int, fi int) {
  108. if t > 0 && t < tagMapFastLimit {
  109. for len(p.fastTags) < t+1 {
  110. p.fastTags = append(p.fastTags, -1)
  111. }
  112. p.fastTags[t] = fi
  113. return
  114. }
  115. if p.slowTags == nil {
  116. p.slowTags = make(map[int]int)
  117. }
  118. p.slowTags[t] = fi
  119. }
  120. // StructProperties represents properties for all the fields of a struct.
  121. // decoderTags and decoderOrigNames should only be used by the decoder.
  122. type StructProperties struct {
  123. Prop []*Properties // properties for each field
  124. reqCount int // required count
  125. decoderTags tagMap // map from proto tag to struct field number
  126. decoderOrigNames map[string]int // map from original name to struct field number
  127. order []int // list of struct field numbers in tag order
  128. unrecField field // field id of the XXX_unrecognized []byte field
  129. extendable bool // is this an extendable proto
  130. oneofMarshaler oneofMarshaler
  131. oneofUnmarshaler oneofUnmarshaler
  132. oneofSizer oneofSizer
  133. stype reflect.Type
  134. // OneofTypes contains information about the oneof fields in this message.
  135. // It is keyed by the original name of a field.
  136. OneofTypes map[string]*OneofProperties
  137. }
  138. // OneofProperties represents information about a specific field in a oneof.
  139. type OneofProperties struct {
  140. Type reflect.Type // pointer to generated struct type for this oneof field
  141. Field int // struct field number of the containing oneof in the message
  142. Prop *Properties
  143. }
  144. // Implement the sorting interface so we can sort the fields in tag order, as recommended by the spec.
  145. // See encode.go, (*Buffer).enc_struct.
  146. func (sp *StructProperties) Len() int { return len(sp.order) }
  147. func (sp *StructProperties) Less(i, j int) bool {
  148. return sp.Prop[sp.order[i]].Tag < sp.Prop[sp.order[j]].Tag
  149. }
  150. func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order[j], sp.order[i] }
  151. // Properties represents the protocol-specific behavior of a single struct field.
  152. type Properties struct {
  153. Name string // name of the field, for error messages
  154. OrigName string // original name before protocol compiler (always set)
  155. JSONName string // name to use for JSON; determined by protoc
  156. Wire string
  157. WireType int
  158. Tag int
  159. Required bool
  160. Optional bool
  161. Repeated bool
  162. Packed bool // relevant for repeated primitives only
  163. Enum string // set for enum types only
  164. proto3 bool // whether this is known to be a proto3 field; set for []byte only
  165. oneof bool // whether this is a oneof field
  166. Default string // default value
  167. HasDefault bool // whether an explicit default was provided
  168. CustomType string
  169. CastType string
  170. StdTime bool
  171. StdDuration bool
  172. enc encoder
  173. valEnc valueEncoder // set for bool and numeric types only
  174. field field
  175. tagcode []byte // encoding of EncodeVarint((Tag<<3)|WireType)
  176. tagbuf [8]byte
  177. stype reflect.Type // set for struct types only
  178. sstype reflect.Type // set for slices of structs types only
  179. ctype reflect.Type // set for custom types only
  180. sprop *StructProperties // set for struct types only
  181. isMarshaler bool
  182. isUnmarshaler bool
  183. mtype reflect.Type // set for map types only
  184. mkeyprop *Properties // set for map types only
  185. mvalprop *Properties // set for map types only
  186. size sizer
  187. valSize valueSizer // set for bool and numeric types only
  188. dec decoder
  189. valDec valueDecoder // set for bool and numeric types only
  190. // If this is a packable field, this will be the decoder for the packed version of the field.
  191. packedDec decoder
  192. }
  193. // String formats the properties in the protobuf struct field tag style.
  194. func (p *Properties) String() string {
  195. s := p.Wire
  196. s = ","
  197. s += strconv.Itoa(p.Tag)
  198. if p.Required {
  199. s += ",req"
  200. }
  201. if p.Optional {
  202. s += ",opt"
  203. }
  204. if p.Repeated {
  205. s += ",rep"
  206. }
  207. if p.Packed {
  208. s += ",packed"
  209. }
  210. s += ",name=" + p.OrigName
  211. if p.JSONName != p.OrigName {
  212. s += ",json=" + p.JSONName
  213. }
  214. if p.proto3 {
  215. s += ",proto3"
  216. }
  217. if p.oneof {
  218. s += ",oneof"
  219. }
  220. if len(p.Enum) > 0 {
  221. s += ",enum=" + p.Enum
  222. }
  223. if p.HasDefault {
  224. s += ",def=" + p.Default
  225. }
  226. return s
  227. }
  228. // Parse populates p by parsing a string in the protobuf struct field tag style.
  229. func (p *Properties) Parse(s string) {
  230. // "bytes,49,opt,name=foo,def=hello!"
  231. fields := strings.Split(s, ",") // breaks def=, but handled below.
  232. if len(fields) < 2 {
  233. fmt.Fprintf(os.Stderr, "proto: tag has too few fields: %q\n", s)
  234. return
  235. }
  236. p.Wire = fields[0]
  237. switch p.Wire {
  238. case "varint":
  239. p.WireType = WireVarint
  240. p.valEnc = (*Buffer).EncodeVarint
  241. p.valDec = (*Buffer).DecodeVarint
  242. p.valSize = sizeVarint
  243. case "fixed32":
  244. p.WireType = WireFixed32
  245. p.valEnc = (*Buffer).EncodeFixed32
  246. p.valDec = (*Buffer).DecodeFixed32
  247. p.valSize = sizeFixed32
  248. case "fixed64":
  249. p.WireType = WireFixed64
  250. p.valEnc = (*Buffer).EncodeFixed64
  251. p.valDec = (*Buffer).DecodeFixed64
  252. p.valSize = sizeFixed64
  253. case "zigzag32":
  254. p.WireType = WireVarint
  255. p.valEnc = (*Buffer).EncodeZigzag32
  256. p.valDec = (*Buffer).DecodeZigzag32
  257. p.valSize = sizeZigzag32
  258. case "zigzag64":
  259. p.WireType = WireVarint
  260. p.valEnc = (*Buffer).EncodeZigzag64
  261. p.valDec = (*Buffer).DecodeZigzag64
  262. p.valSize = sizeZigzag64
  263. case "bytes", "group":
  264. p.WireType = WireBytes
  265. // no numeric converter for non-numeric types
  266. default:
  267. fmt.Fprintf(os.Stderr, "proto: tag has unknown wire type: %q\n", s)
  268. return
  269. }
  270. var err error
  271. p.Tag, err = strconv.Atoi(fields[1])
  272. if err != nil {
  273. return
  274. }
  275. for i := 2; i < len(fields); i++ {
  276. f := fields[i]
  277. switch {
  278. case f == "req":
  279. p.Required = true
  280. case f == "opt":
  281. p.Optional = true
  282. case f == "rep":
  283. p.Repeated = true
  284. case f == "packed":
  285. p.Packed = true
  286. case strings.HasPrefix(f, "name="):
  287. p.OrigName = f[5:]
  288. case strings.HasPrefix(f, "json="):
  289. p.JSONName = f[5:]
  290. case strings.HasPrefix(f, "enum="):
  291. p.Enum = f[5:]
  292. case f == "proto3":
  293. p.proto3 = true
  294. case f == "oneof":
  295. p.oneof = true
  296. case strings.HasPrefix(f, "def="):
  297. p.HasDefault = true
  298. p.Default = f[4:] // rest of string
  299. if i+1 < len(fields) {
  300. // Commas aren't escaped, and def is always last.
  301. p.Default += "," + strings.Join(fields[i+1:], ",")
  302. break
  303. }
  304. case strings.HasPrefix(f, "embedded="):
  305. p.OrigName = strings.Split(f, "=")[1]
  306. case strings.HasPrefix(f, "customtype="):
  307. p.CustomType = strings.Split(f, "=")[1]
  308. case strings.HasPrefix(f, "casttype="):
  309. p.CastType = strings.Split(f, "=")[1]
  310. case f == "stdtime":
  311. p.StdTime = true
  312. case f == "stdduration":
  313. p.StdDuration = true
  314. }
  315. }
  316. }
  317. func logNoSliceEnc(t1, t2 reflect.Type) {
  318. fmt.Fprintf(os.Stderr, "proto: no slice oenc for %T = []%T\n", t1, t2)
  319. }
  320. var protoMessageType = reflect.TypeOf((*Message)(nil)).Elem()
  321. // Initialize the fields for encoding and decoding.
  322. func (p *Properties) setEncAndDec(typ reflect.Type, f *reflect.StructField, lockGetProp bool) {
  323. p.enc = nil
  324. p.dec = nil
  325. p.size = nil
  326. isMap := typ.Kind() == reflect.Map
  327. if len(p.CustomType) > 0 && !isMap {
  328. p.setCustomEncAndDec(typ)
  329. p.setTag(lockGetProp)
  330. return
  331. }
  332. if p.StdTime && !isMap {
  333. p.setTimeEncAndDec(typ)
  334. p.setTag(lockGetProp)
  335. return
  336. }
  337. if p.StdDuration && !isMap {
  338. p.setDurationEncAndDec(typ)
  339. p.setTag(lockGetProp)
  340. return
  341. }
  342. switch t1 := typ; t1.Kind() {
  343. default:
  344. fmt.Fprintf(os.Stderr, "proto: no coders for %v\n", t1)
  345. // proto3 scalar types
  346. case reflect.Bool:
  347. if p.proto3 {
  348. p.enc = (*Buffer).enc_proto3_bool
  349. p.dec = (*Buffer).dec_proto3_bool
  350. p.size = size_proto3_bool
  351. } else {
  352. p.enc = (*Buffer).enc_ref_bool
  353. p.dec = (*Buffer).dec_proto3_bool
  354. p.size = size_ref_bool
  355. }
  356. case reflect.Int32:
  357. if p.proto3 {
  358. p.enc = (*Buffer).enc_proto3_int32
  359. p.dec = (*Buffer).dec_proto3_int32
  360. p.size = size_proto3_int32
  361. } else {
  362. p.enc = (*Buffer).enc_ref_int32
  363. p.dec = (*Buffer).dec_proto3_int32
  364. p.size = size_ref_int32
  365. }
  366. case reflect.Uint32:
  367. if p.proto3 {
  368. p.enc = (*Buffer).enc_proto3_uint32
  369. p.dec = (*Buffer).dec_proto3_int32 // can reuse
  370. p.size = size_proto3_uint32
  371. } else {
  372. p.enc = (*Buffer).enc_ref_uint32
  373. p.dec = (*Buffer).dec_proto3_int32 // can reuse
  374. p.size = size_ref_uint32
  375. }
  376. case reflect.Int64, reflect.Uint64:
  377. if p.proto3 {
  378. p.enc = (*Buffer).enc_proto3_int64
  379. p.dec = (*Buffer).dec_proto3_int64
  380. p.size = size_proto3_int64
  381. } else {
  382. p.enc = (*Buffer).enc_ref_int64
  383. p.dec = (*Buffer).dec_proto3_int64
  384. p.size = size_ref_int64
  385. }
  386. case reflect.Float32:
  387. if p.proto3 {
  388. p.enc = (*Buffer).enc_proto3_uint32 // can just treat them as bits
  389. p.dec = (*Buffer).dec_proto3_int32
  390. p.size = size_proto3_uint32
  391. } else {
  392. p.enc = (*Buffer).enc_ref_uint32 // can just treat them as bits
  393. p.dec = (*Buffer).dec_proto3_int32
  394. p.size = size_ref_uint32
  395. }
  396. case reflect.Float64:
  397. if p.proto3 {
  398. p.enc = (*Buffer).enc_proto3_int64 // can just treat them as bits
  399. p.dec = (*Buffer).dec_proto3_int64
  400. p.size = size_proto3_int64
  401. } else {
  402. p.enc = (*Buffer).enc_ref_int64 // can just treat them as bits
  403. p.dec = (*Buffer).dec_proto3_int64
  404. p.size = size_ref_int64
  405. }
  406. case reflect.String:
  407. if p.proto3 {
  408. p.enc = (*Buffer).enc_proto3_string
  409. p.dec = (*Buffer).dec_proto3_string
  410. p.size = size_proto3_string
  411. } else {
  412. p.enc = (*Buffer).enc_ref_string
  413. p.dec = (*Buffer).dec_proto3_string
  414. p.size = size_ref_string
  415. }
  416. case reflect.Struct:
  417. p.stype = typ
  418. p.isMarshaler = isMarshaler(typ)
  419. p.isUnmarshaler = isUnmarshaler(typ)
  420. if p.Wire == "bytes" {
  421. p.enc = (*Buffer).enc_ref_struct_message
  422. p.dec = (*Buffer).dec_ref_struct_message
  423. p.size = size_ref_struct_message
  424. } else {
  425. fmt.Fprintf(os.Stderr, "proto: no coders for struct %T\n", typ)
  426. }
  427. case reflect.Ptr:
  428. switch t2 := t1.Elem(); t2.Kind() {
  429. default:
  430. fmt.Fprintf(os.Stderr, "proto: no encoder function for %v -> %v\n", t1, t2)
  431. break
  432. case reflect.Bool:
  433. p.enc = (*Buffer).enc_bool
  434. p.dec = (*Buffer).dec_bool
  435. p.size = size_bool
  436. case reflect.Int32:
  437. p.enc = (*Buffer).enc_int32
  438. p.dec = (*Buffer).dec_int32
  439. p.size = size_int32
  440. case reflect.Uint32:
  441. p.enc = (*Buffer).enc_uint32
  442. p.dec = (*Buffer).dec_int32 // can reuse
  443. p.size = size_uint32
  444. case reflect.Int64, reflect.Uint64:
  445. p.enc = (*Buffer).enc_int64
  446. p.dec = (*Buffer).dec_int64
  447. p.size = size_int64
  448. case reflect.Float32:
  449. p.enc = (*Buffer).enc_uint32 // can just treat them as bits
  450. p.dec = (*Buffer).dec_int32
  451. p.size = size_uint32
  452. case reflect.Float64:
  453. p.enc = (*Buffer).enc_int64 // can just treat them as bits
  454. p.dec = (*Buffer).dec_int64
  455. p.size = size_int64
  456. case reflect.String:
  457. p.enc = (*Buffer).enc_string
  458. p.dec = (*Buffer).dec_string
  459. p.size = size_string
  460. case reflect.Struct:
  461. p.stype = t1.Elem()
  462. p.isMarshaler = isMarshaler(t1)
  463. p.isUnmarshaler = isUnmarshaler(t1)
  464. if p.Wire == "bytes" {
  465. p.enc = (*Buffer).enc_struct_message
  466. p.dec = (*Buffer).dec_struct_message
  467. p.size = size_struct_message
  468. } else {
  469. p.enc = (*Buffer).enc_struct_group
  470. p.dec = (*Buffer).dec_struct_group
  471. p.size = size_struct_group
  472. }
  473. }
  474. case reflect.Slice:
  475. switch t2 := t1.Elem(); t2.Kind() {
  476. default:
  477. logNoSliceEnc(t1, t2)
  478. break
  479. case reflect.Bool:
  480. if p.Packed {
  481. p.enc = (*Buffer).enc_slice_packed_bool
  482. p.size = size_slice_packed_bool
  483. } else {
  484. p.enc = (*Buffer).enc_slice_bool
  485. p.size = size_slice_bool
  486. }
  487. p.dec = (*Buffer).dec_slice_bool
  488. p.packedDec = (*Buffer).dec_slice_packed_bool
  489. case reflect.Int32:
  490. if p.Packed {
  491. p.enc = (*Buffer).enc_slice_packed_int32
  492. p.size = size_slice_packed_int32
  493. } else {
  494. p.enc = (*Buffer).enc_slice_int32
  495. p.size = size_slice_int32
  496. }
  497. p.dec = (*Buffer).dec_slice_int32
  498. p.packedDec = (*Buffer).dec_slice_packed_int32
  499. case reflect.Uint32:
  500. if p.Packed {
  501. p.enc = (*Buffer).enc_slice_packed_uint32
  502. p.size = size_slice_packed_uint32
  503. } else {
  504. p.enc = (*Buffer).enc_slice_uint32
  505. p.size = size_slice_uint32
  506. }
  507. p.dec = (*Buffer).dec_slice_int32
  508. p.packedDec = (*Buffer).dec_slice_packed_int32
  509. case reflect.Int64, reflect.Uint64:
  510. if p.Packed {
  511. p.enc = (*Buffer).enc_slice_packed_int64
  512. p.size = size_slice_packed_int64
  513. } else {
  514. p.enc = (*Buffer).enc_slice_int64
  515. p.size = size_slice_int64
  516. }
  517. p.dec = (*Buffer).dec_slice_int64
  518. p.packedDec = (*Buffer).dec_slice_packed_int64
  519. case reflect.Uint8:
  520. p.dec = (*Buffer).dec_slice_byte
  521. if p.proto3 {
  522. p.enc = (*Buffer).enc_proto3_slice_byte
  523. p.size = size_proto3_slice_byte
  524. } else {
  525. p.enc = (*Buffer).enc_slice_byte
  526. p.size = size_slice_byte
  527. }
  528. case reflect.Float32, reflect.Float64:
  529. switch t2.Bits() {
  530. case 32:
  531. // can just treat them as bits
  532. if p.Packed {
  533. p.enc = (*Buffer).enc_slice_packed_uint32
  534. p.size = size_slice_packed_uint32
  535. } else {
  536. p.enc = (*Buffer).enc_slice_uint32
  537. p.size = size_slice_uint32
  538. }
  539. p.dec = (*Buffer).dec_slice_int32
  540. p.packedDec = (*Buffer).dec_slice_packed_int32
  541. case 64:
  542. // can just treat them as bits
  543. if p.Packed {
  544. p.enc = (*Buffer).enc_slice_packed_int64
  545. p.size = size_slice_packed_int64
  546. } else {
  547. p.enc = (*Buffer).enc_slice_int64
  548. p.size = size_slice_int64
  549. }
  550. p.dec = (*Buffer).dec_slice_int64
  551. p.packedDec = (*Buffer).dec_slice_packed_int64
  552. default:
  553. logNoSliceEnc(t1, t2)
  554. break
  555. }
  556. case reflect.String:
  557. p.enc = (*Buffer).enc_slice_string
  558. p.dec = (*Buffer).dec_slice_string
  559. p.size = size_slice_string
  560. case reflect.Ptr:
  561. switch t3 := t2.Elem(); t3.Kind() {
  562. default:
  563. fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T -> %T\n", t1, t2, t3)
  564. break
  565. case reflect.Struct:
  566. p.stype = t2.Elem()
  567. p.isMarshaler = isMarshaler(t2)
  568. p.isUnmarshaler = isUnmarshaler(t2)
  569. if p.Wire == "bytes" {
  570. p.enc = (*Buffer).enc_slice_struct_message
  571. p.dec = (*Buffer).dec_slice_struct_message
  572. p.size = size_slice_struct_message
  573. } else {
  574. p.enc = (*Buffer).enc_slice_struct_group
  575. p.dec = (*Buffer).dec_slice_struct_group
  576. p.size = size_slice_struct_group
  577. }
  578. }
  579. case reflect.Slice:
  580. switch t2.Elem().Kind() {
  581. default:
  582. fmt.Fprintf(os.Stderr, "proto: no slice elem oenc for %T -> %T -> %T\n", t1, t2, t2.Elem())
  583. break
  584. case reflect.Uint8:
  585. p.enc = (*Buffer).enc_slice_slice_byte
  586. p.dec = (*Buffer).dec_slice_slice_byte
  587. p.size = size_slice_slice_byte
  588. }
  589. case reflect.Struct:
  590. p.setSliceOfNonPointerStructs(t1)
  591. }
  592. case reflect.Map:
  593. p.enc = (*Buffer).enc_new_map
  594. p.dec = (*Buffer).dec_new_map
  595. p.size = size_new_map
  596. p.mtype = t1
  597. p.mkeyprop = &Properties{}
  598. p.mkeyprop.init(reflect.PtrTo(p.mtype.Key()), "Key", f.Tag.Get("protobuf_key"), nil, lockGetProp)
  599. p.mvalprop = &Properties{}
  600. vtype := p.mtype.Elem()
  601. if vtype.Kind() != reflect.Ptr && vtype.Kind() != reflect.Slice {
  602. // The value type is not a message (*T) or bytes ([]byte),
  603. // so we need encoders for the pointer to this type.
  604. vtype = reflect.PtrTo(vtype)
  605. }
  606. p.mvalprop.CustomType = p.CustomType
  607. p.mvalprop.StdDuration = p.StdDuration
  608. p.mvalprop.StdTime = p.StdTime
  609. p.mvalprop.init(vtype, "Value", f.Tag.Get("protobuf_val"), nil, lockGetProp)
  610. }
  611. p.setTag(lockGetProp)
  612. }
  613. func (p *Properties) setTag(lockGetProp bool) {
  614. // precalculate tag code
  615. wire := p.WireType
  616. if p.Packed {
  617. wire = WireBytes
  618. }
  619. x := uint32(p.Tag)<<3 | uint32(wire)
  620. i := 0
  621. for i = 0; x > 127; i++ {
  622. p.tagbuf[i] = 0x80 | uint8(x&0x7F)
  623. x >>= 7
  624. }
  625. p.tagbuf[i] = uint8(x)
  626. p.tagcode = p.tagbuf[0 : i+1]
  627. if p.stype != nil {
  628. if lockGetProp {
  629. p.sprop = GetProperties(p.stype)
  630. } else {
  631. p.sprop = getPropertiesLocked(p.stype)
  632. }
  633. }
  634. }
  635. var (
  636. marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
  637. unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
  638. )
  639. // isMarshaler reports whether type t implements Marshaler.
  640. func isMarshaler(t reflect.Type) bool {
  641. return t.Implements(marshalerType)
  642. }
  643. // isUnmarshaler reports whether type t implements Unmarshaler.
  644. func isUnmarshaler(t reflect.Type) bool {
  645. return t.Implements(unmarshalerType)
  646. }
  647. // Init populates the properties from a protocol buffer struct tag.
  648. func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) {
  649. p.init(typ, name, tag, f, true)
  650. }
  651. func (p *Properties) init(typ reflect.Type, name, tag string, f *reflect.StructField, lockGetProp bool) {
  652. // "bytes,49,opt,def=hello!"
  653. p.Name = name
  654. p.OrigName = name
  655. if f != nil {
  656. p.field = toField(f)
  657. }
  658. if tag == "" {
  659. return
  660. }
  661. p.Parse(tag)
  662. p.setEncAndDec(typ, f, lockGetProp)
  663. }
  664. var (
  665. propertiesMu sync.RWMutex
  666. propertiesMap = make(map[reflect.Type]*StructProperties)
  667. )
  668. // GetProperties returns the list of properties for the type represented by t.
  669. // t must represent a generated struct type of a protocol message.
  670. func GetProperties(t reflect.Type) *StructProperties {
  671. if t.Kind() != reflect.Struct {
  672. panic("proto: type must have kind struct")
  673. }
  674. // Most calls to GetProperties in a long-running program will be
  675. // retrieving details for types we have seen before.
  676. propertiesMu.RLock()
  677. sprop, ok := propertiesMap[t]
  678. propertiesMu.RUnlock()
  679. if ok {
  680. if collectStats {
  681. stats.Chit++
  682. }
  683. return sprop
  684. }
  685. propertiesMu.Lock()
  686. sprop = getPropertiesLocked(t)
  687. propertiesMu.Unlock()
  688. return sprop
  689. }
  690. // getPropertiesLocked requires that propertiesMu is held.
  691. func getPropertiesLocked(t reflect.Type) *StructProperties {
  692. if prop, ok := propertiesMap[t]; ok {
  693. if collectStats {
  694. stats.Chit++
  695. }
  696. return prop
  697. }
  698. if collectStats {
  699. stats.Cmiss++
  700. }
  701. prop := new(StructProperties)
  702. // in case of recursive protos, fill this in now.
  703. propertiesMap[t] = prop
  704. // build properties
  705. prop.extendable = reflect.PtrTo(t).Implements(extendableProtoType) ||
  706. reflect.PtrTo(t).Implements(extendableProtoV1Type) ||
  707. reflect.PtrTo(t).Implements(extendableBytesType)
  708. prop.unrecField = invalidField
  709. prop.Prop = make([]*Properties, t.NumField())
  710. prop.order = make([]int, t.NumField())
  711. isOneofMessage := false
  712. for i := 0; i < t.NumField(); i++ {
  713. f := t.Field(i)
  714. p := new(Properties)
  715. name := f.Name
  716. p.init(f.Type, name, f.Tag.Get("protobuf"), &f, false)
  717. if f.Name == "XXX_InternalExtensions" { // special case
  718. p.enc = (*Buffer).enc_exts
  719. p.dec = nil // not needed
  720. p.size = size_exts
  721. } else if f.Name == "XXX_extensions" { // special case
  722. if len(f.Tag.Get("protobuf")) > 0 {
  723. p.enc = (*Buffer).enc_ext_slice_byte
  724. p.dec = nil // not needed
  725. p.size = size_ext_slice_byte
  726. } else {
  727. p.enc = (*Buffer).enc_map
  728. p.dec = nil // not needed
  729. p.size = size_map
  730. }
  731. } else if f.Name == "XXX_unrecognized" { // special case
  732. prop.unrecField = toField(&f)
  733. }
  734. oneof := f.Tag.Get("protobuf_oneof") // special case
  735. if oneof != "" {
  736. isOneofMessage = true
  737. // Oneof fields don't use the traditional protobuf tag.
  738. p.OrigName = oneof
  739. }
  740. prop.Prop[i] = p
  741. prop.order[i] = i
  742. if debug {
  743. print(i, " ", f.Name, " ", t.String(), " ")
  744. if p.Tag > 0 {
  745. print(p.String())
  746. }
  747. print("\n")
  748. }
  749. if p.enc == nil && !strings.HasPrefix(f.Name, "XXX_") && oneof == "" {
  750. fmt.Fprintln(os.Stderr, "proto: no encoder for", f.Name, f.Type.String(), "[GetProperties]")
  751. }
  752. }
  753. // Re-order prop.order.
  754. sort.Sort(prop)
  755. type oneofMessage interface {
  756. XXX_OneofFuncs() (func(Message, *Buffer) error, func(Message, int, int, *Buffer) (bool, error), func(Message) int, []interface{})
  757. }
  758. if om, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(oneofMessage); isOneofMessage && ok {
  759. var oots []interface{}
  760. prop.oneofMarshaler, prop.oneofUnmarshaler, prop.oneofSizer, oots = om.XXX_OneofFuncs()
  761. prop.stype = t
  762. // Interpret oneof metadata.
  763. prop.OneofTypes = make(map[string]*OneofProperties)
  764. for _, oot := range oots {
  765. oop := &OneofProperties{
  766. Type: reflect.ValueOf(oot).Type(), // *T
  767. Prop: new(Properties),
  768. }
  769. sft := oop.Type.Elem().Field(0)
  770. oop.Prop.Name = sft.Name
  771. oop.Prop.Parse(sft.Tag.Get("protobuf"))
  772. // There will be exactly one interface field that
  773. // this new value is assignable to.
  774. for i := 0; i < t.NumField(); i++ {
  775. f := t.Field(i)
  776. if f.Type.Kind() != reflect.Interface {
  777. continue
  778. }
  779. if !oop.Type.AssignableTo(f.Type) {
  780. continue
  781. }
  782. oop.Field = i
  783. break
  784. }
  785. prop.OneofTypes[oop.Prop.OrigName] = oop
  786. }
  787. }
  788. // build required counts
  789. // build tags
  790. reqCount := 0
  791. prop.decoderOrigNames = make(map[string]int)
  792. for i, p := range prop.Prop {
  793. if strings.HasPrefix(p.Name, "XXX_") {
  794. // Internal fields should not appear in tags/origNames maps.
  795. // They are handled specially when encoding and decoding.
  796. continue
  797. }
  798. if p.Required {
  799. reqCount++
  800. }
  801. prop.decoderTags.put(p.Tag, i)
  802. prop.decoderOrigNames[p.OrigName] = i
  803. }
  804. prop.reqCount = reqCount
  805. return prop
  806. }
  807. // Return the Properties object for the x[0]'th field of the structure.
  808. func propByIndex(t reflect.Type, x []int) *Properties {
  809. if len(x) != 1 {
  810. fmt.Fprintf(os.Stderr, "proto: field index dimension %d (not 1) for type %s\n", len(x), t)
  811. return nil
  812. }
  813. prop := GetProperties(t)
  814. return prop.Prop[x[0]]
  815. }
  816. // Get the address and type of a pointer to a struct from an interface.
  817. func getbase(pb Message) (t reflect.Type, b structPointer, err error) {
  818. if pb == nil {
  819. err = ErrNil
  820. return
  821. }
  822. // get the reflect type of the pointer to the struct.
  823. t = reflect.TypeOf(pb)
  824. // get the address of the struct.
  825. value := reflect.ValueOf(pb)
  826. b = toStructPointer(value)
  827. return
  828. }
  829. // A global registry of enum types.
  830. // The generated code will register the generated maps by calling RegisterEnum.
  831. var enumValueMaps = make(map[string]map[string]int32)
  832. var enumStringMaps = make(map[string]map[int32]string)
  833. // RegisterEnum is called from the generated code to install the enum descriptor
  834. // maps into the global table to aid parsing text format protocol buffers.
  835. func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) {
  836. if _, ok := enumValueMaps[typeName]; ok {
  837. panic("proto: duplicate enum registered: " + typeName)
  838. }
  839. enumValueMaps[typeName] = valueMap
  840. if _, ok := enumStringMaps[typeName]; ok {
  841. panic("proto: duplicate enum registered: " + typeName)
  842. }
  843. enumStringMaps[typeName] = unusedNameMap
  844. }
  845. // EnumValueMap returns the mapping from names to integers of the
  846. // enum type enumType, or a nil if not found.
  847. func EnumValueMap(enumType string) map[string]int32 {
  848. return enumValueMaps[enumType]
  849. }
  850. // A registry of all linked message types.
  851. // The string is a fully-qualified proto name ("pkg.Message").
  852. var (
  853. protoTypes = make(map[string]reflect.Type)
  854. revProtoTypes = make(map[reflect.Type]string)
  855. )
  856. // RegisterType is called from generated code and maps from the fully qualified
  857. // proto name to the type (pointer to struct) of the protocol buffer.
  858. func RegisterType(x Message, name string) {
  859. if _, ok := protoTypes[name]; ok {
  860. // TODO: Some day, make this a panic.
  861. log.Printf("proto: duplicate proto type registered: %s", name)
  862. return
  863. }
  864. t := reflect.TypeOf(x)
  865. protoTypes[name] = t
  866. revProtoTypes[t] = name
  867. }
  868. // MessageName returns the fully-qualified proto name for the given message type.
  869. func MessageName(x Message) string {
  870. type xname interface {
  871. XXX_MessageName() string
  872. }
  873. if m, ok := x.(xname); ok {
  874. return m.XXX_MessageName()
  875. }
  876. return revProtoTypes[reflect.TypeOf(x)]
  877. }
  878. // MessageType returns the message type (pointer to struct) for a named message.
  879. func MessageType(name string) reflect.Type { return protoTypes[name] }
  880. // A registry of all linked proto files.
  881. var (
  882. protoFiles = make(map[string][]byte) // file name => fileDescriptor
  883. )
  884. // RegisterFile is called from generated code and maps from the
  885. // full file name of a .proto file to its compressed FileDescriptorProto.
  886. func RegisterFile(filename string, fileDescriptor []byte) {
  887. protoFiles[filename] = fileDescriptor
  888. }
  889. // FileDescriptor returns the compressed FileDescriptorProto for a .proto file.
  890. func FileDescriptor(filename string) []byte { return protoFiles[filename] }