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