encode.go 33 KB

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