encode.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  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. }