properties.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. /*
  33. * Routines for encoding data into the wire format for protocol buffers.
  34. */
  35. import (
  36. "fmt"
  37. "os"
  38. "reflect"
  39. "sort"
  40. "strconv"
  41. "strings"
  42. "sync"
  43. )
  44. const debug bool = false
  45. // Constants that identify the encoding of a value on the wire.
  46. const (
  47. WireVarint = 0
  48. WireFixed64 = 1
  49. WireBytes = 2
  50. WireStartGroup = 3
  51. WireEndGroup = 4
  52. WireFixed32 = 5
  53. )
  54. const startSize = 10 // initial slice/string sizes
  55. // Encoders are defined in encoder.go
  56. // An encoder outputs the full representation of a field, including its
  57. // tag and encoder type.
  58. type encoder func(p *Buffer, prop *Properties, base structPointer) error
  59. // A valueEncoder encodes a single integer in a particular encoding.
  60. type valueEncoder func(o *Buffer, x uint64) error
  61. // Decoders are defined in decode.go
  62. // A decoder creates a value from its wire representation.
  63. // Unrecognized subelements are saved in unrec.
  64. type decoder func(p *Buffer, prop *Properties, base structPointer) error
  65. // A valueDecoder decodes a single integer in a particular encoding.
  66. type valueDecoder func(o *Buffer) (x uint64, err error)
  67. // tagMap is an optimization over map[int]int for typical protocol buffer
  68. // use-cases. Encoded protocol buffers are often in tag order with small tag
  69. // numbers.
  70. type tagMap struct {
  71. fastTags []int
  72. slowTags map[int]int
  73. }
  74. // tagMapFastLimit is the upper bound on the tag number that will be stored in
  75. // the tagMap slice rather than its map.
  76. const tagMapFastLimit = 1024
  77. func (p *tagMap) get(t int) (int, bool) {
  78. if t > 0 && t < tagMapFastLimit {
  79. if t >= len(p.fastTags) {
  80. return 0, false
  81. }
  82. fi := p.fastTags[t]
  83. return fi, fi >= 0
  84. }
  85. fi, ok := p.slowTags[t]
  86. return fi, ok
  87. }
  88. func (p *tagMap) put(t int, fi int) {
  89. if t > 0 && t < tagMapFastLimit {
  90. for len(p.fastTags) < t+1 {
  91. p.fastTags = append(p.fastTags, -1)
  92. }
  93. p.fastTags[t] = fi
  94. return
  95. }
  96. if p.slowTags == nil {
  97. p.slowTags = make(map[int]int)
  98. }
  99. p.slowTags[t] = fi
  100. }
  101. // StructProperties represents properties for all the fields of a struct.
  102. // decoderTags and decoderOrigNames should only be used by the decoder.
  103. type StructProperties struct {
  104. Prop []*Properties // properties for each field
  105. reqCount int // required count
  106. decoderTags tagMap // map from proto tag to struct field number
  107. decoderOrigNames map[string]int // map from original name to struct field number
  108. order []int // list of struct field numbers in tag order
  109. unrecField field // field id of the XXX_unrecognized []byte field
  110. extendable bool // is this an extendable proto
  111. }
  112. // Implement the sorting interface so we can sort the fields in tag order, as recommended by the spec.
  113. // See encoder.go, (*Buffer).enc_struct.
  114. func (sp *StructProperties) Len() int { return len(sp.order) }
  115. func (sp *StructProperties) Less(i, j int) bool {
  116. return sp.Prop[sp.order[i]].Tag < sp.Prop[sp.order[j]].Tag
  117. }
  118. func (sp *StructProperties) Swap(i, j int) { sp.order[i], sp.order[j] = sp.order[j], sp.order[i] }
  119. // Properties represents the protocol-specific behavior of a single struct field.
  120. type Properties struct {
  121. Name string // name of the field, for error messages
  122. OrigName string // original name before protocol compiler (always set)
  123. Wire string
  124. WireType int
  125. Tag int
  126. Required bool
  127. Optional bool
  128. Repeated bool
  129. Packed bool // relevant for repeated primitives only
  130. Enum string // set for enum types only
  131. Default string // default value
  132. def_uint64 uint64
  133. enc encoder
  134. valEnc valueEncoder // set for bool and numeric types only
  135. field field
  136. tagcode []byte // encoding of EncodeVarint((Tag<<3)|WireType)
  137. tagbuf [8]byte
  138. stype reflect.Type // set for struct types only
  139. sprop *StructProperties // set for struct types only
  140. isMarshaler bool
  141. isUnmarshaler bool
  142. dec decoder
  143. valDec valueDecoder // set for bool and numeric types only
  144. // If this is a packable field, this will be the decoder for the packed version of the field.
  145. packedDec decoder
  146. }
  147. // String formats the properties in the protobuf struct field tag style.
  148. func (p *Properties) String() string {
  149. s := p.Wire
  150. s = ","
  151. s += strconv.Itoa(p.Tag)
  152. if p.Required {
  153. s += ",req"
  154. }
  155. if p.Optional {
  156. s += ",opt"
  157. }
  158. if p.Repeated {
  159. s += ",rep"
  160. }
  161. if p.Packed {
  162. s += ",packed"
  163. }
  164. if p.OrigName != p.Name {
  165. s += ",name=" + p.OrigName
  166. }
  167. if len(p.Enum) > 0 {
  168. s += ",enum=" + p.Enum
  169. }
  170. if len(p.Default) > 0 {
  171. s += ",def=" + p.Default
  172. }
  173. return s
  174. }
  175. // Parse populates p by parsing a string in the protobuf struct field tag style.
  176. func (p *Properties) Parse(s string) {
  177. // "bytes,49,opt,name=foo,def=hello!"
  178. fields := strings.Split(s, ",") // breaks def=, but handled below.
  179. if len(fields) < 2 {
  180. fmt.Fprintf(os.Stderr, "proto: tag has too few fields: %q\n", s)
  181. return
  182. }
  183. p.Wire = fields[0]
  184. switch p.Wire {
  185. case "varint":
  186. p.WireType = WireVarint
  187. p.valEnc = (*Buffer).EncodeVarint
  188. p.valDec = (*Buffer).DecodeVarint
  189. case "fixed32":
  190. p.WireType = WireFixed32
  191. p.valEnc = (*Buffer).EncodeFixed32
  192. p.valDec = (*Buffer).DecodeFixed32
  193. case "fixed64":
  194. p.WireType = WireFixed64
  195. p.valEnc = (*Buffer).EncodeFixed64
  196. p.valDec = (*Buffer).DecodeFixed64
  197. case "zigzag32":
  198. p.WireType = WireVarint
  199. p.valEnc = (*Buffer).EncodeZigzag32
  200. p.valDec = (*Buffer).DecodeZigzag32
  201. case "zigzag64":
  202. p.WireType = WireVarint
  203. p.valEnc = (*Buffer).EncodeZigzag64
  204. p.valDec = (*Buffer).DecodeZigzag64
  205. case "bytes", "group":
  206. p.WireType = WireBytes
  207. // no numeric converter for non-numeric types
  208. default:
  209. fmt.Fprintf(os.Stderr, "proto: tag has unknown wire type: %q\n", s)
  210. return
  211. }
  212. var err error
  213. p.Tag, err = strconv.Atoi(fields[1])
  214. if err != nil {
  215. return
  216. }
  217. for i := 2; i < len(fields); i++ {
  218. f := fields[i]
  219. switch {
  220. case f == "req":
  221. p.Required = true
  222. case f == "opt":
  223. p.Optional = true
  224. case f == "rep":
  225. p.Repeated = true
  226. case f == "packed":
  227. p.Packed = true
  228. case strings.HasPrefix(f, "name="):
  229. p.OrigName = f[5:]
  230. case strings.HasPrefix(f, "enum="):
  231. p.Enum = f[5:]
  232. case strings.HasPrefix(f, "def="):
  233. p.Default = f[4:] // rest of string
  234. if i+1 < len(fields) {
  235. // Commas aren't escaped, and def is always last.
  236. p.Default += "," + strings.Join(fields[i+1:], ",")
  237. break
  238. }
  239. }
  240. }
  241. }
  242. func logNoSliceEnc(t1, t2 reflect.Type) {
  243. fmt.Fprintf(os.Stderr, "proto: no slice oenc for %T = []%T\n", t1, t2)
  244. }
  245. var protoMessageType = reflect.TypeOf((*Message)(nil)).Elem()
  246. // Initialize the fields for encoding and decoding.
  247. func (p *Properties) setEncAndDec(typ reflect.Type, lockGetProp bool) {
  248. p.enc = nil
  249. p.dec = nil
  250. switch t1 := typ; t1.Kind() {
  251. default:
  252. fmt.Fprintf(os.Stderr, "proto: no coders for %T\n", t1)
  253. case reflect.Ptr:
  254. switch t2 := t1.Elem(); t2.Kind() {
  255. default:
  256. fmt.Fprintf(os.Stderr, "proto: no encoder function for %T -> %T\n", t1, t2)
  257. break
  258. case reflect.Bool:
  259. p.enc = (*Buffer).enc_bool
  260. p.dec = (*Buffer).dec_bool
  261. case reflect.Int32, reflect.Uint32:
  262. p.enc = (*Buffer).enc_int32
  263. p.dec = (*Buffer).dec_int32
  264. case reflect.Int64, reflect.Uint64:
  265. p.enc = (*Buffer).enc_int64
  266. p.dec = (*Buffer).dec_int64
  267. case reflect.Float32:
  268. p.enc = (*Buffer).enc_int32 // can just treat them as bits
  269. p.dec = (*Buffer).dec_int32
  270. case reflect.Float64:
  271. p.enc = (*Buffer).enc_int64 // can just treat them as bits
  272. p.dec = (*Buffer).dec_int64
  273. case reflect.String:
  274. p.enc = (*Buffer).enc_string
  275. p.dec = (*Buffer).dec_string
  276. case reflect.Struct:
  277. p.stype = t1.Elem()
  278. p.isMarshaler = isMarshaler(t1)
  279. p.isUnmarshaler = isUnmarshaler(t1)
  280. if p.Wire == "bytes" {
  281. p.enc = (*Buffer).enc_struct_message
  282. p.dec = (*Buffer).dec_struct_message
  283. } else {
  284. p.enc = (*Buffer).enc_struct_group
  285. p.dec = (*Buffer).dec_struct_group
  286. }
  287. }
  288. case reflect.Slice:
  289. switch t2 := t1.Elem(); t2.Kind() {
  290. default:
  291. logNoSliceEnc(t1, t2)
  292. break
  293. case reflect.Bool:
  294. if p.Packed {
  295. p.enc = (*Buffer).enc_slice_packed_bool
  296. } else {
  297. p.enc = (*Buffer).enc_slice_bool
  298. }
  299. p.dec = (*Buffer).dec_slice_bool
  300. p.packedDec = (*Buffer).dec_slice_packed_bool
  301. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  302. switch t2.Bits() {
  303. case 32:
  304. if p.Packed {
  305. p.enc = (*Buffer).enc_slice_packed_int32
  306. } else {
  307. p.enc = (*Buffer).enc_slice_int32
  308. }
  309. p.dec = (*Buffer).dec_slice_int32
  310. p.packedDec = (*Buffer).dec_slice_packed_int32
  311. case 64:
  312. if p.Packed {
  313. p.enc = (*Buffer).enc_slice_packed_int64
  314. } else {
  315. p.enc = (*Buffer).enc_slice_int64
  316. }
  317. p.dec = (*Buffer).dec_slice_int64
  318. p.packedDec = (*Buffer).dec_slice_packed_int64
  319. case 8:
  320. if t2.Kind() == reflect.Uint8 {
  321. p.enc = (*Buffer).enc_slice_byte
  322. p.dec = (*Buffer).dec_slice_byte
  323. }
  324. default:
  325. logNoSliceEnc(t1, t2)
  326. break
  327. }
  328. case reflect.Float32, reflect.Float64:
  329. switch t2.Bits() {
  330. case 32:
  331. // can just treat them as bits
  332. if p.Packed {
  333. p.enc = (*Buffer).enc_slice_packed_int32
  334. } else {
  335. p.enc = (*Buffer).enc_slice_int32
  336. }
  337. p.dec = (*Buffer).dec_slice_int32
  338. p.packedDec = (*Buffer).dec_slice_packed_int32
  339. case 64:
  340. // can just treat them as bits
  341. if p.Packed {
  342. p.enc = (*Buffer).enc_slice_packed_int64
  343. } else {
  344. p.enc = (*Buffer).enc_slice_int64
  345. }
  346. p.dec = (*Buffer).dec_slice_int64
  347. p.packedDec = (*Buffer).dec_slice_packed_int64
  348. default:
  349. logNoSliceEnc(t1, t2)
  350. break
  351. }
  352. case reflect.String:
  353. p.enc = (*Buffer).enc_slice_string
  354. p.dec = (*Buffer).dec_slice_string
  355. case reflect.Ptr:
  356. switch t3 := t2.Elem(); t3.Kind() {
  357. default:
  358. fmt.Fprintf(os.Stderr, "proto: no ptr oenc for %T -> %T -> %T\n", t1, t2, t3)
  359. break
  360. case reflect.Struct:
  361. p.stype = t2.Elem()
  362. p.isMarshaler = isMarshaler(t2)
  363. p.isUnmarshaler = isUnmarshaler(t2)
  364. p.enc = (*Buffer).enc_slice_struct_group
  365. p.dec = (*Buffer).dec_slice_struct_group
  366. if p.Wire == "bytes" {
  367. p.enc = (*Buffer).enc_slice_struct_message
  368. p.dec = (*Buffer).dec_slice_struct_message
  369. }
  370. }
  371. case reflect.Slice:
  372. switch t2.Elem().Kind() {
  373. default:
  374. fmt.Fprintf(os.Stderr, "proto: no slice elem oenc for %T -> %T -> %T\n", t1, t2, t2.Elem())
  375. break
  376. case reflect.Uint8:
  377. p.enc = (*Buffer).enc_slice_slice_byte
  378. p.dec = (*Buffer).dec_slice_slice_byte
  379. }
  380. }
  381. }
  382. // precalculate tag code
  383. wire := p.WireType
  384. if p.Packed {
  385. wire = WireBytes
  386. }
  387. x := uint32(p.Tag)<<3 | uint32(wire)
  388. i := 0
  389. for i = 0; x > 127; i++ {
  390. p.tagbuf[i] = 0x80 | uint8(x&0x7F)
  391. x >>= 7
  392. }
  393. p.tagbuf[i] = uint8(x)
  394. p.tagcode = p.tagbuf[0 : i+1]
  395. if p.stype != nil {
  396. if lockGetProp {
  397. p.sprop = GetProperties(p.stype)
  398. } else {
  399. p.sprop = getPropertiesLocked(p.stype)
  400. }
  401. }
  402. }
  403. var (
  404. marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
  405. unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
  406. )
  407. // isMarshaler reports whether type t implements Marshaler.
  408. func isMarshaler(t reflect.Type) bool {
  409. // We're checking for (likely) pointer-receiver methods
  410. // so if t is not a pointer, something is very wrong.
  411. // The calls above only invoke isMarshaler on pointer types.
  412. if t.Kind() != reflect.Ptr {
  413. panic("proto: misuse of isMarshaler")
  414. }
  415. return t.Implements(marshalerType)
  416. }
  417. // isUnmarshaler reports whether type t implements Unmarshaler.
  418. func isUnmarshaler(t reflect.Type) bool {
  419. // We're checking for (likely) pointer-receiver methods
  420. // so if t is not a pointer, something is very wrong.
  421. // The calls above only invoke isUnmarshaler on pointer types.
  422. if t.Kind() != reflect.Ptr {
  423. panic("proto: misuse of isUnmarshaler")
  424. }
  425. return t.Implements(unmarshalerType)
  426. }
  427. // Init populates the properties from a protocol buffer struct tag.
  428. func (p *Properties) Init(typ reflect.Type, name, tag string, f *reflect.StructField) {
  429. p.init(typ, name, tag, f, true)
  430. }
  431. func (p *Properties) init(typ reflect.Type, name, tag string, f *reflect.StructField, lockGetProp bool) {
  432. // "bytes,49,opt,def=hello!"
  433. p.Name = name
  434. p.OrigName = name
  435. if f != nil {
  436. p.field = toField(f)
  437. }
  438. if tag == "" {
  439. return
  440. }
  441. p.Parse(tag)
  442. p.setEncAndDec(typ, lockGetProp)
  443. }
  444. var (
  445. mutex sync.Mutex
  446. propertiesMap = make(map[reflect.Type]*StructProperties)
  447. )
  448. // GetProperties returns the list of properties for the type represented by t.
  449. func GetProperties(t reflect.Type) *StructProperties {
  450. mutex.Lock()
  451. sprop := getPropertiesLocked(t)
  452. mutex.Unlock()
  453. return sprop
  454. }
  455. // getPropertiesLocked requires that mutex is held.
  456. func getPropertiesLocked(t reflect.Type) *StructProperties {
  457. if prop, ok := propertiesMap[t]; ok {
  458. if collectStats {
  459. stats.Chit++
  460. }
  461. return prop
  462. }
  463. if collectStats {
  464. stats.Cmiss++
  465. }
  466. prop := new(StructProperties)
  467. // in case of recursive protos, fill this in now.
  468. propertiesMap[t] = prop
  469. // build properties
  470. prop.extendable = reflect.PtrTo(t).Implements(extendableProtoType)
  471. prop.unrecField = invalidField
  472. prop.Prop = make([]*Properties, t.NumField())
  473. prop.order = make([]int, t.NumField())
  474. for i := 0; i < t.NumField(); i++ {
  475. f := t.Field(i)
  476. p := new(Properties)
  477. name := f.Name
  478. p.init(f.Type, name, f.Tag.Get("protobuf"), &f, false)
  479. if f.Name == "XXX_extensions" { // special case
  480. p.enc = (*Buffer).enc_map
  481. p.dec = nil // not needed
  482. }
  483. if f.Name == "XXX_unrecognized" { // special case
  484. prop.unrecField = toField(&f)
  485. }
  486. prop.Prop[i] = p
  487. prop.order[i] = i
  488. if debug {
  489. print(i, " ", f.Name, " ", t.String(), " ")
  490. if p.Tag > 0 {
  491. print(p.String())
  492. }
  493. print("\n")
  494. }
  495. if p.enc == nil && !strings.HasPrefix(f.Name, "XXX_") {
  496. fmt.Fprintln(os.Stderr, "proto: no encoder for", f.Name, f.Type.String(), "[GetProperties]")
  497. }
  498. }
  499. // Re-order prop.order.
  500. sort.Sort(prop)
  501. // build required counts
  502. // build tags
  503. reqCount := 0
  504. prop.decoderOrigNames = make(map[string]int)
  505. for i, p := range prop.Prop {
  506. if strings.HasPrefix(p.Name, "XXX_") {
  507. // Internal fields should not appear in tags/origNames maps.
  508. // They are handled specially when encoding and decoding.
  509. continue
  510. }
  511. if p.Required {
  512. reqCount++
  513. }
  514. prop.decoderTags.put(p.Tag, i)
  515. prop.decoderOrigNames[p.OrigName] = i
  516. }
  517. prop.reqCount = reqCount
  518. return prop
  519. }
  520. // Return the Properties object for the x[0]'th field of the structure.
  521. func propByIndex(t reflect.Type, x []int) *Properties {
  522. if len(x) != 1 {
  523. fmt.Fprintf(os.Stderr, "proto: field index dimension %d (not 1) for type %s\n", len(x), t)
  524. return nil
  525. }
  526. prop := GetProperties(t)
  527. return prop.Prop[x[0]]
  528. }
  529. // Get the address and type of a pointer to a struct from an interface.
  530. func getbase(pb Message) (t reflect.Type, b structPointer, err error) {
  531. if pb == nil {
  532. err = ErrNil
  533. return
  534. }
  535. // get the reflect type of the pointer to the struct.
  536. t = reflect.TypeOf(pb)
  537. // get the address of the struct.
  538. value := reflect.ValueOf(pb)
  539. b = toStructPointer(value)
  540. return
  541. }
  542. // A global registry of enum types.
  543. // The generated code will register the generated maps by calling RegisterEnum.
  544. var enumValueMaps = make(map[string]map[string]int32)
  545. // RegisterEnum is called from the generated code to install the enum descriptor
  546. // maps into the global table to aid parsing text format protocol buffers.
  547. func RegisterEnum(typeName string, unusedNameMap map[int32]string, valueMap map[string]int32) {
  548. if _, ok := enumValueMaps[typeName]; ok {
  549. panic("proto: duplicate enum registered: " + typeName)
  550. }
  551. enumValueMaps[typeName] = valueMap
  552. }