codec_field.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package impl
  5. import (
  6. "fmt"
  7. "reflect"
  8. "sync"
  9. "google.golang.org/protobuf/internal/encoding/wire"
  10. "google.golang.org/protobuf/proto"
  11. pref "google.golang.org/protobuf/reflect/protoreflect"
  12. preg "google.golang.org/protobuf/reflect/protoregistry"
  13. )
  14. type errInvalidUTF8 struct{}
  15. func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" }
  16. func (errInvalidUTF8) InvalidUTF8() bool { return true }
  17. func makeOneofFieldCoder(fd pref.FieldDescriptor, si structInfo) pointerCoderFuncs {
  18. ot := si.oneofWrappersByNumber[fd.Number()]
  19. funcs := fieldCoder(fd, ot.Field(0).Type)
  20. fs := si.oneofsByName[fd.ContainingOneof().Name()]
  21. ft := fs.Type
  22. wiretag := wire.EncodeTag(fd.Number(), wireTypes[fd.Kind()])
  23. tagsize := wire.SizeVarint(wiretag)
  24. getInfo := func(p pointer) (pointer, bool) {
  25. v := p.AsValueOf(ft).Elem()
  26. if v.IsNil() {
  27. return pointer{}, false
  28. }
  29. v = v.Elem() // interface -> *struct
  30. if v.IsNil() || v.Elem().Type() != ot {
  31. return pointer{}, false
  32. }
  33. return pointerOfValue(v).Apply(zeroOffset), true
  34. }
  35. pcf := pointerCoderFuncs{
  36. size: func(p pointer, _ int, opts marshalOptions) int {
  37. v, ok := getInfo(p)
  38. if !ok {
  39. return 0
  40. }
  41. return funcs.size(v, tagsize, opts)
  42. },
  43. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  44. v, ok := getInfo(p)
  45. if !ok {
  46. return b, nil
  47. }
  48. return funcs.marshal(b, v, wiretag, opts)
  49. },
  50. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  51. var vw reflect.Value // pointer to wrapper type
  52. vi := p.AsValueOf(ft).Elem() // oneof field value of interface kind
  53. if !vi.IsNil() && !vi.Elem().IsNil() && vi.Elem().Elem().Type() == ot {
  54. vw = vi.Elem()
  55. } else {
  56. vw = reflect.New(ot)
  57. }
  58. n, err := funcs.unmarshal(b, pointerOfValue(vw).Apply(zeroOffset), wtyp, opts)
  59. if err != nil {
  60. return 0, err
  61. }
  62. vi.Set(vw)
  63. return n, nil
  64. },
  65. }
  66. if funcs.isInit != nil {
  67. pcf.isInit = func(p pointer) error {
  68. v, ok := getInfo(p)
  69. if !ok {
  70. return nil
  71. }
  72. return funcs.isInit(v)
  73. }
  74. }
  75. return pcf
  76. }
  77. func makeWeakMessageFieldCoder(fd pref.FieldDescriptor) pointerCoderFuncs {
  78. var once sync.Once
  79. var messageType pref.MessageType
  80. lazyInit := func() {
  81. once.Do(func() {
  82. messageName := fd.Message().FullName()
  83. messageType, _ = preg.GlobalTypes.FindMessageByName(messageName)
  84. })
  85. }
  86. num := fd.Number()
  87. return pointerCoderFuncs{
  88. size: func(p pointer, tagsize int, opts marshalOptions) int {
  89. m, ok := p.WeakFields().get(num)
  90. if !ok {
  91. return 0
  92. }
  93. lazyInit()
  94. if messageType == nil {
  95. panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
  96. }
  97. return sizeMessage(m, tagsize, opts)
  98. },
  99. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  100. m, ok := p.WeakFields().get(num)
  101. if !ok {
  102. return b, nil
  103. }
  104. lazyInit()
  105. if messageType == nil {
  106. panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
  107. }
  108. return appendMessage(b, m, wiretag, opts)
  109. },
  110. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  111. fs := p.WeakFields()
  112. m, ok := fs.get(num)
  113. if !ok {
  114. lazyInit()
  115. if messageType == nil {
  116. return 0, errUnknown
  117. }
  118. m = messageType.New().Interface()
  119. fs.set(num, m)
  120. }
  121. return consumeMessage(b, m, wtyp, opts)
  122. },
  123. isInit: func(p pointer) error {
  124. m, ok := p.WeakFields().get(num)
  125. if !ok {
  126. return nil
  127. }
  128. return proto.IsInitialized(m)
  129. },
  130. }
  131. }
  132. func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  133. if mi := getMessageInfo(ft); mi != nil {
  134. return pointerCoderFuncs{
  135. size: func(p pointer, tagsize int, opts marshalOptions) int {
  136. return sizeMessageInfo(p, mi, tagsize, opts)
  137. },
  138. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  139. return appendMessageInfo(b, p, wiretag, mi, opts)
  140. },
  141. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  142. return consumeMessageInfo(b, p, mi, wtyp, opts)
  143. },
  144. isInit: func(p pointer) error {
  145. return mi.isInitializedPointer(p.Elem())
  146. },
  147. }
  148. } else {
  149. return pointerCoderFuncs{
  150. size: func(p pointer, tagsize int, opts marshalOptions) int {
  151. m := asMessage(p.AsValueOf(ft).Elem())
  152. return sizeMessage(m, tagsize, opts)
  153. },
  154. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  155. m := asMessage(p.AsValueOf(ft).Elem())
  156. return appendMessage(b, m, wiretag, opts)
  157. },
  158. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  159. mp := p.AsValueOf(ft).Elem()
  160. if mp.IsNil() {
  161. mp.Set(reflect.New(ft.Elem()))
  162. }
  163. return consumeMessage(b, asMessage(mp), wtyp, opts)
  164. },
  165. isInit: func(p pointer) error {
  166. m := asMessage(p.AsValueOf(ft).Elem())
  167. return proto.IsInitialized(m)
  168. },
  169. }
  170. }
  171. }
  172. func sizeMessageInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
  173. return wire.SizeBytes(mi.sizePointer(p.Elem(), opts)) + tagsize
  174. }
  175. func appendMessageInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
  176. b = wire.AppendVarint(b, wiretag)
  177. b = wire.AppendVarint(b, uint64(mi.sizePointer(p.Elem(), opts)))
  178. return mi.marshalAppendPointer(b, p.Elem(), opts)
  179. }
  180. func consumeMessageInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  181. if wtyp != wire.BytesType {
  182. return 0, errUnknown
  183. }
  184. v, n := wire.ConsumeBytes(b)
  185. if n < 0 {
  186. return 0, wire.ParseError(n)
  187. }
  188. if p.Elem().IsNil() {
  189. p.SetPointer(pointerOfValue(reflect.New(mi.GoReflectType.Elem())))
  190. }
  191. if _, err := mi.unmarshalPointer(v, p.Elem(), 0, opts); err != nil {
  192. return 0, err
  193. }
  194. return n, nil
  195. }
  196. func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int {
  197. return wire.SizeBytes(proto.Size(m)) + tagsize
  198. }
  199. func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
  200. b = wire.AppendVarint(b, wiretag)
  201. b = wire.AppendVarint(b, uint64(proto.Size(m)))
  202. return opts.Options().MarshalAppend(b, m)
  203. }
  204. func consumeMessage(b []byte, m proto.Message, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  205. if wtyp != wire.BytesType {
  206. return 0, errUnknown
  207. }
  208. v, n := wire.ConsumeBytes(b)
  209. if n < 0 {
  210. return 0, wire.ParseError(n)
  211. }
  212. if err := opts.Options().Unmarshal(v, m); err != nil {
  213. return 0, err
  214. }
  215. return n, nil
  216. }
  217. func sizeMessageValue(v pref.Value, tagsize int, opts marshalOptions) int {
  218. m := v.Message().Interface()
  219. return sizeMessage(m, tagsize, opts)
  220. }
  221. func appendMessageValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  222. m := v.Message().Interface()
  223. return appendMessage(b, m, wiretag, opts)
  224. }
  225. func consumeMessageValue(b []byte, v pref.Value, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
  226. m := v.Message().Interface()
  227. n, err := consumeMessage(b, m, wtyp, opts)
  228. return v, n, err
  229. }
  230. func isInitMessageValue(v pref.Value) error {
  231. m := v.Message().Interface()
  232. return proto.IsInitialized(m)
  233. }
  234. var coderMessageValue = valueCoderFuncs{
  235. size: sizeMessageValue,
  236. marshal: appendMessageValue,
  237. unmarshal: consumeMessageValue,
  238. isInit: isInitMessageValue,
  239. }
  240. func sizeGroupValue(v pref.Value, tagsize int, opts marshalOptions) int {
  241. m := v.Message().Interface()
  242. return sizeGroup(m, tagsize, opts)
  243. }
  244. func appendGroupValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  245. m := v.Message().Interface()
  246. return appendGroup(b, m, wiretag, opts)
  247. }
  248. func consumeGroupValue(b []byte, v pref.Value, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
  249. m := v.Message().Interface()
  250. n, err := consumeGroup(b, m, num, wtyp, opts)
  251. return v, n, err
  252. }
  253. var coderGroupValue = valueCoderFuncs{
  254. size: sizeGroupValue,
  255. marshal: appendGroupValue,
  256. unmarshal: consumeGroupValue,
  257. isInit: isInitMessageValue,
  258. }
  259. func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  260. num := fd.Number()
  261. if mi := getMessageInfo(ft); mi != nil {
  262. return pointerCoderFuncs{
  263. size: func(p pointer, tagsize int, opts marshalOptions) int {
  264. return sizeGroupType(p, mi, tagsize, opts)
  265. },
  266. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  267. return appendGroupType(b, p, wiretag, mi, opts)
  268. },
  269. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  270. return consumeGroupType(b, p, mi, num, wtyp, opts)
  271. },
  272. isInit: func(p pointer) error {
  273. return mi.isInitializedPointer(p.Elem())
  274. },
  275. }
  276. } else {
  277. return pointerCoderFuncs{
  278. size: func(p pointer, tagsize int, opts marshalOptions) int {
  279. m := asMessage(p.AsValueOf(ft).Elem())
  280. return sizeGroup(m, tagsize, opts)
  281. },
  282. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  283. m := asMessage(p.AsValueOf(ft).Elem())
  284. return appendGroup(b, m, wiretag, opts)
  285. },
  286. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  287. mp := p.AsValueOf(ft).Elem()
  288. if mp.IsNil() {
  289. mp.Set(reflect.New(ft.Elem()))
  290. }
  291. return consumeGroup(b, asMessage(mp), num, wtyp, opts)
  292. },
  293. isInit: func(p pointer) error {
  294. m := asMessage(p.AsValueOf(ft).Elem())
  295. return proto.IsInitialized(m)
  296. },
  297. }
  298. }
  299. }
  300. func sizeGroupType(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
  301. return 2*tagsize + mi.sizePointer(p.Elem(), opts)
  302. }
  303. func appendGroupType(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
  304. b = wire.AppendVarint(b, wiretag) // start group
  305. b, err := mi.marshalAppendPointer(b, p.Elem(), opts)
  306. b = wire.AppendVarint(b, wiretag+1) // end group
  307. return b, err
  308. }
  309. func consumeGroupType(b []byte, p pointer, mi *MessageInfo, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  310. if wtyp != wire.StartGroupType {
  311. return 0, errUnknown
  312. }
  313. if p.Elem().IsNil() {
  314. p.SetPointer(pointerOfValue(reflect.New(mi.GoReflectType.Elem())))
  315. }
  316. return mi.unmarshalPointer(b, p.Elem(), num, opts)
  317. }
  318. func sizeGroup(m proto.Message, tagsize int, _ marshalOptions) int {
  319. return 2*tagsize + proto.Size(m)
  320. }
  321. func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
  322. b = wire.AppendVarint(b, wiretag) // start group
  323. b, err := opts.Options().MarshalAppend(b, m)
  324. b = wire.AppendVarint(b, wiretag+1) // end group
  325. return b, err
  326. }
  327. func consumeGroup(b []byte, m proto.Message, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  328. if wtyp != wire.StartGroupType {
  329. return 0, errUnknown
  330. }
  331. b, n := wire.ConsumeGroup(num, b)
  332. if n < 0 {
  333. return 0, wire.ParseError(n)
  334. }
  335. return n, opts.Options().Unmarshal(b, m)
  336. }
  337. func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  338. if mi := getMessageInfo(ft); mi != nil {
  339. return pointerCoderFuncs{
  340. size: func(p pointer, tagsize int, opts marshalOptions) int {
  341. return sizeMessageSliceInfo(p, mi, tagsize, opts)
  342. },
  343. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  344. return appendMessageSliceInfo(b, p, wiretag, mi, opts)
  345. },
  346. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  347. return consumeMessageSliceInfo(b, p, mi, wtyp, opts)
  348. },
  349. isInit: func(p pointer) error {
  350. return isInitMessageSliceInfo(p, mi)
  351. },
  352. }
  353. }
  354. return pointerCoderFuncs{
  355. size: func(p pointer, tagsize int, opts marshalOptions) int {
  356. return sizeMessageSlice(p, ft, tagsize, opts)
  357. },
  358. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  359. return appendMessageSlice(b, p, wiretag, ft, opts)
  360. },
  361. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  362. return consumeMessageSlice(b, p, ft, wtyp, opts)
  363. },
  364. isInit: func(p pointer) error {
  365. return isInitMessageSlice(p, ft)
  366. },
  367. }
  368. }
  369. func sizeMessageSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
  370. s := p.PointerSlice()
  371. n := 0
  372. for _, v := range s {
  373. n += wire.SizeBytes(mi.sizePointer(v, opts)) + tagsize
  374. }
  375. return n
  376. }
  377. func appendMessageSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
  378. s := p.PointerSlice()
  379. var err error
  380. for _, v := range s {
  381. b = wire.AppendVarint(b, wiretag)
  382. siz := mi.sizePointer(v, opts)
  383. b = wire.AppendVarint(b, uint64(siz))
  384. b, err = mi.marshalAppendPointer(b, v, opts)
  385. if err != nil {
  386. return b, err
  387. }
  388. }
  389. return b, nil
  390. }
  391. func consumeMessageSliceInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  392. if wtyp != wire.BytesType {
  393. return 0, errUnknown
  394. }
  395. v, n := wire.ConsumeBytes(b)
  396. if n < 0 {
  397. return 0, wire.ParseError(n)
  398. }
  399. m := reflect.New(mi.GoReflectType.Elem()).Interface()
  400. mp := pointerOfIface(m)
  401. if _, err := mi.unmarshalPointer(v, mp, 0, opts); err != nil {
  402. return 0, err
  403. }
  404. p.AppendPointerSlice(mp)
  405. return n, nil
  406. }
  407. func isInitMessageSliceInfo(p pointer, mi *MessageInfo) error {
  408. s := p.PointerSlice()
  409. for _, v := range s {
  410. if err := mi.isInitializedPointer(v); err != nil {
  411. return err
  412. }
  413. }
  414. return nil
  415. }
  416. func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, _ marshalOptions) int {
  417. s := p.PointerSlice()
  418. n := 0
  419. for _, v := range s {
  420. m := asMessage(v.AsValueOf(goType.Elem()))
  421. n += wire.SizeBytes(proto.Size(m)) + tagsize
  422. }
  423. return n
  424. }
  425. func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) {
  426. s := p.PointerSlice()
  427. var err error
  428. for _, v := range s {
  429. m := asMessage(v.AsValueOf(goType.Elem()))
  430. b = wire.AppendVarint(b, wiretag)
  431. siz := proto.Size(m)
  432. b = wire.AppendVarint(b, uint64(siz))
  433. b, err = opts.Options().MarshalAppend(b, m)
  434. if err != nil {
  435. return b, err
  436. }
  437. }
  438. return b, nil
  439. }
  440. func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  441. if wtyp != wire.BytesType {
  442. return 0, errUnknown
  443. }
  444. v, n := wire.ConsumeBytes(b)
  445. if n < 0 {
  446. return 0, wire.ParseError(n)
  447. }
  448. mp := reflect.New(goType.Elem())
  449. if err := opts.Options().Unmarshal(v, asMessage(mp)); err != nil {
  450. return 0, err
  451. }
  452. p.AppendPointerSlice(pointerOfValue(mp))
  453. return n, nil
  454. }
  455. func isInitMessageSlice(p pointer, goType reflect.Type) error {
  456. s := p.PointerSlice()
  457. for _, v := range s {
  458. m := asMessage(v.AsValueOf(goType.Elem()))
  459. if err := proto.IsInitialized(m); err != nil {
  460. return err
  461. }
  462. }
  463. return nil
  464. }
  465. // Slices of messages
  466. func sizeMessageSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int {
  467. list := listv.List()
  468. n := 0
  469. for i, llen := 0, list.Len(); i < llen; i++ {
  470. m := list.Get(i).Message().Interface()
  471. n += wire.SizeBytes(proto.Size(m)) + tagsize
  472. }
  473. return n
  474. }
  475. func appendMessageSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  476. list := listv.List()
  477. mopts := opts.Options()
  478. for i, llen := 0, list.Len(); i < llen; i++ {
  479. m := list.Get(i).Message().Interface()
  480. b = wire.AppendVarint(b, wiretag)
  481. siz := proto.Size(m)
  482. b = wire.AppendVarint(b, uint64(siz))
  483. var err error
  484. b, err = mopts.MarshalAppend(b, m)
  485. if err != nil {
  486. return b, err
  487. }
  488. }
  489. return b, nil
  490. }
  491. func consumeMessageSliceValue(b []byte, listv pref.Value, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
  492. list := listv.List()
  493. if wtyp != wire.BytesType {
  494. return pref.Value{}, 0, errUnknown
  495. }
  496. v, n := wire.ConsumeBytes(b)
  497. if n < 0 {
  498. return pref.Value{}, 0, wire.ParseError(n)
  499. }
  500. m := list.NewElement()
  501. if err := opts.Options().Unmarshal(v, m.Message().Interface()); err != nil {
  502. return pref.Value{}, 0, err
  503. }
  504. list.Append(m)
  505. return listv, n, nil
  506. }
  507. func isInitMessageSliceValue(listv pref.Value) error {
  508. list := listv.List()
  509. for i, llen := 0, list.Len(); i < llen; i++ {
  510. m := list.Get(i).Message().Interface()
  511. if err := proto.IsInitialized(m); err != nil {
  512. return err
  513. }
  514. }
  515. return nil
  516. }
  517. var coderMessageSliceValue = valueCoderFuncs{
  518. size: sizeMessageSliceValue,
  519. marshal: appendMessageSliceValue,
  520. unmarshal: consumeMessageSliceValue,
  521. isInit: isInitMessageSliceValue,
  522. }
  523. func sizeGroupSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int {
  524. list := listv.List()
  525. n := 0
  526. for i, llen := 0, list.Len(); i < llen; i++ {
  527. m := list.Get(i).Message().Interface()
  528. n += 2*tagsize + proto.Size(m)
  529. }
  530. return n
  531. }
  532. func appendGroupSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  533. list := listv.List()
  534. mopts := opts.Options()
  535. for i, llen := 0, list.Len(); i < llen; i++ {
  536. m := list.Get(i).Message().Interface()
  537. b = wire.AppendVarint(b, wiretag) // start group
  538. var err error
  539. b, err = mopts.MarshalAppend(b, m)
  540. if err != nil {
  541. return b, err
  542. }
  543. b = wire.AppendVarint(b, wiretag+1) // end group
  544. }
  545. return b, nil
  546. }
  547. func consumeGroupSliceValue(b []byte, listv pref.Value, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error) {
  548. list := listv.List()
  549. if wtyp != wire.StartGroupType {
  550. return pref.Value{}, 0, errUnknown
  551. }
  552. b, n := wire.ConsumeGroup(num, b)
  553. if n < 0 {
  554. return pref.Value{}, 0, wire.ParseError(n)
  555. }
  556. m := list.NewElement()
  557. if err := opts.Options().Unmarshal(b, m.Message().Interface()); err != nil {
  558. return pref.Value{}, 0, err
  559. }
  560. list.Append(m)
  561. return listv, n, nil
  562. }
  563. var coderGroupSliceValue = valueCoderFuncs{
  564. size: sizeGroupSliceValue,
  565. marshal: appendGroupSliceValue,
  566. unmarshal: consumeGroupSliceValue,
  567. isInit: isInitMessageSliceValue,
  568. }
  569. func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  570. num := fd.Number()
  571. if mi := getMessageInfo(ft); mi != nil {
  572. return pointerCoderFuncs{
  573. size: func(p pointer, tagsize int, opts marshalOptions) int {
  574. return sizeGroupSliceInfo(p, mi, tagsize, opts)
  575. },
  576. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  577. return appendGroupSliceInfo(b, p, wiretag, mi, opts)
  578. },
  579. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  580. return consumeGroupSliceInfo(b, p, num, wtyp, mi, opts)
  581. },
  582. isInit: func(p pointer) error {
  583. return isInitMessageSliceInfo(p, mi)
  584. },
  585. }
  586. }
  587. return pointerCoderFuncs{
  588. size: func(p pointer, tagsize int, opts marshalOptions) int {
  589. return sizeGroupSlice(p, ft, tagsize, opts)
  590. },
  591. marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) {
  592. return appendGroupSlice(b, p, wiretag, ft, opts)
  593. },
  594. unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) {
  595. return consumeGroupSlice(b, p, num, wtyp, ft, opts)
  596. },
  597. isInit: func(p pointer) error {
  598. return isInitMessageSlice(p, ft)
  599. },
  600. }
  601. }
  602. func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, _ marshalOptions) int {
  603. s := p.PointerSlice()
  604. n := 0
  605. for _, v := range s {
  606. m := asMessage(v.AsValueOf(messageType.Elem()))
  607. n += 2*tagsize + proto.Size(m)
  608. }
  609. return n
  610. }
  611. func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) {
  612. s := p.PointerSlice()
  613. var err error
  614. for _, v := range s {
  615. m := asMessage(v.AsValueOf(messageType.Elem()))
  616. b = wire.AppendVarint(b, wiretag) // start group
  617. b, err = opts.Options().MarshalAppend(b, m)
  618. if err != nil {
  619. return b, err
  620. }
  621. b = wire.AppendVarint(b, wiretag+1) // end group
  622. }
  623. return b, nil
  624. }
  625. func consumeGroupSlice(b []byte, p pointer, num wire.Number, wtyp wire.Type, goType reflect.Type, opts unmarshalOptions) (int, error) {
  626. if wtyp != wire.StartGroupType {
  627. return 0, errUnknown
  628. }
  629. b, n := wire.ConsumeGroup(num, b)
  630. if n < 0 {
  631. return 0, wire.ParseError(n)
  632. }
  633. mp := reflect.New(goType.Elem())
  634. if err := opts.Options().Unmarshal(b, asMessage(mp)); err != nil {
  635. return 0, err
  636. }
  637. p.AppendPointerSlice(pointerOfValue(mp))
  638. return n, nil
  639. }
  640. func sizeGroupSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int {
  641. s := p.PointerSlice()
  642. n := 0
  643. for _, v := range s {
  644. n += 2*tagsize + mi.sizePointer(v, opts)
  645. }
  646. return n
  647. }
  648. func appendGroupSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) {
  649. s := p.PointerSlice()
  650. var err error
  651. for _, v := range s {
  652. b = wire.AppendVarint(b, wiretag) // start group
  653. b, err = mi.marshalAppendPointer(b, v, opts)
  654. if err != nil {
  655. return b, err
  656. }
  657. b = wire.AppendVarint(b, wiretag+1) // end group
  658. }
  659. return b, nil
  660. }
  661. func consumeGroupSliceInfo(b []byte, p pointer, num wire.Number, wtyp wire.Type, mi *MessageInfo, opts unmarshalOptions) (int, error) {
  662. if wtyp != wire.StartGroupType {
  663. return 0, errUnknown
  664. }
  665. m := reflect.New(mi.GoReflectType.Elem()).Interface()
  666. mp := pointerOfIface(m)
  667. n, err := mi.unmarshalPointer(b, mp, num, opts)
  668. if err != nil {
  669. return 0, err
  670. }
  671. p.AppendPointerSlice(mp)
  672. return n, nil
  673. }
  674. func asMessage(v reflect.Value) pref.ProtoMessage {
  675. if m, ok := v.Interface().(pref.ProtoMessage); ok {
  676. return m
  677. }
  678. return legacyWrapMessage(v)
  679. }