encode.go 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. /*
  33. * Routines for encoding data into the wire format for protocol buffers.
  34. */
  35. import (
  36. "errors"
  37. "fmt"
  38. "reflect"
  39. "sort"
  40. )
  41. // RequiredNotSetError is the error returned if Marshal is called with
  42. // a protocol buffer struct whose required fields have not
  43. // all been initialized. It is also the error returned if Unmarshal is
  44. // called with an encoded protocol buffer that does not include all the
  45. // required fields.
  46. //
  47. // When printed, RequiredNotSetError reports the first unset required field in a
  48. // message. If the field cannot be precisely determined, it is reported as
  49. // "{Unknown}".
  50. type RequiredNotSetError struct {
  51. field string
  52. }
  53. func (e *RequiredNotSetError) Error() string {
  54. return fmt.Sprintf("proto: required field %q not set", e.field)
  55. }
  56. var (
  57. // errRepeatedHasNil is the error returned if Marshal is called with
  58. // a struct with a repeated field containing a nil element.
  59. errRepeatedHasNil = errors.New("proto: repeated field has nil element")
  60. // errOneofHasNil is the error returned if Marshal is called with
  61. // a struct with a oneof field containing a nil element.
  62. errOneofHasNil = errors.New("proto: oneof field has nil value")
  63. // ErrNil is the error returned if Marshal is called with nil.
  64. ErrNil = errors.New("proto: Marshal called with nil")
  65. // ErrTooLarge is the error returned if Marshal is called with a
  66. // message that encodes to >2GB.
  67. ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
  68. )
  69. // The fundamental encoders that put bytes on the wire.
  70. // Those that take integer types all accept uint64 and are
  71. // therefore of type valueEncoder.
  72. const maxVarintBytes = 10 // maximum length of a varint
  73. // maxMarshalSize is the largest allowed size of an encoded protobuf,
  74. // since C++ and Java use signed int32s for the size.
  75. const maxMarshalSize = 1<<31 - 1
  76. // EncodeVarint returns the varint encoding of x.
  77. // This is the format for the
  78. // int32, int64, uint32, uint64, bool, and enum
  79. // protocol buffer types.
  80. // Not used by the package itself, but helpful to clients
  81. // wishing to use the same encoding.
  82. func EncodeVarint(x uint64) []byte {
  83. var buf [maxVarintBytes]byte
  84. var n int
  85. for n = 0; x > 127; n++ {
  86. buf[n] = 0x80 | uint8(x&0x7F)
  87. x >>= 7
  88. }
  89. buf[n] = uint8(x)
  90. n++
  91. return buf[0:n]
  92. }
  93. // EncodeVarint writes a varint-encoded integer to the Buffer.
  94. // This is the format for the
  95. // int32, int64, uint32, uint64, bool, and enum
  96. // protocol buffer types.
  97. func (p *Buffer) EncodeVarint(x uint64) error {
  98. for x >= 1<<7 {
  99. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  100. x >>= 7
  101. }
  102. p.buf = append(p.buf, uint8(x))
  103. return nil
  104. }
  105. // SizeVarint returns the varint encoding size of an integer.
  106. func SizeVarint(x uint64) int {
  107. return sizeVarint(x)
  108. }
  109. func sizeVarint(x uint64) (n int) {
  110. for {
  111. n++
  112. x >>= 7
  113. if x == 0 {
  114. break
  115. }
  116. }
  117. return n
  118. }
  119. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  120. // This is the format for the
  121. // fixed64, sfixed64, and double protocol buffer types.
  122. func (p *Buffer) EncodeFixed64(x uint64) error {
  123. p.buf = append(p.buf,
  124. uint8(x),
  125. uint8(x>>8),
  126. uint8(x>>16),
  127. uint8(x>>24),
  128. uint8(x>>32),
  129. uint8(x>>40),
  130. uint8(x>>48),
  131. uint8(x>>56))
  132. return nil
  133. }
  134. func sizeFixed64(x uint64) int {
  135. return 8
  136. }
  137. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  138. // This is the format for the
  139. // fixed32, sfixed32, and float protocol buffer types.
  140. func (p *Buffer) EncodeFixed32(x uint64) error {
  141. p.buf = append(p.buf,
  142. uint8(x),
  143. uint8(x>>8),
  144. uint8(x>>16),
  145. uint8(x>>24))
  146. return nil
  147. }
  148. func sizeFixed32(x uint64) int {
  149. return 4
  150. }
  151. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  152. // to the Buffer.
  153. // This is the format used for the sint64 protocol buffer type.
  154. func (p *Buffer) EncodeZigzag64(x uint64) error {
  155. // use signed number to get arithmetic right shift.
  156. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  157. }
  158. func sizeZigzag64(x uint64) int {
  159. return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  160. }
  161. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  162. // to the Buffer.
  163. // This is the format used for the sint32 protocol buffer type.
  164. func (p *Buffer) EncodeZigzag32(x uint64) error {
  165. // use signed number to get arithmetic right shift.
  166. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  167. }
  168. func sizeZigzag32(x uint64) int {
  169. return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  170. }
  171. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  172. // This is the format used for the bytes protocol buffer
  173. // type and for embedded messages.
  174. func (p *Buffer) EncodeRawBytes(b []byte) error {
  175. p.EncodeVarint(uint64(len(b)))
  176. p.buf = append(p.buf, b...)
  177. return nil
  178. }
  179. func sizeRawBytes(b []byte) int {
  180. return sizeVarint(uint64(len(b))) +
  181. len(b)
  182. }
  183. // EncodeStringBytes writes an encoded string to the Buffer.
  184. // This is the format used for the proto2 string type.
  185. func (p *Buffer) EncodeStringBytes(s string) error {
  186. p.EncodeVarint(uint64(len(s)))
  187. p.buf = append(p.buf, s...)
  188. return nil
  189. }
  190. func sizeStringBytes(s string) int {
  191. return sizeVarint(uint64(len(s))) +
  192. len(s)
  193. }
  194. // Marshaler is the interface representing objects that can marshal themselves.
  195. type Marshaler interface {
  196. Marshal() ([]byte, error)
  197. }
  198. // Marshal takes the protocol buffer
  199. // and encodes it into the wire format, returning the data.
  200. func Marshal(pb Message) ([]byte, error) {
  201. // Can the object marshal itself?
  202. if m, ok := pb.(Marshaler); ok {
  203. return m.Marshal()
  204. }
  205. p := NewBuffer(nil)
  206. err := p.Marshal(pb)
  207. 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. if err := encodeExtensions(exts); err != nil {
  959. return err
  960. }
  961. v, _ := exts.extensionsRead()
  962. return o.enc_map_body(v)
  963. }
  964. func (o *Buffer) enc_map_body(v map[int32]Extension) error {
  965. // Fast-path for common cases: zero or one extensions.
  966. if len(v) <= 1 {
  967. for _, e := range v {
  968. o.buf = append(o.buf, e.enc...)
  969. }
  970. return nil
  971. }
  972. // Sort keys to provide a deterministic encoding.
  973. keys := make([]int, 0, len(v))
  974. for k := range v {
  975. keys = append(keys, int(k))
  976. }
  977. sort.Ints(keys)
  978. for _, k := range keys {
  979. o.buf = append(o.buf, v[int32(k)].enc...)
  980. }
  981. return nil
  982. }
  983. func size_map(p *Properties, base structPointer) int {
  984. v := structPointer_ExtMap(base, p.field)
  985. return extensionsMapSize(*v)
  986. }
  987. func size_exts(p *Properties, base structPointer) int {
  988. v := structPointer_Extensions(base, p.field)
  989. return extensionsSize(v)
  990. }
  991. // Encode a map field.
  992. func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
  993. var state errorState // XXX: or do we need to plumb this through?
  994. /*
  995. A map defined as
  996. map<key_type, value_type> map_field = N;
  997. is encoded in the same way as
  998. message MapFieldEntry {
  999. key_type key = 1;
  1000. value_type value = 2;
  1001. }
  1002. repeated MapFieldEntry map_field = N;
  1003. */
  1004. v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1005. if v.Len() == 0 {
  1006. return nil
  1007. }
  1008. keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
  1009. enc := func() error {
  1010. if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
  1011. return err
  1012. }
  1013. if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil && err != ErrNil {
  1014. return err
  1015. }
  1016. return nil
  1017. }
  1018. // Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
  1019. for _, key := range v.MapKeys() {
  1020. val := v.MapIndex(key)
  1021. keycopy.Set(key)
  1022. valcopy.Set(val)
  1023. o.buf = append(o.buf, p.tagcode...)
  1024. if err := o.enc_len_thing(enc, &state); err != nil {
  1025. return err
  1026. }
  1027. }
  1028. return nil
  1029. }
  1030. func size_new_map(p *Properties, base structPointer) int {
  1031. v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1032. keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
  1033. n := 0
  1034. for _, key := range v.MapKeys() {
  1035. val := v.MapIndex(key)
  1036. keycopy.Set(key)
  1037. valcopy.Set(val)
  1038. // Tag codes for key and val are the responsibility of the sub-sizer.
  1039. keysize := p.mkeyprop.size(p.mkeyprop, keybase)
  1040. valsize := p.mvalprop.size(p.mvalprop, valbase)
  1041. entry := keysize + valsize
  1042. // Add on tag code and length of map entry itself.
  1043. n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
  1044. }
  1045. return n
  1046. }
  1047. // mapEncodeScratch returns a new reflect.Value matching the map's value type,
  1048. // and a structPointer suitable for passing to an encoder or sizer.
  1049. func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
  1050. // Prepare addressable doubly-indirect placeholders for the key and value types.
  1051. // This is needed because the element-type encoders expect **T, but the map iteration produces T.
  1052. keycopy = reflect.New(mapType.Key()).Elem() // addressable K
  1053. keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
  1054. keyptr.Set(keycopy.Addr()) //
  1055. keybase = toStructPointer(keyptr.Addr()) // **K
  1056. // Value types are more varied and require special handling.
  1057. switch mapType.Elem().Kind() {
  1058. case reflect.Slice:
  1059. // []byte
  1060. var dummy []byte
  1061. valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
  1062. valbase = toStructPointer(valcopy.Addr())
  1063. case reflect.Ptr:
  1064. // message; the generated field type is map[K]*Msg (so V is *Msg),
  1065. // so we only need one level of indirection.
  1066. valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
  1067. valbase = toStructPointer(valcopy.Addr())
  1068. default:
  1069. // everything else
  1070. valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
  1071. valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
  1072. valptr.Set(valcopy.Addr()) //
  1073. valbase = toStructPointer(valptr.Addr()) // **V
  1074. }
  1075. return
  1076. }
  1077. // Encode a struct.
  1078. func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
  1079. var state errorState
  1080. // Encode fields in tag order so that decoders may use optimizations
  1081. // that depend on the ordering.
  1082. // https://developers.google.com/protocol-buffers/docs/encoding#order
  1083. for _, i := range prop.order {
  1084. p := prop.Prop[i]
  1085. if p.enc != nil {
  1086. err := p.enc(o, p, base)
  1087. if err != nil {
  1088. if err == ErrNil {
  1089. if p.Required && state.err == nil {
  1090. state.err = &RequiredNotSetError{p.Name}
  1091. }
  1092. } else if err == errRepeatedHasNil {
  1093. // Give more context to nil values in repeated fields.
  1094. return errors.New("repeated field " + p.OrigName + " has nil element")
  1095. } else if !state.shouldContinue(err, p) {
  1096. return err
  1097. }
  1098. }
  1099. if len(o.buf) > maxMarshalSize {
  1100. return ErrTooLarge
  1101. }
  1102. }
  1103. }
  1104. // Do oneof fields.
  1105. if prop.oneofMarshaler != nil {
  1106. m := structPointer_Interface(base, prop.stype).(Message)
  1107. if err := prop.oneofMarshaler(m, o); err == ErrNil {
  1108. return errOneofHasNil
  1109. } else if err != nil {
  1110. return err
  1111. }
  1112. }
  1113. // Add unrecognized fields at the end.
  1114. if prop.unrecField.IsValid() {
  1115. v := *structPointer_Bytes(base, prop.unrecField)
  1116. if len(o.buf)+len(v) > maxMarshalSize {
  1117. return ErrTooLarge
  1118. }
  1119. if len(v) > 0 {
  1120. o.buf = append(o.buf, v...)
  1121. }
  1122. }
  1123. return state.err
  1124. }
  1125. func size_struct(prop *StructProperties, base structPointer) (n int) {
  1126. for _, i := range prop.order {
  1127. p := prop.Prop[i]
  1128. if p.size != nil {
  1129. n += p.size(p, base)
  1130. }
  1131. }
  1132. // Add unrecognized fields at the end.
  1133. if prop.unrecField.IsValid() {
  1134. v := *structPointer_Bytes(base, prop.unrecField)
  1135. n += len(v)
  1136. }
  1137. // Factor in any oneof fields.
  1138. if prop.oneofSizer != nil {
  1139. m := structPointer_Interface(base, prop.stype).(Message)
  1140. n += prop.oneofSizer(m)
  1141. }
  1142. return
  1143. }
  1144. var zeroes [20]byte // longer than any conceivable sizeVarint
  1145. // Encode a struct, preceded by its encoded length (as a varint).
  1146. func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
  1147. return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
  1148. }
  1149. // Encode something, preceded by its encoded length (as a varint).
  1150. func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
  1151. iLen := len(o.buf)
  1152. o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
  1153. iMsg := len(o.buf)
  1154. err := enc()
  1155. if err != nil && !state.shouldContinue(err, nil) {
  1156. return err
  1157. }
  1158. lMsg := len(o.buf) - iMsg
  1159. lLen := sizeVarint(uint64(lMsg))
  1160. switch x := lLen - (iMsg - iLen); {
  1161. case x > 0: // actual length is x bytes larger than the space we reserved
  1162. // Move msg x bytes right.
  1163. o.buf = append(o.buf, zeroes[:x]...)
  1164. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  1165. case x < 0: // actual length is x bytes smaller than the space we reserved
  1166. // Move msg x bytes left.
  1167. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  1168. o.buf = o.buf[:len(o.buf)+x] // x is negative
  1169. }
  1170. // Encode the length in the reserved space.
  1171. o.buf = o.buf[:iLen]
  1172. o.EncodeVarint(uint64(lMsg))
  1173. o.buf = o.buf[:len(o.buf)+lMsg]
  1174. return state.err
  1175. }
  1176. // errorState maintains the first error that occurs and updates that error
  1177. // with additional context.
  1178. type errorState struct {
  1179. err error
  1180. }
  1181. // shouldContinue reports whether encoding should continue upon encountering the
  1182. // given error. If the error is RequiredNotSetError, shouldContinue returns true
  1183. // and, if this is the first appearance of that error, remembers it for future
  1184. // reporting.
  1185. //
  1186. // If prop is not nil, it may update any error with additional context about the
  1187. // field with the error.
  1188. func (s *errorState) shouldContinue(err error, prop *Properties) bool {
  1189. // Ignore unset required fields.
  1190. reqNotSet, ok := err.(*RequiredNotSetError)
  1191. if !ok {
  1192. return false
  1193. }
  1194. if s.err == nil {
  1195. if prop != nil {
  1196. err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
  1197. }
  1198. s.err = err
  1199. }
  1200. return true
  1201. }