encode.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. "errors"
  37. "fmt"
  38. "reflect"
  39. "sort"
  40. )
  41. // RequiredNotSetError is the error returned if Marshal is called with
  42. // a protocol buffer struct whose required fields have not
  43. // all been initialized. It is also the error returned if Unmarshal is
  44. // called with an encoded protocol buffer that does not include all the
  45. // required fields.
  46. //
  47. // When printed, RequiredNotSetError reports the first unset required field in a
  48. // message. If the field cannot be precisely determined, it is reported as
  49. // "{Unknown}".
  50. type RequiredNotSetError struct {
  51. field string
  52. }
  53. func (e *RequiredNotSetError) Error() string {
  54. return fmt.Sprintf("proto: required field %q not set", e.field)
  55. }
  56. var (
  57. // ErrRepeatedHasNil is the error returned if Marshal is called with
  58. // a struct with a repeated field containing a nil element.
  59. ErrRepeatedHasNil = errors.New("proto: repeated field has nil element")
  60. // ErrNil is the error returned if Marshal is called with nil.
  61. ErrNil = errors.New("proto: Marshal called with nil")
  62. )
  63. // The fundamental encoders that put bytes on the wire.
  64. // Those that take integer types all accept uint64 and are
  65. // therefore of type valueEncoder.
  66. const maxVarintBytes = 10 // maximum length of a varint
  67. // EncodeVarint returns the varint encoding of x.
  68. // This is the format for the
  69. // int32, int64, uint32, uint64, bool, and enum
  70. // protocol buffer types.
  71. // Not used by the package itself, but helpful to clients
  72. // wishing to use the same encoding.
  73. func EncodeVarint(x uint64) []byte {
  74. var buf [maxVarintBytes]byte
  75. var n int
  76. for n = 0; x > 127; n++ {
  77. buf[n] = 0x80 | uint8(x&0x7F)
  78. x >>= 7
  79. }
  80. buf[n] = uint8(x)
  81. n++
  82. return buf[0:n]
  83. }
  84. // EncodeVarint writes a varint-encoded integer to the Buffer.
  85. // This is the format for the
  86. // int32, int64, uint32, uint64, bool, and enum
  87. // protocol buffer types.
  88. func (p *Buffer) EncodeVarint(x uint64) error {
  89. for x >= 1<<7 {
  90. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  91. x >>= 7
  92. }
  93. p.buf = append(p.buf, uint8(x))
  94. return nil
  95. }
  96. func sizeVarint(x uint64) (n int) {
  97. for {
  98. n++
  99. x >>= 7
  100. if x == 0 {
  101. break
  102. }
  103. }
  104. return n
  105. }
  106. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  107. // This is the format for the
  108. // fixed64, sfixed64, and double protocol buffer types.
  109. func (p *Buffer) EncodeFixed64(x uint64) error {
  110. p.buf = append(p.buf,
  111. uint8(x),
  112. uint8(x>>8),
  113. uint8(x>>16),
  114. uint8(x>>24),
  115. uint8(x>>32),
  116. uint8(x>>40),
  117. uint8(x>>48),
  118. uint8(x>>56))
  119. return nil
  120. }
  121. func sizeFixed64(x uint64) int {
  122. return 8
  123. }
  124. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  125. // This is the format for the
  126. // fixed32, sfixed32, and float protocol buffer types.
  127. func (p *Buffer) EncodeFixed32(x uint64) error {
  128. p.buf = append(p.buf,
  129. uint8(x),
  130. uint8(x>>8),
  131. uint8(x>>16),
  132. uint8(x>>24))
  133. return nil
  134. }
  135. func sizeFixed32(x uint64) int {
  136. return 4
  137. }
  138. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  139. // to the Buffer.
  140. // This is the format used for the sint64 protocol buffer type.
  141. func (p *Buffer) EncodeZigzag64(x uint64) error {
  142. // use signed number to get arithmetic right shift.
  143. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  144. }
  145. func sizeZigzag64(x uint64) int {
  146. return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  147. }
  148. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  149. // to the Buffer.
  150. // This is the format used for the sint32 protocol buffer type.
  151. func (p *Buffer) EncodeZigzag32(x uint64) error {
  152. // use signed number to get arithmetic right shift.
  153. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  154. }
  155. func sizeZigzag32(x uint64) int {
  156. return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  157. }
  158. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  159. // This is the format used for the bytes protocol buffer
  160. // type and for embedded messages.
  161. func (p *Buffer) EncodeRawBytes(b []byte) error {
  162. p.EncodeVarint(uint64(len(b)))
  163. p.buf = append(p.buf, b...)
  164. return nil
  165. }
  166. func sizeRawBytes(b []byte) int {
  167. return sizeVarint(uint64(len(b))) +
  168. len(b)
  169. }
  170. // EncodeStringBytes writes an encoded string to the Buffer.
  171. // This is the format used for the proto2 string type.
  172. func (p *Buffer) EncodeStringBytes(s string) error {
  173. p.EncodeVarint(uint64(len(s)))
  174. p.buf = append(p.buf, s...)
  175. return nil
  176. }
  177. func sizeStringBytes(s string) int {
  178. return sizeVarint(uint64(len(s))) +
  179. len(s)
  180. }
  181. // Marshaler is the interface representing objects that can marshal themselves.
  182. type Marshaler interface {
  183. Marshal() ([]byte, error)
  184. }
  185. // Marshal takes the protocol buffer
  186. // and encodes it into the wire format, returning the data.
  187. func Marshal(pb Message) ([]byte, error) {
  188. // Can the object marshal itself?
  189. if m, ok := pb.(Marshaler); ok {
  190. return m.Marshal()
  191. }
  192. p := NewBuffer(nil)
  193. err := p.Marshal(pb)
  194. var state errorState
  195. if err != nil && !state.shouldContinue(err, nil) {
  196. return nil, err
  197. }
  198. return p.buf, err
  199. }
  200. // Marshal takes the protocol buffer
  201. // and encodes it into the wire format, writing the result to the
  202. // Buffer.
  203. func (p *Buffer) Marshal(pb Message) error {
  204. // Can the object marshal itself?
  205. if m, ok := pb.(Marshaler); ok {
  206. data, err := m.Marshal()
  207. if err != nil {
  208. return err
  209. }
  210. p.buf = append(p.buf, data...)
  211. return nil
  212. }
  213. t, base, err := getbase(pb)
  214. if structPointer_IsNil(base) {
  215. return ErrNil
  216. }
  217. if err == nil {
  218. err = p.enc_struct(t.Elem(), GetProperties(t.Elem()), base)
  219. }
  220. if collectStats {
  221. stats.Encode++
  222. }
  223. return err
  224. }
  225. // Size returns the encoded size of a protocol buffer.
  226. func Size(pb Message) (n int) {
  227. // Can the object marshal itself? If so, Size is slow.
  228. // TODO: add Size to Marshaler, or add a Sizer interface.
  229. if m, ok := pb.(Marshaler); ok {
  230. b, _ := m.Marshal()
  231. return len(b)
  232. }
  233. t, base, err := getbase(pb)
  234. if structPointer_IsNil(base) {
  235. return 0
  236. }
  237. if err == nil {
  238. n = size_struct(t.Elem(), GetProperties(t.Elem()), base)
  239. }
  240. if collectStats {
  241. stats.Size++
  242. }
  243. return
  244. }
  245. // Individual type encoders.
  246. // Encode a bool.
  247. func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
  248. v := *structPointer_Bool(base, p.field)
  249. if v == nil {
  250. return ErrNil
  251. }
  252. x := 0
  253. if *v {
  254. x = 1
  255. }
  256. o.buf = append(o.buf, p.tagcode...)
  257. p.valEnc(o, uint64(x))
  258. return nil
  259. }
  260. func size_bool(p *Properties, base structPointer) int {
  261. v := *structPointer_Bool(base, p.field)
  262. if v == nil {
  263. return 0
  264. }
  265. return len(p.tagcode) + 1 // each bool takes exactly one byte
  266. }
  267. // Encode an int32.
  268. func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
  269. v := structPointer_Word32(base, p.field)
  270. if word32_IsNil(v) {
  271. return ErrNil
  272. }
  273. x := word32_Get(v)
  274. o.buf = append(o.buf, p.tagcode...)
  275. p.valEnc(o, uint64(x))
  276. return nil
  277. }
  278. func size_int32(p *Properties, base structPointer) (n int) {
  279. v := structPointer_Word32(base, p.field)
  280. if word32_IsNil(v) {
  281. return 0
  282. }
  283. x := word32_Get(v)
  284. n += len(p.tagcode)
  285. n += p.valSize(uint64(x))
  286. return
  287. }
  288. // Encode an int64.
  289. func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
  290. v := structPointer_Word64(base, p.field)
  291. if word64_IsNil(v) {
  292. return ErrNil
  293. }
  294. x := word64_Get(v)
  295. o.buf = append(o.buf, p.tagcode...)
  296. p.valEnc(o, x)
  297. return nil
  298. }
  299. func size_int64(p *Properties, base structPointer) (n int) {
  300. v := structPointer_Word64(base, p.field)
  301. if word64_IsNil(v) {
  302. return 0
  303. }
  304. x := word64_Get(v)
  305. n += len(p.tagcode)
  306. n += p.valSize(x)
  307. return
  308. }
  309. // Encode a string.
  310. func (o *Buffer) enc_string(p *Properties, base structPointer) error {
  311. v := *structPointer_String(base, p.field)
  312. if v == nil {
  313. return ErrNil
  314. }
  315. x := *v
  316. o.buf = append(o.buf, p.tagcode...)
  317. o.EncodeStringBytes(x)
  318. return nil
  319. }
  320. func size_string(p *Properties, base structPointer) (n int) {
  321. v := *structPointer_String(base, p.field)
  322. if v == nil {
  323. return 0
  324. }
  325. x := *v
  326. n += len(p.tagcode)
  327. n += sizeStringBytes(x)
  328. return
  329. }
  330. // All protocol buffer fields are nillable, but be careful.
  331. func isNil(v reflect.Value) bool {
  332. switch v.Kind() {
  333. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  334. return v.IsNil()
  335. }
  336. return false
  337. }
  338. // Encode a message struct.
  339. func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
  340. var state errorState
  341. structp := structPointer_GetStructPointer(base, p.field)
  342. if structPointer_IsNil(structp) {
  343. return ErrNil
  344. }
  345. // Can the object marshal itself?
  346. if p.isMarshaler {
  347. m := structPointer_Interface(structp, p.stype).(Marshaler)
  348. data, err := m.Marshal()
  349. if err != nil && !state.shouldContinue(err, nil) {
  350. return err
  351. }
  352. o.buf = append(o.buf, p.tagcode...)
  353. o.EncodeRawBytes(data)
  354. return nil
  355. }
  356. // need the length before we can write out the message itself,
  357. // so marshal into a separate byte buffer first.
  358. obuf := o.buf
  359. o.buf = o.bufalloc()
  360. err := o.enc_struct(p.stype, p.sprop, structp)
  361. nbuf := o.buf
  362. o.buf = obuf
  363. if err != nil && !state.shouldContinue(err, nil) {
  364. o.buffree(nbuf)
  365. return err
  366. }
  367. o.buf = append(o.buf, p.tagcode...)
  368. o.EncodeRawBytes(nbuf)
  369. o.buffree(nbuf)
  370. return state.err
  371. }
  372. func size_struct_message(p *Properties, base structPointer) int {
  373. structp := structPointer_GetStructPointer(base, p.field)
  374. if structPointer_IsNil(structp) {
  375. return 0
  376. }
  377. // Can the object marshal itself?
  378. if p.isMarshaler {
  379. m := structPointer_Interface(structp, p.stype).(Marshaler)
  380. data, _ := m.Marshal()
  381. n0 := len(p.tagcode)
  382. n1 := sizeRawBytes(data)
  383. return n0 + n1
  384. }
  385. n0 := len(p.tagcode)
  386. n1 := size_struct(p.stype, p.sprop, structp)
  387. n2 := sizeVarint(uint64(n1)) // size of encoded length
  388. return n0 + n1 + n2
  389. }
  390. // Encode a group struct.
  391. func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
  392. var state errorState
  393. b := structPointer_GetStructPointer(base, p.field)
  394. if structPointer_IsNil(b) {
  395. return ErrNil
  396. }
  397. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  398. err := o.enc_struct(p.stype, p.sprop, b)
  399. if err != nil && !state.shouldContinue(err, nil) {
  400. return err
  401. }
  402. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  403. return state.err
  404. }
  405. func size_struct_group(p *Properties, base structPointer) (n int) {
  406. b := structPointer_GetStructPointer(base, p.field)
  407. if structPointer_IsNil(b) {
  408. return 0
  409. }
  410. n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
  411. n += size_struct(p.stype, p.sprop, b)
  412. n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
  413. return
  414. }
  415. // Encode a slice of bools ([]bool).
  416. func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
  417. s := *structPointer_BoolSlice(base, p.field)
  418. l := len(s)
  419. if l == 0 {
  420. return ErrNil
  421. }
  422. for _, x := range s {
  423. o.buf = append(o.buf, p.tagcode...)
  424. v := uint64(0)
  425. if x {
  426. v = 1
  427. }
  428. p.valEnc(o, v)
  429. }
  430. return nil
  431. }
  432. func size_slice_bool(p *Properties, base structPointer) int {
  433. s := *structPointer_BoolSlice(base, p.field)
  434. l := len(s)
  435. if l == 0 {
  436. return 0
  437. }
  438. return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
  439. }
  440. // Encode a slice of bools ([]bool) in packed format.
  441. func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
  442. s := *structPointer_BoolSlice(base, p.field)
  443. l := len(s)
  444. if l == 0 {
  445. return ErrNil
  446. }
  447. o.buf = append(o.buf, p.tagcode...)
  448. o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
  449. for _, x := range s {
  450. v := uint64(0)
  451. if x {
  452. v = 1
  453. }
  454. p.valEnc(o, v)
  455. }
  456. return nil
  457. }
  458. func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
  459. s := *structPointer_BoolSlice(base, p.field)
  460. l := len(s)
  461. if l == 0 {
  462. return 0
  463. }
  464. n += len(p.tagcode)
  465. n += sizeVarint(uint64(l))
  466. n += l // each bool takes exactly one byte
  467. return
  468. }
  469. // Encode a slice of bytes ([]byte).
  470. func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
  471. s := *structPointer_Bytes(base, p.field)
  472. if s == nil {
  473. return ErrNil
  474. }
  475. o.buf = append(o.buf, p.tagcode...)
  476. o.EncodeRawBytes(s)
  477. return nil
  478. }
  479. func size_slice_byte(p *Properties, base structPointer) (n int) {
  480. s := *structPointer_Bytes(base, p.field)
  481. if s == nil {
  482. return 0
  483. }
  484. n += len(p.tagcode)
  485. n += sizeRawBytes(s)
  486. return
  487. }
  488. // Encode a slice of int32s ([]int32).
  489. func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
  490. s := structPointer_Word32Slice(base, p.field)
  491. l := s.Len()
  492. if l == 0 {
  493. return ErrNil
  494. }
  495. for i := 0; i < l; i++ {
  496. o.buf = append(o.buf, p.tagcode...)
  497. x := s.Index(i)
  498. p.valEnc(o, uint64(x))
  499. }
  500. return nil
  501. }
  502. func size_slice_int32(p *Properties, base structPointer) (n int) {
  503. s := structPointer_Word32Slice(base, p.field)
  504. l := s.Len()
  505. if l == 0 {
  506. return 0
  507. }
  508. for i := 0; i < l; i++ {
  509. n += len(p.tagcode)
  510. x := s.Index(i)
  511. n += p.valSize(uint64(x))
  512. }
  513. return
  514. }
  515. // Encode a slice of int32s ([]int32) in packed format.
  516. func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
  517. s := structPointer_Word32Slice(base, p.field)
  518. l := s.Len()
  519. if l == 0 {
  520. return ErrNil
  521. }
  522. // TODO: Reuse a Buffer.
  523. buf := NewBuffer(nil)
  524. for i := 0; i < l; i++ {
  525. p.valEnc(buf, uint64(s.Index(i)))
  526. }
  527. o.buf = append(o.buf, p.tagcode...)
  528. o.EncodeVarint(uint64(len(buf.buf)))
  529. o.buf = append(o.buf, buf.buf...)
  530. return nil
  531. }
  532. func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
  533. s := structPointer_Word32Slice(base, p.field)
  534. l := s.Len()
  535. if l == 0 {
  536. return 0
  537. }
  538. var bufSize int
  539. for i := 0; i < l; i++ {
  540. bufSize += p.valSize(uint64(s.Index(i)))
  541. }
  542. n += len(p.tagcode)
  543. n += sizeVarint(uint64(bufSize))
  544. n += bufSize
  545. return
  546. }
  547. // Encode a slice of int64s ([]int64).
  548. func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
  549. s := structPointer_Word64Slice(base, p.field)
  550. l := s.Len()
  551. if l == 0 {
  552. return ErrNil
  553. }
  554. for i := 0; i < l; i++ {
  555. o.buf = append(o.buf, p.tagcode...)
  556. p.valEnc(o, s.Index(i))
  557. }
  558. return nil
  559. }
  560. func size_slice_int64(p *Properties, base structPointer) (n int) {
  561. s := structPointer_Word64Slice(base, p.field)
  562. l := s.Len()
  563. if l == 0 {
  564. return 0
  565. }
  566. for i := 0; i < l; i++ {
  567. n += len(p.tagcode)
  568. n += p.valSize(s.Index(i))
  569. }
  570. return
  571. }
  572. // Encode a slice of int64s ([]int64) in packed format.
  573. func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
  574. s := structPointer_Word64Slice(base, p.field)
  575. l := s.Len()
  576. if l == 0 {
  577. return ErrNil
  578. }
  579. // TODO: Reuse a Buffer.
  580. buf := NewBuffer(nil)
  581. for i := 0; i < l; i++ {
  582. p.valEnc(buf, s.Index(i))
  583. }
  584. o.buf = append(o.buf, p.tagcode...)
  585. o.EncodeVarint(uint64(len(buf.buf)))
  586. o.buf = append(o.buf, buf.buf...)
  587. return nil
  588. }
  589. func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
  590. s := structPointer_Word64Slice(base, p.field)
  591. l := s.Len()
  592. if l == 0 {
  593. return 0
  594. }
  595. var bufSize int
  596. for i := 0; i < l; i++ {
  597. bufSize += p.valSize(s.Index(i))
  598. }
  599. n += len(p.tagcode)
  600. n += sizeVarint(uint64(bufSize))
  601. n += bufSize
  602. return
  603. }
  604. // Encode a slice of slice of bytes ([][]byte).
  605. func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
  606. ss := *structPointer_BytesSlice(base, p.field)
  607. l := len(ss)
  608. if l == 0 {
  609. return ErrNil
  610. }
  611. for i := 0; i < l; i++ {
  612. o.buf = append(o.buf, p.tagcode...)
  613. o.EncodeRawBytes(ss[i])
  614. }
  615. return nil
  616. }
  617. func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
  618. ss := *structPointer_BytesSlice(base, p.field)
  619. l := len(ss)
  620. if l == 0 {
  621. return 0
  622. }
  623. n += l * len(p.tagcode)
  624. for i := 0; i < l; i++ {
  625. n += sizeRawBytes(ss[i])
  626. }
  627. return
  628. }
  629. // Encode a slice of strings ([]string).
  630. func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
  631. ss := *structPointer_StringSlice(base, p.field)
  632. l := len(ss)
  633. for i := 0; i < l; i++ {
  634. o.buf = append(o.buf, p.tagcode...)
  635. o.EncodeStringBytes(ss[i])
  636. }
  637. return nil
  638. }
  639. func size_slice_string(p *Properties, base structPointer) (n int) {
  640. ss := *structPointer_StringSlice(base, p.field)
  641. l := len(ss)
  642. n += l * len(p.tagcode)
  643. for i := 0; i < l; i++ {
  644. n += sizeStringBytes(ss[i])
  645. }
  646. return
  647. }
  648. // Encode a slice of message structs ([]*struct).
  649. func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
  650. var state errorState
  651. s := structPointer_StructPointerSlice(base, p.field)
  652. l := s.Len()
  653. for i := 0; i < l; i++ {
  654. structp := s.Index(i)
  655. if structPointer_IsNil(structp) {
  656. return ErrRepeatedHasNil
  657. }
  658. // Can the object marshal itself?
  659. if p.isMarshaler {
  660. m := structPointer_Interface(structp, p.stype).(Marshaler)
  661. data, err := m.Marshal()
  662. if err != nil && !state.shouldContinue(err, nil) {
  663. return err
  664. }
  665. o.buf = append(o.buf, p.tagcode...)
  666. o.EncodeRawBytes(data)
  667. continue
  668. }
  669. obuf := o.buf
  670. o.buf = o.bufalloc()
  671. err := o.enc_struct(p.stype, p.sprop, structp)
  672. nbuf := o.buf
  673. o.buf = obuf
  674. if err != nil && !state.shouldContinue(err, nil) {
  675. o.buffree(nbuf)
  676. if err == ErrNil {
  677. return ErrRepeatedHasNil
  678. }
  679. return err
  680. }
  681. o.buf = append(o.buf, p.tagcode...)
  682. o.EncodeRawBytes(nbuf)
  683. o.buffree(nbuf)
  684. }
  685. return state.err
  686. }
  687. func size_slice_struct_message(p *Properties, base structPointer) (n int) {
  688. s := structPointer_StructPointerSlice(base, p.field)
  689. l := s.Len()
  690. n += l * len(p.tagcode)
  691. for i := 0; i < l; i++ {
  692. structp := s.Index(i)
  693. if structPointer_IsNil(structp) {
  694. return // return the size up to this point
  695. }
  696. // Can the object marshal itself?
  697. if p.isMarshaler {
  698. m := structPointer_Interface(structp, p.stype).(Marshaler)
  699. data, _ := m.Marshal()
  700. n += len(p.tagcode)
  701. n += sizeRawBytes(data)
  702. continue
  703. }
  704. n0 := size_struct(p.stype, p.sprop, structp)
  705. n1 := sizeVarint(uint64(n0)) // size of encoded length
  706. n += n0 + n1
  707. }
  708. return
  709. }
  710. // Encode a slice of group structs ([]*struct).
  711. func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
  712. var state errorState
  713. s := structPointer_StructPointerSlice(base, p.field)
  714. l := s.Len()
  715. for i := 0; i < l; i++ {
  716. b := s.Index(i)
  717. if structPointer_IsNil(b) {
  718. return ErrRepeatedHasNil
  719. }
  720. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  721. err := o.enc_struct(p.stype, p.sprop, b)
  722. if err != nil && !state.shouldContinue(err, nil) {
  723. if err == ErrNil {
  724. return ErrRepeatedHasNil
  725. }
  726. return err
  727. }
  728. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  729. }
  730. return state.err
  731. }
  732. func size_slice_struct_group(p *Properties, base structPointer) (n int) {
  733. s := structPointer_StructPointerSlice(base, p.field)
  734. l := s.Len()
  735. n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
  736. n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
  737. for i := 0; i < l; i++ {
  738. b := s.Index(i)
  739. if structPointer_IsNil(b) {
  740. return // return size up to this point
  741. }
  742. n += size_struct(p.stype, p.sprop, b)
  743. }
  744. return
  745. }
  746. // Encode an extension map.
  747. func (o *Buffer) enc_map(p *Properties, base structPointer) error {
  748. v := *structPointer_ExtMap(base, p.field)
  749. if err := encodeExtensionMap(v); err != nil {
  750. return err
  751. }
  752. // Fast-path for common cases: zero or one extensions.
  753. if len(v) <= 1 {
  754. for _, e := range v {
  755. o.buf = append(o.buf, e.enc...)
  756. }
  757. return nil
  758. }
  759. // Sort keys to provide a deterministic encoding.
  760. keys := make([]int, 0, len(v))
  761. for k := range v {
  762. keys = append(keys, int(k))
  763. }
  764. sort.Ints(keys)
  765. for _, k := range keys {
  766. o.buf = append(o.buf, v[int32(k)].enc...)
  767. }
  768. return nil
  769. }
  770. func size_map(p *Properties, base structPointer) int {
  771. v := *structPointer_ExtMap(base, p.field)
  772. return sizeExtensionMap(v)
  773. }
  774. // Encode a struct.
  775. func (o *Buffer) enc_struct(t reflect.Type, prop *StructProperties, base structPointer) error {
  776. var state errorState
  777. // Encode fields in tag order so that decoders may use optimizations
  778. // that depend on the ordering.
  779. // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order
  780. for _, i := range prop.order {
  781. p := prop.Prop[i]
  782. if p.enc != nil {
  783. err := p.enc(o, p, base)
  784. if err != nil {
  785. if err == ErrNil {
  786. if p.Required && state.err == nil {
  787. state.err = &RequiredNotSetError{p.Name}
  788. }
  789. } else if !state.shouldContinue(err, p) {
  790. return err
  791. }
  792. }
  793. }
  794. }
  795. // Add unrecognized fields at the end.
  796. if prop.unrecField.IsValid() {
  797. v := *structPointer_Bytes(base, prop.unrecField)
  798. if len(v) > 0 {
  799. o.buf = append(o.buf, v...)
  800. }
  801. }
  802. return state.err
  803. }
  804. func size_struct(t reflect.Type, prop *StructProperties, base structPointer) (n int) {
  805. for _, i := range prop.order {
  806. p := prop.Prop[i]
  807. if p.size != nil {
  808. n += p.size(p, base)
  809. }
  810. }
  811. // Add unrecognized fields at the end.
  812. if prop.unrecField.IsValid() {
  813. v := *structPointer_Bytes(base, prop.unrecField)
  814. n += len(v)
  815. }
  816. return
  817. }
  818. // errorState maintains the first error that occurs and updates that error
  819. // with additional context.
  820. type errorState struct {
  821. err error
  822. }
  823. // shouldContinue reports whether encoding should continue upon encountering the
  824. // given error. If the error is RequiredNotSetError, shouldContinue returns true
  825. // and, if this is the first appearance of that error, remembers it for future
  826. // reporting.
  827. //
  828. // If prop is not nil, it may update any error with additional context about the
  829. // field with the error.
  830. func (s *errorState) shouldContinue(err error, prop *Properties) bool {
  831. // Ignore unset required fields.
  832. reqNotSet, ok := err.(*RequiredNotSetError)
  833. if !ok {
  834. return false
  835. }
  836. if s.err == nil {
  837. if prop != nil {
  838. err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
  839. }
  840. s.err = err
  841. }
  842. return true
  843. }