encode.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  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. // errOneofHasNil is the error returned if Marshal is called with
  61. // a struct with a oneof field containing a nil element.
  62. errOneofHasNil = errors.New("proto: oneof field has nil value")
  63. // ErrNil is the error returned if Marshal is called with nil.
  64. ErrNil = errors.New("proto: Marshal called with nil")
  65. // ErrTooLarge is the error returned if Marshal is called with a
  66. // message that encodes to >2GB.
  67. ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
  68. )
  69. // The fundamental encoders that put bytes on the wire.
  70. // Those that take integer types all accept uint64 and are
  71. // therefore of type valueEncoder.
  72. const maxVarintBytes = 10 // maximum length of a varint
  73. // maxMarshalSize is the largest allowed size of an encoded protobuf,
  74. // since C++ and Java use signed int32s for the size.
  75. const maxMarshalSize = 1<<31 - 1
  76. // EncodeVarint returns the varint encoding of x.
  77. // This is the format for the
  78. // int32, int64, uint32, uint64, bool, and enum
  79. // protocol buffer types.
  80. // Not used by the package itself, but helpful to clients
  81. // wishing to use the same encoding.
  82. func EncodeVarint(x uint64) []byte {
  83. var buf [maxVarintBytes]byte
  84. var n int
  85. for n = 0; x > 127; n++ {
  86. buf[n] = 0x80 | uint8(x&0x7F)
  87. x >>= 7
  88. }
  89. buf[n] = uint8(x)
  90. n++
  91. return buf[0:n]
  92. }
  93. // EncodeVarint writes a varint-encoded integer to the Buffer.
  94. // This is the format for the
  95. // int32, int64, uint32, uint64, bool, and enum
  96. // protocol buffer types.
  97. func (p *Buffer) EncodeVarint(x uint64) error {
  98. for x >= 1<<7 {
  99. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  100. x >>= 7
  101. }
  102. p.buf = append(p.buf, uint8(x))
  103. return nil
  104. }
  105. // SizeVarint returns the varint encoding size of an integer.
  106. func SizeVarint(x uint64) int {
  107. return sizeVarint(x)
  108. }
  109. func sizeVarint(x uint64) (n int) {
  110. for {
  111. n++
  112. x >>= 7
  113. if x == 0 {
  114. break
  115. }
  116. }
  117. return n
  118. }
  119. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  120. // This is the format for the
  121. // fixed64, sfixed64, and double protocol buffer types.
  122. func (p *Buffer) EncodeFixed64(x uint64) error {
  123. p.buf = append(p.buf,
  124. uint8(x),
  125. uint8(x>>8),
  126. uint8(x>>16),
  127. uint8(x>>24),
  128. uint8(x>>32),
  129. uint8(x>>40),
  130. uint8(x>>48),
  131. uint8(x>>56))
  132. return nil
  133. }
  134. func sizeFixed64(x uint64) int {
  135. return 8
  136. }
  137. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  138. // This is the format for the
  139. // fixed32, sfixed32, and float protocol buffer types.
  140. func (p *Buffer) EncodeFixed32(x uint64) error {
  141. p.buf = append(p.buf,
  142. uint8(x),
  143. uint8(x>>8),
  144. uint8(x>>16),
  145. uint8(x>>24))
  146. return nil
  147. }
  148. func sizeFixed32(x uint64) int {
  149. return 4
  150. }
  151. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  152. // to the Buffer.
  153. // This is the format used for the sint64 protocol buffer type.
  154. func (p *Buffer) EncodeZigzag64(x uint64) error {
  155. // use signed number to get arithmetic right shift.
  156. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  157. }
  158. func sizeZigzag64(x uint64) int {
  159. return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  160. }
  161. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  162. // to the Buffer.
  163. // This is the format used for the sint32 protocol buffer type.
  164. func (p *Buffer) EncodeZigzag32(x uint64) error {
  165. // use signed number to get arithmetic right shift.
  166. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  167. }
  168. func sizeZigzag32(x uint64) int {
  169. return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  170. }
  171. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  172. // This is the format used for the bytes protocol buffer
  173. // type and for embedded messages.
  174. func (p *Buffer) EncodeRawBytes(b []byte) error {
  175. p.EncodeVarint(uint64(len(b)))
  176. p.buf = append(p.buf, b...)
  177. return nil
  178. }
  179. func sizeRawBytes(b []byte) int {
  180. return sizeVarint(uint64(len(b))) +
  181. len(b)
  182. }
  183. // EncodeStringBytes writes an encoded string to the Buffer.
  184. // This is the format used for the proto2 string type.
  185. func (p *Buffer) EncodeStringBytes(s string) error {
  186. p.EncodeVarint(uint64(len(s)))
  187. p.buf = append(p.buf, s...)
  188. return nil
  189. }
  190. func sizeStringBytes(s string) int {
  191. return sizeVarint(uint64(len(s))) +
  192. len(s)
  193. }
  194. // Marshaler is the interface representing objects that can marshal themselves.
  195. type Marshaler interface {
  196. Marshal() ([]byte, error)
  197. }
  198. // Marshal takes the protocol buffer
  199. // and encodes it into the wire format, returning the data.
  200. func Marshal(pb Message) ([]byte, error) {
  201. // Can the object marshal itself?
  202. if m, ok := pb.(Marshaler); ok {
  203. return m.Marshal()
  204. }
  205. p := NewBuffer(nil)
  206. err := p.Marshal(pb)
  207. var state errorState
  208. if err != nil && !state.shouldContinue(err, nil) {
  209. return nil, err
  210. }
  211. if p.buf == nil && err == nil {
  212. // Return a non-nil slice on success.
  213. return []byte{}, nil
  214. }
  215. return p.buf, err
  216. }
  217. // EncodeMessage writes the protocol buffer to the Buffer,
  218. // prefixed by a varint-encoded length.
  219. func (p *Buffer) EncodeMessage(pb Message) error {
  220. t, base, err := getbase(pb)
  221. if structPointer_IsNil(base) {
  222. return ErrNil
  223. }
  224. if err == nil {
  225. var state errorState
  226. err = p.enc_len_struct(GetProperties(t.Elem()), base, &state)
  227. }
  228. return err
  229. }
  230. // Marshal takes the protocol buffer
  231. // and encodes it into the wire format, writing the result to the
  232. // Buffer.
  233. func (p *Buffer) Marshal(pb Message) error {
  234. // Can the object marshal itself?
  235. if m, ok := pb.(Marshaler); ok {
  236. data, err := m.Marshal()
  237. if err != nil {
  238. return err
  239. }
  240. p.buf = append(p.buf, data...)
  241. return nil
  242. }
  243. t, base, err := getbase(pb)
  244. if structPointer_IsNil(base) {
  245. return ErrNil
  246. }
  247. if err == nil {
  248. err = p.enc_struct(GetProperties(t.Elem()), base)
  249. }
  250. if collectStats {
  251. stats.Encode++
  252. }
  253. if len(p.buf) > maxMarshalSize {
  254. return ErrTooLarge
  255. }
  256. return err
  257. }
  258. // Size returns the encoded size of a protocol buffer.
  259. func Size(pb Message) (n int) {
  260. // Can the object marshal itself? If so, Size is slow.
  261. // TODO: add Size to Marshaler, or add a Sizer interface.
  262. if m, ok := pb.(Marshaler); ok {
  263. b, _ := m.Marshal()
  264. return len(b)
  265. }
  266. t, base, err := getbase(pb)
  267. if structPointer_IsNil(base) {
  268. return 0
  269. }
  270. if err == nil {
  271. n = size_struct(GetProperties(t.Elem()), base)
  272. }
  273. if collectStats {
  274. stats.Size++
  275. }
  276. return
  277. }
  278. // Individual type encoders.
  279. // Encode a bool.
  280. func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
  281. v := *structPointer_Bool(base, p.field)
  282. if v == nil {
  283. return ErrNil
  284. }
  285. x := 0
  286. if *v {
  287. x = 1
  288. }
  289. o.buf = append(o.buf, p.tagcode...)
  290. p.valEnc(o, uint64(x))
  291. return nil
  292. }
  293. func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
  294. v := *structPointer_BoolVal(base, p.field)
  295. if !v {
  296. return ErrNil
  297. }
  298. o.buf = append(o.buf, p.tagcode...)
  299. p.valEnc(o, 1)
  300. return nil
  301. }
  302. func size_bool(p *Properties, base structPointer) int {
  303. v := *structPointer_Bool(base, p.field)
  304. if v == nil {
  305. return 0
  306. }
  307. return len(p.tagcode) + 1 // each bool takes exactly one byte
  308. }
  309. func size_proto3_bool(p *Properties, base structPointer) int {
  310. v := *structPointer_BoolVal(base, p.field)
  311. if !v && !p.oneof {
  312. return 0
  313. }
  314. return len(p.tagcode) + 1 // each bool takes exactly one byte
  315. }
  316. // Encode an int32.
  317. func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
  318. v := structPointer_Word32(base, p.field)
  319. if word32_IsNil(v) {
  320. return ErrNil
  321. }
  322. x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
  323. o.buf = append(o.buf, p.tagcode...)
  324. p.valEnc(o, uint64(x))
  325. return nil
  326. }
  327. func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
  328. v := structPointer_Word32Val(base, p.field)
  329. x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
  330. if x == 0 {
  331. return ErrNil
  332. }
  333. o.buf = append(o.buf, p.tagcode...)
  334. p.valEnc(o, uint64(x))
  335. return nil
  336. }
  337. func size_int32(p *Properties, base structPointer) (n int) {
  338. v := structPointer_Word32(base, p.field)
  339. if word32_IsNil(v) {
  340. return 0
  341. }
  342. x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
  343. n += len(p.tagcode)
  344. n += p.valSize(uint64(x))
  345. return
  346. }
  347. func size_proto3_int32(p *Properties, base structPointer) (n int) {
  348. v := structPointer_Word32Val(base, p.field)
  349. x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
  350. if x == 0 && !p.oneof {
  351. return 0
  352. }
  353. n += len(p.tagcode)
  354. n += p.valSize(uint64(x))
  355. return
  356. }
  357. // Encode a uint32.
  358. // Exactly the same as int32, except for no sign extension.
  359. func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
  360. v := structPointer_Word32(base, p.field)
  361. if word32_IsNil(v) {
  362. return ErrNil
  363. }
  364. x := word32_Get(v)
  365. o.buf = append(o.buf, p.tagcode...)
  366. p.valEnc(o, uint64(x))
  367. return nil
  368. }
  369. func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
  370. v := structPointer_Word32Val(base, p.field)
  371. x := word32Val_Get(v)
  372. if x == 0 {
  373. return ErrNil
  374. }
  375. o.buf = append(o.buf, p.tagcode...)
  376. p.valEnc(o, uint64(x))
  377. return nil
  378. }
  379. func size_uint32(p *Properties, base structPointer) (n int) {
  380. v := structPointer_Word32(base, p.field)
  381. if word32_IsNil(v) {
  382. return 0
  383. }
  384. x := word32_Get(v)
  385. n += len(p.tagcode)
  386. n += p.valSize(uint64(x))
  387. return
  388. }
  389. func size_proto3_uint32(p *Properties, base structPointer) (n int) {
  390. v := structPointer_Word32Val(base, p.field)
  391. x := word32Val_Get(v)
  392. if x == 0 && !p.oneof {
  393. return 0
  394. }
  395. n += len(p.tagcode)
  396. n += p.valSize(uint64(x))
  397. return
  398. }
  399. // Encode an int64.
  400. func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
  401. v := structPointer_Word64(base, p.field)
  402. if word64_IsNil(v) {
  403. return ErrNil
  404. }
  405. x := word64_Get(v)
  406. o.buf = append(o.buf, p.tagcode...)
  407. p.valEnc(o, x)
  408. return nil
  409. }
  410. func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
  411. v := structPointer_Word64Val(base, p.field)
  412. x := word64Val_Get(v)
  413. if x == 0 {
  414. return ErrNil
  415. }
  416. o.buf = append(o.buf, p.tagcode...)
  417. p.valEnc(o, x)
  418. return nil
  419. }
  420. func size_int64(p *Properties, base structPointer) (n int) {
  421. v := structPointer_Word64(base, p.field)
  422. if word64_IsNil(v) {
  423. return 0
  424. }
  425. x := word64_Get(v)
  426. n += len(p.tagcode)
  427. n += p.valSize(x)
  428. return
  429. }
  430. func size_proto3_int64(p *Properties, base structPointer) (n int) {
  431. v := structPointer_Word64Val(base, p.field)
  432. x := word64Val_Get(v)
  433. if x == 0 && !p.oneof {
  434. return 0
  435. }
  436. n += len(p.tagcode)
  437. n += p.valSize(x)
  438. return
  439. }
  440. // Encode a string.
  441. func (o *Buffer) enc_string(p *Properties, base structPointer) error {
  442. v := *structPointer_String(base, p.field)
  443. if v == nil {
  444. return ErrNil
  445. }
  446. x := *v
  447. o.buf = append(o.buf, p.tagcode...)
  448. o.EncodeStringBytes(x)
  449. return nil
  450. }
  451. func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
  452. v := *structPointer_StringVal(base, p.field)
  453. if v == "" {
  454. return ErrNil
  455. }
  456. o.buf = append(o.buf, p.tagcode...)
  457. o.EncodeStringBytes(v)
  458. return nil
  459. }
  460. func size_string(p *Properties, base structPointer) (n int) {
  461. v := *structPointer_String(base, p.field)
  462. if v == nil {
  463. return 0
  464. }
  465. x := *v
  466. n += len(p.tagcode)
  467. n += sizeStringBytes(x)
  468. return
  469. }
  470. func size_proto3_string(p *Properties, base structPointer) (n int) {
  471. v := *structPointer_StringVal(base, p.field)
  472. if v == "" && !p.oneof {
  473. return 0
  474. }
  475. n += len(p.tagcode)
  476. n += sizeStringBytes(v)
  477. return
  478. }
  479. // All protocol buffer fields are nillable, but be careful.
  480. func isNil(v reflect.Value) bool {
  481. switch v.Kind() {
  482. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  483. return v.IsNil()
  484. }
  485. return false
  486. }
  487. // Encode a message struct.
  488. func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
  489. var state errorState
  490. structp := structPointer_GetStructPointer(base, p.field)
  491. if structPointer_IsNil(structp) {
  492. return ErrNil
  493. }
  494. // Can the object marshal itself?
  495. if p.isMarshaler {
  496. m := structPointer_Interface(structp, p.stype).(Marshaler)
  497. data, err := m.Marshal()
  498. if err != nil && !state.shouldContinue(err, nil) {
  499. return err
  500. }
  501. o.buf = append(o.buf, p.tagcode...)
  502. o.EncodeRawBytes(data)
  503. return state.err
  504. }
  505. o.buf = append(o.buf, p.tagcode...)
  506. return o.enc_len_struct(p.sprop, structp, &state)
  507. }
  508. func size_struct_message(p *Properties, base structPointer) int {
  509. structp := structPointer_GetStructPointer(base, p.field)
  510. if structPointer_IsNil(structp) {
  511. return 0
  512. }
  513. // Can the object marshal itself?
  514. if p.isMarshaler {
  515. m := structPointer_Interface(structp, p.stype).(Marshaler)
  516. data, _ := m.Marshal()
  517. n0 := len(p.tagcode)
  518. n1 := sizeRawBytes(data)
  519. return n0 + n1
  520. }
  521. n0 := len(p.tagcode)
  522. n1 := size_struct(p.sprop, structp)
  523. n2 := sizeVarint(uint64(n1)) // size of encoded length
  524. return n0 + n1 + n2
  525. }
  526. // Encode a group struct.
  527. func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
  528. var state errorState
  529. b := structPointer_GetStructPointer(base, p.field)
  530. if structPointer_IsNil(b) {
  531. return ErrNil
  532. }
  533. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  534. err := o.enc_struct(p.sprop, b)
  535. if err != nil && !state.shouldContinue(err, nil) {
  536. return err
  537. }
  538. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  539. return state.err
  540. }
  541. func size_struct_group(p *Properties, base structPointer) (n int) {
  542. b := structPointer_GetStructPointer(base, p.field)
  543. if structPointer_IsNil(b) {
  544. return 0
  545. }
  546. n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
  547. n += size_struct(p.sprop, b)
  548. n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
  549. return
  550. }
  551. // Encode a slice of bools ([]bool).
  552. func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
  553. s := *structPointer_BoolSlice(base, p.field)
  554. l := len(s)
  555. if l == 0 {
  556. return ErrNil
  557. }
  558. for _, x := range s {
  559. o.buf = append(o.buf, p.tagcode...)
  560. v := uint64(0)
  561. if x {
  562. v = 1
  563. }
  564. p.valEnc(o, v)
  565. }
  566. return nil
  567. }
  568. func size_slice_bool(p *Properties, base structPointer) int {
  569. s := *structPointer_BoolSlice(base, p.field)
  570. l := len(s)
  571. if l == 0 {
  572. return 0
  573. }
  574. return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
  575. }
  576. // Encode a slice of bools ([]bool) in packed format.
  577. func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
  578. s := *structPointer_BoolSlice(base, p.field)
  579. l := len(s)
  580. if l == 0 {
  581. return ErrNil
  582. }
  583. o.buf = append(o.buf, p.tagcode...)
  584. o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
  585. for _, x := range s {
  586. v := uint64(0)
  587. if x {
  588. v = 1
  589. }
  590. p.valEnc(o, v)
  591. }
  592. return nil
  593. }
  594. func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
  595. s := *structPointer_BoolSlice(base, p.field)
  596. l := len(s)
  597. if l == 0 {
  598. return 0
  599. }
  600. n += len(p.tagcode)
  601. n += sizeVarint(uint64(l))
  602. n += l // each bool takes exactly one byte
  603. return
  604. }
  605. // Encode a slice of bytes ([]byte).
  606. func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
  607. s := *structPointer_Bytes(base, p.field)
  608. if s == nil {
  609. return ErrNil
  610. }
  611. o.buf = append(o.buf, p.tagcode...)
  612. o.EncodeRawBytes(s)
  613. return nil
  614. }
  615. func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
  616. s := *structPointer_Bytes(base, p.field)
  617. if len(s) == 0 {
  618. return ErrNil
  619. }
  620. o.buf = append(o.buf, p.tagcode...)
  621. o.EncodeRawBytes(s)
  622. return nil
  623. }
  624. func size_slice_byte(p *Properties, base structPointer) (n int) {
  625. s := *structPointer_Bytes(base, p.field)
  626. if s == nil && !p.oneof {
  627. return 0
  628. }
  629. n += len(p.tagcode)
  630. n += sizeRawBytes(s)
  631. return
  632. }
  633. func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
  634. s := *structPointer_Bytes(base, p.field)
  635. if len(s) == 0 && !p.oneof {
  636. return 0
  637. }
  638. n += len(p.tagcode)
  639. n += sizeRawBytes(s)
  640. return
  641. }
  642. // Encode a slice of int32s ([]int32).
  643. func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
  644. s := structPointer_Word32Slice(base, p.field)
  645. l := s.Len()
  646. if l == 0 {
  647. return ErrNil
  648. }
  649. for i := 0; i < l; i++ {
  650. o.buf = append(o.buf, p.tagcode...)
  651. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  652. p.valEnc(o, uint64(x))
  653. }
  654. return nil
  655. }
  656. func size_slice_int32(p *Properties, base structPointer) (n int) {
  657. s := structPointer_Word32Slice(base, p.field)
  658. l := s.Len()
  659. if l == 0 {
  660. return 0
  661. }
  662. for i := 0; i < l; i++ {
  663. n += len(p.tagcode)
  664. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  665. n += p.valSize(uint64(x))
  666. }
  667. return
  668. }
  669. // Encode a slice of int32s ([]int32) in packed format.
  670. func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
  671. s := structPointer_Word32Slice(base, p.field)
  672. l := s.Len()
  673. if l == 0 {
  674. return ErrNil
  675. }
  676. // TODO: Reuse a Buffer.
  677. buf := NewBuffer(nil)
  678. for i := 0; i < l; i++ {
  679. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  680. p.valEnc(buf, uint64(x))
  681. }
  682. o.buf = append(o.buf, p.tagcode...)
  683. o.EncodeVarint(uint64(len(buf.buf)))
  684. o.buf = append(o.buf, buf.buf...)
  685. return nil
  686. }
  687. func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
  688. s := structPointer_Word32Slice(base, p.field)
  689. l := s.Len()
  690. if l == 0 {
  691. return 0
  692. }
  693. var bufSize int
  694. for i := 0; i < l; i++ {
  695. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  696. bufSize += p.valSize(uint64(x))
  697. }
  698. n += len(p.tagcode)
  699. n += sizeVarint(uint64(bufSize))
  700. n += bufSize
  701. return
  702. }
  703. // Encode a slice of uint32s ([]uint32).
  704. // Exactly the same as int32, except for no sign extension.
  705. func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
  706. s := structPointer_Word32Slice(base, p.field)
  707. l := s.Len()
  708. if l == 0 {
  709. return ErrNil
  710. }
  711. for i := 0; i < l; i++ {
  712. o.buf = append(o.buf, p.tagcode...)
  713. x := s.Index(i)
  714. p.valEnc(o, uint64(x))
  715. }
  716. return nil
  717. }
  718. func size_slice_uint32(p *Properties, base structPointer) (n int) {
  719. s := structPointer_Word32Slice(base, p.field)
  720. l := s.Len()
  721. if l == 0 {
  722. return 0
  723. }
  724. for i := 0; i < l; i++ {
  725. n += len(p.tagcode)
  726. x := s.Index(i)
  727. n += p.valSize(uint64(x))
  728. }
  729. return
  730. }
  731. // Encode a slice of uint32s ([]uint32) in packed format.
  732. // Exactly the same as int32, except for no sign extension.
  733. func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
  734. s := structPointer_Word32Slice(base, p.field)
  735. l := s.Len()
  736. if l == 0 {
  737. return ErrNil
  738. }
  739. // TODO: Reuse a Buffer.
  740. buf := NewBuffer(nil)
  741. for i := 0; i < l; i++ {
  742. p.valEnc(buf, uint64(s.Index(i)))
  743. }
  744. o.buf = append(o.buf, p.tagcode...)
  745. o.EncodeVarint(uint64(len(buf.buf)))
  746. o.buf = append(o.buf, buf.buf...)
  747. return nil
  748. }
  749. func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
  750. s := structPointer_Word32Slice(base, p.field)
  751. l := s.Len()
  752. if l == 0 {
  753. return 0
  754. }
  755. var bufSize int
  756. for i := 0; i < l; i++ {
  757. bufSize += p.valSize(uint64(s.Index(i)))
  758. }
  759. n += len(p.tagcode)
  760. n += sizeVarint(uint64(bufSize))
  761. n += bufSize
  762. return
  763. }
  764. // Encode a slice of int64s ([]int64).
  765. func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
  766. s := structPointer_Word64Slice(base, p.field)
  767. l := s.Len()
  768. if l == 0 {
  769. return ErrNil
  770. }
  771. for i := 0; i < l; i++ {
  772. o.buf = append(o.buf, p.tagcode...)
  773. p.valEnc(o, s.Index(i))
  774. }
  775. return nil
  776. }
  777. func size_slice_int64(p *Properties, base structPointer) (n int) {
  778. s := structPointer_Word64Slice(base, p.field)
  779. l := s.Len()
  780. if l == 0 {
  781. return 0
  782. }
  783. for i := 0; i < l; i++ {
  784. n += len(p.tagcode)
  785. n += p.valSize(s.Index(i))
  786. }
  787. return
  788. }
  789. // Encode a slice of int64s ([]int64) in packed format.
  790. func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
  791. s := structPointer_Word64Slice(base, p.field)
  792. l := s.Len()
  793. if l == 0 {
  794. return ErrNil
  795. }
  796. // TODO: Reuse a Buffer.
  797. buf := NewBuffer(nil)
  798. for i := 0; i < l; i++ {
  799. p.valEnc(buf, s.Index(i))
  800. }
  801. o.buf = append(o.buf, p.tagcode...)
  802. o.EncodeVarint(uint64(len(buf.buf)))
  803. o.buf = append(o.buf, buf.buf...)
  804. return nil
  805. }
  806. func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
  807. s := structPointer_Word64Slice(base, p.field)
  808. l := s.Len()
  809. if l == 0 {
  810. return 0
  811. }
  812. var bufSize int
  813. for i := 0; i < l; i++ {
  814. bufSize += p.valSize(s.Index(i))
  815. }
  816. n += len(p.tagcode)
  817. n += sizeVarint(uint64(bufSize))
  818. n += bufSize
  819. return
  820. }
  821. // Encode a slice of slice of bytes ([][]byte).
  822. func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
  823. ss := *structPointer_BytesSlice(base, p.field)
  824. l := len(ss)
  825. if l == 0 {
  826. return ErrNil
  827. }
  828. for i := 0; i < l; i++ {
  829. o.buf = append(o.buf, p.tagcode...)
  830. o.EncodeRawBytes(ss[i])
  831. }
  832. return nil
  833. }
  834. func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
  835. ss := *structPointer_BytesSlice(base, p.field)
  836. l := len(ss)
  837. if l == 0 {
  838. return 0
  839. }
  840. n += l * len(p.tagcode)
  841. for i := 0; i < l; i++ {
  842. n += sizeRawBytes(ss[i])
  843. }
  844. return
  845. }
  846. // Encode a slice of strings ([]string).
  847. func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
  848. ss := *structPointer_StringSlice(base, p.field)
  849. l := len(ss)
  850. for i := 0; i < l; i++ {
  851. o.buf = append(o.buf, p.tagcode...)
  852. o.EncodeStringBytes(ss[i])
  853. }
  854. return nil
  855. }
  856. func size_slice_string(p *Properties, base structPointer) (n int) {
  857. ss := *structPointer_StringSlice(base, p.field)
  858. l := len(ss)
  859. n += l * len(p.tagcode)
  860. for i := 0; i < l; i++ {
  861. n += sizeStringBytes(ss[i])
  862. }
  863. return
  864. }
  865. // Encode a slice of message structs ([]*struct).
  866. func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
  867. var state errorState
  868. s := structPointer_StructPointerSlice(base, p.field)
  869. l := s.Len()
  870. for i := 0; i < l; i++ {
  871. structp := s.Index(i)
  872. if structPointer_IsNil(structp) {
  873. return errRepeatedHasNil
  874. }
  875. // Can the object marshal itself?
  876. if p.isMarshaler {
  877. m := structPointer_Interface(structp, p.stype).(Marshaler)
  878. data, err := m.Marshal()
  879. if err != nil && !state.shouldContinue(err, nil) {
  880. return err
  881. }
  882. o.buf = append(o.buf, p.tagcode...)
  883. o.EncodeRawBytes(data)
  884. continue
  885. }
  886. o.buf = append(o.buf, p.tagcode...)
  887. err := o.enc_len_struct(p.sprop, structp, &state)
  888. if err != nil && !state.shouldContinue(err, nil) {
  889. if err == ErrNil {
  890. return errRepeatedHasNil
  891. }
  892. return err
  893. }
  894. }
  895. return state.err
  896. }
  897. func size_slice_struct_message(p *Properties, base structPointer) (n int) {
  898. s := structPointer_StructPointerSlice(base, p.field)
  899. l := s.Len()
  900. n += l * len(p.tagcode)
  901. for i := 0; i < l; i++ {
  902. structp := s.Index(i)
  903. if structPointer_IsNil(structp) {
  904. return // return the size up to this point
  905. }
  906. // Can the object marshal itself?
  907. if p.isMarshaler {
  908. m := structPointer_Interface(structp, p.stype).(Marshaler)
  909. data, _ := m.Marshal()
  910. n += len(p.tagcode)
  911. n += sizeRawBytes(data)
  912. continue
  913. }
  914. n0 := size_struct(p.sprop, structp)
  915. n1 := sizeVarint(uint64(n0)) // size of encoded length
  916. n += n0 + n1
  917. }
  918. return
  919. }
  920. // Encode a slice of group structs ([]*struct).
  921. func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
  922. var state errorState
  923. s := structPointer_StructPointerSlice(base, p.field)
  924. l := s.Len()
  925. for i := 0; i < l; i++ {
  926. b := s.Index(i)
  927. if structPointer_IsNil(b) {
  928. return errRepeatedHasNil
  929. }
  930. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  931. err := o.enc_struct(p.sprop, b)
  932. if err != nil && !state.shouldContinue(err, nil) {
  933. if err == ErrNil {
  934. return errRepeatedHasNil
  935. }
  936. return err
  937. }
  938. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  939. }
  940. return state.err
  941. }
  942. func size_slice_struct_group(p *Properties, base structPointer) (n int) {
  943. s := structPointer_StructPointerSlice(base, p.field)
  944. l := s.Len()
  945. n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
  946. n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
  947. for i := 0; i < l; i++ {
  948. b := s.Index(i)
  949. if structPointer_IsNil(b) {
  950. return // return size up to this point
  951. }
  952. n += size_struct(p.sprop, b)
  953. }
  954. return
  955. }
  956. // Encode an extension map.
  957. func (o *Buffer) enc_map(p *Properties, base structPointer) error {
  958. exts := structPointer_ExtMap(base, p.field)
  959. if err := encodeExtensionsMap(*exts); err != nil {
  960. return err
  961. }
  962. return o.enc_map_body(*exts)
  963. }
  964. func (o *Buffer) enc_exts(p *Properties, base structPointer) error {
  965. exts := structPointer_Extensions(base, p.field)
  966. if err := encodeExtensions(exts); err != nil {
  967. return err
  968. }
  969. v, _ := exts.extensionsRead()
  970. return o.enc_map_body(v)
  971. }
  972. func (o *Buffer) enc_map_body(v map[int32]Extension) error {
  973. // Fast-path for common cases: zero or one extensions.
  974. if len(v) <= 1 {
  975. for _, e := range v {
  976. o.buf = append(o.buf, e.enc...)
  977. }
  978. return nil
  979. }
  980. // Sort keys to provide a deterministic encoding.
  981. keys := make([]int, 0, len(v))
  982. for k := range v {
  983. keys = append(keys, int(k))
  984. }
  985. sort.Ints(keys)
  986. for _, k := range keys {
  987. o.buf = append(o.buf, v[int32(k)].enc...)
  988. }
  989. return nil
  990. }
  991. func size_map(p *Properties, base structPointer) int {
  992. v := structPointer_ExtMap(base, p.field)
  993. return extensionsMapSize(*v)
  994. }
  995. func size_exts(p *Properties, base structPointer) int {
  996. v := structPointer_Extensions(base, p.field)
  997. return extensionsSize(v)
  998. }
  999. // Encode a map field.
  1000. func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
  1001. var state errorState // XXX: or do we need to plumb this through?
  1002. /*
  1003. A map defined as
  1004. map<key_type, value_type> map_field = N;
  1005. is encoded in the same way as
  1006. message MapFieldEntry {
  1007. key_type key = 1;
  1008. value_type value = 2;
  1009. }
  1010. repeated MapFieldEntry map_field = N;
  1011. */
  1012. v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1013. if v.Len() == 0 {
  1014. return nil
  1015. }
  1016. keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
  1017. enc := func() error {
  1018. if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
  1019. return err
  1020. }
  1021. if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil && err != ErrNil {
  1022. return err
  1023. }
  1024. return nil
  1025. }
  1026. // Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
  1027. for _, key := range v.MapKeys() {
  1028. val := v.MapIndex(key)
  1029. keycopy.Set(key)
  1030. valcopy.Set(val)
  1031. o.buf = append(o.buf, p.tagcode...)
  1032. if err := o.enc_len_thing(enc, &state); err != nil {
  1033. return err
  1034. }
  1035. }
  1036. return nil
  1037. }
  1038. func size_new_map(p *Properties, base structPointer) int {
  1039. v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1040. keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
  1041. n := 0
  1042. for _, key := range v.MapKeys() {
  1043. val := v.MapIndex(key)
  1044. keycopy.Set(key)
  1045. valcopy.Set(val)
  1046. // Tag codes for key and val are the responsibility of the sub-sizer.
  1047. keysize := p.mkeyprop.size(p.mkeyprop, keybase)
  1048. valsize := p.mvalprop.size(p.mvalprop, valbase)
  1049. entry := keysize + valsize
  1050. // Add on tag code and length of map entry itself.
  1051. n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
  1052. }
  1053. return n
  1054. }
  1055. // mapEncodeScratch returns a new reflect.Value matching the map's value type,
  1056. // and a structPointer suitable for passing to an encoder or sizer.
  1057. func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
  1058. // Prepare addressable doubly-indirect placeholders for the key and value types.
  1059. // This is needed because the element-type encoders expect **T, but the map iteration produces T.
  1060. keycopy = reflect.New(mapType.Key()).Elem() // addressable K
  1061. keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
  1062. keyptr.Set(keycopy.Addr()) //
  1063. keybase = toStructPointer(keyptr.Addr()) // **K
  1064. // Value types are more varied and require special handling.
  1065. switch mapType.Elem().Kind() {
  1066. case reflect.Slice:
  1067. // []byte
  1068. var dummy []byte
  1069. valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
  1070. valbase = toStructPointer(valcopy.Addr())
  1071. case reflect.Ptr:
  1072. // message; the generated field type is map[K]*Msg (so V is *Msg),
  1073. // so we only need one level of indirection.
  1074. valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
  1075. valbase = toStructPointer(valcopy.Addr())
  1076. default:
  1077. // everything else
  1078. valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
  1079. valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
  1080. valptr.Set(valcopy.Addr()) //
  1081. valbase = toStructPointer(valptr.Addr()) // **V
  1082. }
  1083. return
  1084. }
  1085. // Encode a struct.
  1086. func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
  1087. var state errorState
  1088. // Encode fields in tag order so that decoders may use optimizations
  1089. // that depend on the ordering.
  1090. // https://developers.google.com/protocol-buffers/docs/encoding#order
  1091. for _, i := range prop.order {
  1092. p := prop.Prop[i]
  1093. if p.enc != nil {
  1094. err := p.enc(o, p, base)
  1095. if err != nil {
  1096. if err == ErrNil {
  1097. if p.Required && state.err == nil {
  1098. state.err = &RequiredNotSetError{p.Name}
  1099. }
  1100. } else if err == errRepeatedHasNil {
  1101. // Give more context to nil values in repeated fields.
  1102. return errors.New("repeated field " + p.OrigName + " has nil element")
  1103. } else if !state.shouldContinue(err, p) {
  1104. return err
  1105. }
  1106. }
  1107. if len(o.buf) > maxMarshalSize {
  1108. return ErrTooLarge
  1109. }
  1110. }
  1111. }
  1112. // Do oneof fields.
  1113. if prop.oneofMarshaler != nil {
  1114. m := structPointer_Interface(base, prop.stype).(Message)
  1115. if err := prop.oneofMarshaler(m, o); err == ErrNil {
  1116. return errOneofHasNil
  1117. } else if err != nil {
  1118. return err
  1119. }
  1120. }
  1121. // Add unrecognized fields at the end.
  1122. if prop.unrecField.IsValid() {
  1123. v := *structPointer_Bytes(base, prop.unrecField)
  1124. if len(o.buf)+len(v) > maxMarshalSize {
  1125. return ErrTooLarge
  1126. }
  1127. if len(v) > 0 {
  1128. o.buf = append(o.buf, v...)
  1129. }
  1130. }
  1131. return state.err
  1132. }
  1133. func size_struct(prop *StructProperties, base structPointer) (n int) {
  1134. for _, i := range prop.order {
  1135. p := prop.Prop[i]
  1136. if p.size != nil {
  1137. n += p.size(p, base)
  1138. }
  1139. }
  1140. // Add unrecognized fields at the end.
  1141. if prop.unrecField.IsValid() {
  1142. v := *structPointer_Bytes(base, prop.unrecField)
  1143. n += len(v)
  1144. }
  1145. // Factor in any oneof fields.
  1146. if prop.oneofSizer != nil {
  1147. m := structPointer_Interface(base, prop.stype).(Message)
  1148. n += prop.oneofSizer(m)
  1149. }
  1150. return
  1151. }
  1152. var zeroes [20]byte // longer than any conceivable sizeVarint
  1153. // Encode a struct, preceded by its encoded length (as a varint).
  1154. func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
  1155. return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
  1156. }
  1157. // Encode something, preceded by its encoded length (as a varint).
  1158. func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
  1159. iLen := len(o.buf)
  1160. o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
  1161. iMsg := len(o.buf)
  1162. err := enc()
  1163. if err != nil && !state.shouldContinue(err, nil) {
  1164. return err
  1165. }
  1166. lMsg := len(o.buf) - iMsg
  1167. lLen := sizeVarint(uint64(lMsg))
  1168. switch x := lLen - (iMsg - iLen); {
  1169. case x > 0: // actual length is x bytes larger than the space we reserved
  1170. // Move msg x bytes right.
  1171. o.buf = append(o.buf, zeroes[:x]...)
  1172. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  1173. case x < 0: // actual length is x bytes smaller than the space we reserved
  1174. // Move msg x bytes left.
  1175. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  1176. o.buf = o.buf[:len(o.buf)+x] // x is negative
  1177. }
  1178. // Encode the length in the reserved space.
  1179. o.buf = o.buf[:iLen]
  1180. o.EncodeVarint(uint64(lMsg))
  1181. o.buf = o.buf[:len(o.buf)+lMsg]
  1182. return state.err
  1183. }
  1184. // errorState maintains the first error that occurs and updates that error
  1185. // with additional context.
  1186. type errorState struct {
  1187. err error
  1188. }
  1189. // shouldContinue reports whether encoding should continue upon encountering the
  1190. // given error. If the error is RequiredNotSetError, shouldContinue returns true
  1191. // and, if this is the first appearance of that error, remembers it for future
  1192. // reporting.
  1193. //
  1194. // If prop is not nil, it may update any error with additional context about the
  1195. // field with the error.
  1196. func (s *errorState) shouldContinue(err error, prop *Properties) bool {
  1197. // Ignore unset required fields.
  1198. reqNotSet, ok := err.(*RequiredNotSetError)
  1199. if !ok {
  1200. return false
  1201. }
  1202. if s.err == nil {
  1203. if prop != nil {
  1204. err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
  1205. }
  1206. s.err = err
  1207. }
  1208. return true
  1209. }