encode.go 33 KB

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