codec_field.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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/encoding/protowire"
  10. "google.golang.org/protobuf/internal/errors"
  11. "google.golang.org/protobuf/proto"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. "google.golang.org/protobuf/reflect/protoregistry"
  14. "google.golang.org/protobuf/runtime/protoiface"
  15. )
  16. type errInvalidUTF8 struct{}
  17. func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" }
  18. func (errInvalidUTF8) InvalidUTF8() bool { return true }
  19. func (errInvalidUTF8) Unwrap() error { return errors.Error }
  20. // initOneofFieldCoders initializes the fast-path functions for the fields in a oneof.
  21. //
  22. // For size, marshal, and isInit operations, functions are set only on the first field
  23. // in the oneof. The functions are called when the oneof is non-nil, and will dispatch
  24. // to the appropriate field-specific function as necessary.
  25. //
  26. // The unmarshal function is set on each field individually as usual.
  27. func (mi *MessageInfo) initOneofFieldCoders(od protoreflect.OneofDescriptor, si structInfo) {
  28. fs := si.oneofsByName[od.Name()]
  29. ft := fs.Type
  30. oneofFields := make(map[reflect.Type]*coderFieldInfo)
  31. needIsInit := false
  32. fields := od.Fields()
  33. for i, lim := 0, fields.Len(); i < lim; i++ {
  34. fd := od.Fields().Get(i)
  35. num := fd.Number()
  36. // Make a copy of the original coderFieldInfo for use in unmarshaling.
  37. //
  38. // oneofFields[oneofType].funcs.marshal is the field-specific marshal function.
  39. //
  40. // mi.coderFields[num].marshal is set on only the first field in the oneof,
  41. // and dispatches to the field-specific marshaler in oneofFields.
  42. cf := *mi.coderFields[num]
  43. ot := si.oneofWrappersByNumber[num]
  44. cf.ft = ot.Field(0).Type
  45. cf.mi, cf.funcs = fieldCoder(fd, cf.ft)
  46. oneofFields[ot] = &cf
  47. if cf.funcs.isInit != nil {
  48. needIsInit = true
  49. }
  50. mi.coderFields[num].funcs.unmarshal = func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, 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. out, err := cf.funcs.unmarshal(b, pointerOfValue(vw).Apply(zeroOffset), wtyp, &cf, opts)
  59. if err != nil {
  60. return out, err
  61. }
  62. vi.Set(vw)
  63. return out, nil
  64. }
  65. }
  66. getInfo := func(p pointer) (pointer, *coderFieldInfo) {
  67. v := p.AsValueOf(ft).Elem()
  68. if v.IsNil() {
  69. return pointer{}, nil
  70. }
  71. v = v.Elem() // interface -> *struct
  72. if v.IsNil() {
  73. return pointer{}, nil
  74. }
  75. return pointerOfValue(v).Apply(zeroOffset), oneofFields[v.Elem().Type()]
  76. }
  77. first := mi.coderFields[od.Fields().Get(0).Number()]
  78. first.funcs.size = func(p pointer, _ *coderFieldInfo, opts marshalOptions) int {
  79. p, info := getInfo(p)
  80. if info == nil || info.funcs.size == nil {
  81. return 0
  82. }
  83. return info.funcs.size(p, info, opts)
  84. }
  85. first.funcs.marshal = func(b []byte, p pointer, _ *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  86. p, info := getInfo(p)
  87. if info == nil || info.funcs.marshal == nil {
  88. return b, nil
  89. }
  90. return info.funcs.marshal(b, p, info, opts)
  91. }
  92. first.funcs.merge = func(dst, src pointer, _ *coderFieldInfo, opts mergeOptions) {
  93. srcp, srcinfo := getInfo(src)
  94. if srcinfo == nil || srcinfo.funcs.merge == nil {
  95. return
  96. }
  97. dstp, dstinfo := getInfo(dst)
  98. if dstinfo != srcinfo {
  99. dst.AsValueOf(ft).Elem().Set(reflect.New(src.AsValueOf(ft).Elem().Elem().Elem().Type()))
  100. dstp = pointerOfValue(dst.AsValueOf(ft).Elem().Elem()).Apply(zeroOffset)
  101. }
  102. srcinfo.funcs.merge(dstp, srcp, srcinfo, opts)
  103. }
  104. if needIsInit {
  105. first.funcs.isInit = func(p pointer, _ *coderFieldInfo) error {
  106. p, info := getInfo(p)
  107. if info == nil || info.funcs.isInit == nil {
  108. return nil
  109. }
  110. return info.funcs.isInit(p, info)
  111. }
  112. }
  113. }
  114. func makeWeakMessageFieldCoder(fd protoreflect.FieldDescriptor) pointerCoderFuncs {
  115. var once sync.Once
  116. var messageType protoreflect.MessageType
  117. lazyInit := func() {
  118. once.Do(func() {
  119. messageName := fd.Message().FullName()
  120. messageType, _ = protoregistry.GlobalTypes.FindMessageByName(messageName)
  121. })
  122. }
  123. return pointerCoderFuncs{
  124. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  125. m, ok := p.WeakFields().get(f.num)
  126. if !ok {
  127. return 0
  128. }
  129. lazyInit()
  130. if messageType == nil {
  131. panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
  132. }
  133. return sizeMessage(m, f.tagsize, opts)
  134. },
  135. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  136. m, ok := p.WeakFields().get(f.num)
  137. if !ok {
  138. return b, nil
  139. }
  140. lazyInit()
  141. if messageType == nil {
  142. panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
  143. }
  144. return appendMessage(b, m, f.wiretag, opts)
  145. },
  146. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  147. fs := p.WeakFields()
  148. m, ok := fs.get(f.num)
  149. if !ok {
  150. lazyInit()
  151. if messageType == nil {
  152. return unmarshalOutput{}, errUnknown
  153. }
  154. m = messageType.New().Interface()
  155. fs.set(f.num, m)
  156. }
  157. return consumeMessage(b, m, wtyp, opts)
  158. },
  159. isInit: func(p pointer, f *coderFieldInfo) error {
  160. m, ok := p.WeakFields().get(f.num)
  161. if !ok {
  162. return nil
  163. }
  164. return proto.CheckInitialized(m)
  165. },
  166. merge: func(dst, src pointer, f *coderFieldInfo, opts mergeOptions) {
  167. sm, ok := src.WeakFields().get(f.num)
  168. if !ok {
  169. return
  170. }
  171. dm, ok := dst.WeakFields().get(f.num)
  172. if !ok {
  173. lazyInit()
  174. if messageType == nil {
  175. panic(fmt.Sprintf("weak message %v is not linked in", fd.Message().FullName()))
  176. }
  177. dm = messageType.New().Interface()
  178. dst.WeakFields().set(f.num, dm)
  179. }
  180. opts.Merge(dm, sm)
  181. },
  182. }
  183. }
  184. func makeMessageFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  185. if mi := getMessageInfo(ft); mi != nil {
  186. funcs := pointerCoderFuncs{
  187. size: sizeMessageInfo,
  188. marshal: appendMessageInfo,
  189. unmarshal: consumeMessageInfo,
  190. merge: mergeMessage,
  191. }
  192. if needsInitCheck(mi.Desc) {
  193. funcs.isInit = isInitMessageInfo
  194. }
  195. return funcs
  196. } else {
  197. return pointerCoderFuncs{
  198. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  199. m := asMessage(p.AsValueOf(ft).Elem())
  200. return sizeMessage(m, f.tagsize, opts)
  201. },
  202. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  203. m := asMessage(p.AsValueOf(ft).Elem())
  204. return appendMessage(b, m, f.wiretag, opts)
  205. },
  206. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  207. mp := p.AsValueOf(ft).Elem()
  208. if mp.IsNil() {
  209. mp.Set(reflect.New(ft.Elem()))
  210. }
  211. return consumeMessage(b, asMessage(mp), wtyp, opts)
  212. },
  213. isInit: func(p pointer, f *coderFieldInfo) error {
  214. m := asMessage(p.AsValueOf(ft).Elem())
  215. return proto.CheckInitialized(m)
  216. },
  217. merge: mergeMessage,
  218. }
  219. }
  220. }
  221. func sizeMessageInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  222. return protowire.SizeBytes(f.mi.sizePointer(p.Elem(), opts)) + f.tagsize
  223. }
  224. func appendMessageInfo(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  225. calculatedSize := f.mi.sizePointer(p.Elem(), opts)
  226. b = protowire.AppendVarint(b, f.wiretag)
  227. b = protowire.AppendVarint(b, uint64(calculatedSize))
  228. before := len(b)
  229. b, err := f.mi.marshalAppendPointer(b, p.Elem(), opts)
  230. if measuredSize := len(b) - before; calculatedSize != measuredSize && err == nil {
  231. return nil, errors.MismatchedSizeCalculation(calculatedSize, measuredSize)
  232. }
  233. return b, err
  234. }
  235. func consumeMessageInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
  236. if wtyp != protowire.BytesType {
  237. return out, errUnknown
  238. }
  239. v, n := protowire.ConsumeBytes(b)
  240. if n < 0 {
  241. return out, errDecode
  242. }
  243. if p.Elem().IsNil() {
  244. p.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem())))
  245. }
  246. o, err := f.mi.unmarshalPointer(v, p.Elem(), 0, opts)
  247. if err != nil {
  248. return out, err
  249. }
  250. out.n = n
  251. out.initialized = o.initialized
  252. return out, nil
  253. }
  254. func isInitMessageInfo(p pointer, f *coderFieldInfo) error {
  255. return f.mi.checkInitializedPointer(p.Elem())
  256. }
  257. func sizeMessage(m proto.Message, tagsize int, opts marshalOptions) int {
  258. return protowire.SizeBytes(opts.Options().Size(m)) + tagsize
  259. }
  260. func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
  261. mopts := opts.Options()
  262. calculatedSize := mopts.Size(m)
  263. b = protowire.AppendVarint(b, wiretag)
  264. b = protowire.AppendVarint(b, uint64(calculatedSize))
  265. before := len(b)
  266. b, err := mopts.MarshalAppend(b, m)
  267. if measuredSize := len(b) - before; calculatedSize != measuredSize && err == nil {
  268. return nil, errors.MismatchedSizeCalculation(calculatedSize, measuredSize)
  269. }
  270. return b, err
  271. }
  272. func consumeMessage(b []byte, m proto.Message, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  273. if wtyp != protowire.BytesType {
  274. return out, errUnknown
  275. }
  276. v, n := protowire.ConsumeBytes(b)
  277. if n < 0 {
  278. return out, errDecode
  279. }
  280. o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{
  281. Buf: v,
  282. Message: m.ProtoReflect(),
  283. })
  284. if err != nil {
  285. return out, err
  286. }
  287. out.n = n
  288. out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0
  289. return out, nil
  290. }
  291. func sizeMessageValue(v protoreflect.Value, tagsize int, opts marshalOptions) int {
  292. m := v.Message().Interface()
  293. return sizeMessage(m, tagsize, opts)
  294. }
  295. func appendMessageValue(b []byte, v protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  296. m := v.Message().Interface()
  297. return appendMessage(b, m, wiretag, opts)
  298. }
  299. func consumeMessageValue(b []byte, v protoreflect.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (protoreflect.Value, unmarshalOutput, error) {
  300. m := v.Message().Interface()
  301. out, err := consumeMessage(b, m, wtyp, opts)
  302. return v, out, err
  303. }
  304. func isInitMessageValue(v protoreflect.Value) error {
  305. m := v.Message().Interface()
  306. return proto.CheckInitialized(m)
  307. }
  308. var coderMessageValue = valueCoderFuncs{
  309. size: sizeMessageValue,
  310. marshal: appendMessageValue,
  311. unmarshal: consumeMessageValue,
  312. isInit: isInitMessageValue,
  313. merge: mergeMessageValue,
  314. }
  315. func sizeGroupValue(v protoreflect.Value, tagsize int, opts marshalOptions) int {
  316. m := v.Message().Interface()
  317. return sizeGroup(m, tagsize, opts)
  318. }
  319. func appendGroupValue(b []byte, v protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  320. m := v.Message().Interface()
  321. return appendGroup(b, m, wiretag, opts)
  322. }
  323. func consumeGroupValue(b []byte, v protoreflect.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (protoreflect.Value, unmarshalOutput, error) {
  324. m := v.Message().Interface()
  325. out, err := consumeGroup(b, m, num, wtyp, opts)
  326. return v, out, err
  327. }
  328. var coderGroupValue = valueCoderFuncs{
  329. size: sizeGroupValue,
  330. marshal: appendGroupValue,
  331. unmarshal: consumeGroupValue,
  332. isInit: isInitMessageValue,
  333. merge: mergeMessageValue,
  334. }
  335. func makeGroupFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  336. num := fd.Number()
  337. if mi := getMessageInfo(ft); mi != nil {
  338. funcs := pointerCoderFuncs{
  339. size: sizeGroupType,
  340. marshal: appendGroupType,
  341. unmarshal: consumeGroupType,
  342. merge: mergeMessage,
  343. }
  344. if needsInitCheck(mi.Desc) {
  345. funcs.isInit = isInitMessageInfo
  346. }
  347. return funcs
  348. } else {
  349. return pointerCoderFuncs{
  350. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  351. m := asMessage(p.AsValueOf(ft).Elem())
  352. return sizeGroup(m, f.tagsize, opts)
  353. },
  354. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  355. m := asMessage(p.AsValueOf(ft).Elem())
  356. return appendGroup(b, m, f.wiretag, opts)
  357. },
  358. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  359. mp := p.AsValueOf(ft).Elem()
  360. if mp.IsNil() {
  361. mp.Set(reflect.New(ft.Elem()))
  362. }
  363. return consumeGroup(b, asMessage(mp), num, wtyp, opts)
  364. },
  365. isInit: func(p pointer, f *coderFieldInfo) error {
  366. m := asMessage(p.AsValueOf(ft).Elem())
  367. return proto.CheckInitialized(m)
  368. },
  369. merge: mergeMessage,
  370. }
  371. }
  372. }
  373. func sizeGroupType(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  374. return 2*f.tagsize + f.mi.sizePointer(p.Elem(), opts)
  375. }
  376. func appendGroupType(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  377. b = protowire.AppendVarint(b, f.wiretag) // start group
  378. b, err := f.mi.marshalAppendPointer(b, p.Elem(), opts)
  379. b = protowire.AppendVarint(b, f.wiretag+1) // end group
  380. return b, err
  381. }
  382. func consumeGroupType(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
  383. if wtyp != protowire.StartGroupType {
  384. return out, errUnknown
  385. }
  386. if p.Elem().IsNil() {
  387. p.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem())))
  388. }
  389. return f.mi.unmarshalPointer(b, p.Elem(), f.num, opts)
  390. }
  391. func sizeGroup(m proto.Message, tagsize int, opts marshalOptions) int {
  392. return 2*tagsize + opts.Options().Size(m)
  393. }
  394. func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
  395. b = protowire.AppendVarint(b, wiretag) // start group
  396. b, err := opts.Options().MarshalAppend(b, m)
  397. b = protowire.AppendVarint(b, wiretag+1) // end group
  398. return b, err
  399. }
  400. func consumeGroup(b []byte, m proto.Message, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  401. if wtyp != protowire.StartGroupType {
  402. return out, errUnknown
  403. }
  404. b, n := protowire.ConsumeGroup(num, b)
  405. if n < 0 {
  406. return out, errDecode
  407. }
  408. o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{
  409. Buf: b,
  410. Message: m.ProtoReflect(),
  411. })
  412. if err != nil {
  413. return out, err
  414. }
  415. out.n = n
  416. out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0
  417. return out, nil
  418. }
  419. func makeMessageSliceFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  420. if mi := getMessageInfo(ft); mi != nil {
  421. funcs := pointerCoderFuncs{
  422. size: sizeMessageSliceInfo,
  423. marshal: appendMessageSliceInfo,
  424. unmarshal: consumeMessageSliceInfo,
  425. merge: mergeMessageSlice,
  426. }
  427. if needsInitCheck(mi.Desc) {
  428. funcs.isInit = isInitMessageSliceInfo
  429. }
  430. return funcs
  431. }
  432. return pointerCoderFuncs{
  433. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  434. return sizeMessageSlice(p, ft, f.tagsize, opts)
  435. },
  436. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  437. return appendMessageSlice(b, p, f.wiretag, ft, opts)
  438. },
  439. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  440. return consumeMessageSlice(b, p, ft, wtyp, opts)
  441. },
  442. isInit: func(p pointer, f *coderFieldInfo) error {
  443. return isInitMessageSlice(p, ft)
  444. },
  445. merge: mergeMessageSlice,
  446. }
  447. }
  448. func sizeMessageSliceInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  449. s := p.PointerSlice()
  450. n := 0
  451. for _, v := range s {
  452. n += protowire.SizeBytes(f.mi.sizePointer(v, opts)) + f.tagsize
  453. }
  454. return n
  455. }
  456. func appendMessageSliceInfo(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  457. s := p.PointerSlice()
  458. var err error
  459. for _, v := range s {
  460. b = protowire.AppendVarint(b, f.wiretag)
  461. siz := f.mi.sizePointer(v, opts)
  462. b = protowire.AppendVarint(b, uint64(siz))
  463. before := len(b)
  464. b, err = f.mi.marshalAppendPointer(b, v, opts)
  465. if err != nil {
  466. return b, err
  467. }
  468. if measuredSize := len(b) - before; siz != measuredSize {
  469. return nil, errors.MismatchedSizeCalculation(siz, measuredSize)
  470. }
  471. }
  472. return b, nil
  473. }
  474. func consumeMessageSliceInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
  475. if wtyp != protowire.BytesType {
  476. return out, errUnknown
  477. }
  478. v, n := protowire.ConsumeBytes(b)
  479. if n < 0 {
  480. return out, errDecode
  481. }
  482. m := reflect.New(f.mi.GoReflectType.Elem()).Interface()
  483. mp := pointerOfIface(m)
  484. o, err := f.mi.unmarshalPointer(v, mp, 0, opts)
  485. if err != nil {
  486. return out, err
  487. }
  488. p.AppendPointerSlice(mp)
  489. out.n = n
  490. out.initialized = o.initialized
  491. return out, nil
  492. }
  493. func isInitMessageSliceInfo(p pointer, f *coderFieldInfo) error {
  494. s := p.PointerSlice()
  495. for _, v := range s {
  496. if err := f.mi.checkInitializedPointer(v); err != nil {
  497. return err
  498. }
  499. }
  500. return nil
  501. }
  502. func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, opts marshalOptions) int {
  503. mopts := opts.Options()
  504. s := p.PointerSlice()
  505. n := 0
  506. for _, v := range s {
  507. m := asMessage(v.AsValueOf(goType.Elem()))
  508. n += protowire.SizeBytes(mopts.Size(m)) + tagsize
  509. }
  510. return n
  511. }
  512. func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) {
  513. mopts := opts.Options()
  514. s := p.PointerSlice()
  515. var err error
  516. for _, v := range s {
  517. m := asMessage(v.AsValueOf(goType.Elem()))
  518. b = protowire.AppendVarint(b, wiretag)
  519. siz := mopts.Size(m)
  520. b = protowire.AppendVarint(b, uint64(siz))
  521. before := len(b)
  522. b, err = mopts.MarshalAppend(b, m)
  523. if err != nil {
  524. return b, err
  525. }
  526. if measuredSize := len(b) - before; siz != measuredSize {
  527. return nil, errors.MismatchedSizeCalculation(siz, measuredSize)
  528. }
  529. }
  530. return b, nil
  531. }
  532. func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  533. if wtyp != protowire.BytesType {
  534. return out, errUnknown
  535. }
  536. v, n := protowire.ConsumeBytes(b)
  537. if n < 0 {
  538. return out, errDecode
  539. }
  540. mp := reflect.New(goType.Elem())
  541. o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{
  542. Buf: v,
  543. Message: asMessage(mp).ProtoReflect(),
  544. })
  545. if err != nil {
  546. return out, err
  547. }
  548. p.AppendPointerSlice(pointerOfValue(mp))
  549. out.n = n
  550. out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0
  551. return out, nil
  552. }
  553. func isInitMessageSlice(p pointer, goType reflect.Type) error {
  554. s := p.PointerSlice()
  555. for _, v := range s {
  556. m := asMessage(v.AsValueOf(goType.Elem()))
  557. if err := proto.CheckInitialized(m); err != nil {
  558. return err
  559. }
  560. }
  561. return nil
  562. }
  563. // Slices of messages
  564. func sizeMessageSliceValue(listv protoreflect.Value, tagsize int, opts marshalOptions) int {
  565. mopts := opts.Options()
  566. list := listv.List()
  567. n := 0
  568. for i, llen := 0, list.Len(); i < llen; i++ {
  569. m := list.Get(i).Message().Interface()
  570. n += protowire.SizeBytes(mopts.Size(m)) + tagsize
  571. }
  572. return n
  573. }
  574. func appendMessageSliceValue(b []byte, listv protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  575. list := listv.List()
  576. mopts := opts.Options()
  577. for i, llen := 0, list.Len(); i < llen; i++ {
  578. m := list.Get(i).Message().Interface()
  579. b = protowire.AppendVarint(b, wiretag)
  580. siz := mopts.Size(m)
  581. b = protowire.AppendVarint(b, uint64(siz))
  582. before := len(b)
  583. var err error
  584. b, err = mopts.MarshalAppend(b, m)
  585. if err != nil {
  586. return b, err
  587. }
  588. if measuredSize := len(b) - before; siz != measuredSize {
  589. return nil, errors.MismatchedSizeCalculation(siz, measuredSize)
  590. }
  591. }
  592. return b, nil
  593. }
  594. func consumeMessageSliceValue(b []byte, listv protoreflect.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ protoreflect.Value, out unmarshalOutput, err error) {
  595. list := listv.List()
  596. if wtyp != protowire.BytesType {
  597. return protoreflect.Value{}, out, errUnknown
  598. }
  599. v, n := protowire.ConsumeBytes(b)
  600. if n < 0 {
  601. return protoreflect.Value{}, out, errDecode
  602. }
  603. m := list.NewElement()
  604. o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{
  605. Buf: v,
  606. Message: m.Message(),
  607. })
  608. if err != nil {
  609. return protoreflect.Value{}, out, err
  610. }
  611. list.Append(m)
  612. out.n = n
  613. out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0
  614. return listv, out, nil
  615. }
  616. func isInitMessageSliceValue(listv protoreflect.Value) error {
  617. list := listv.List()
  618. for i, llen := 0, list.Len(); i < llen; i++ {
  619. m := list.Get(i).Message().Interface()
  620. if err := proto.CheckInitialized(m); err != nil {
  621. return err
  622. }
  623. }
  624. return nil
  625. }
  626. var coderMessageSliceValue = valueCoderFuncs{
  627. size: sizeMessageSliceValue,
  628. marshal: appendMessageSliceValue,
  629. unmarshal: consumeMessageSliceValue,
  630. isInit: isInitMessageSliceValue,
  631. merge: mergeMessageListValue,
  632. }
  633. func sizeGroupSliceValue(listv protoreflect.Value, tagsize int, opts marshalOptions) int {
  634. mopts := opts.Options()
  635. list := listv.List()
  636. n := 0
  637. for i, llen := 0, list.Len(); i < llen; i++ {
  638. m := list.Get(i).Message().Interface()
  639. n += 2*tagsize + mopts.Size(m)
  640. }
  641. return n
  642. }
  643. func appendGroupSliceValue(b []byte, listv protoreflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  644. list := listv.List()
  645. mopts := opts.Options()
  646. for i, llen := 0, list.Len(); i < llen; i++ {
  647. m := list.Get(i).Message().Interface()
  648. b = protowire.AppendVarint(b, wiretag) // start group
  649. var err error
  650. b, err = mopts.MarshalAppend(b, m)
  651. if err != nil {
  652. return b, err
  653. }
  654. b = protowire.AppendVarint(b, wiretag+1) // end group
  655. }
  656. return b, nil
  657. }
  658. func consumeGroupSliceValue(b []byte, listv protoreflect.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ protoreflect.Value, out unmarshalOutput, err error) {
  659. list := listv.List()
  660. if wtyp != protowire.StartGroupType {
  661. return protoreflect.Value{}, out, errUnknown
  662. }
  663. b, n := protowire.ConsumeGroup(num, b)
  664. if n < 0 {
  665. return protoreflect.Value{}, out, errDecode
  666. }
  667. m := list.NewElement()
  668. o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{
  669. Buf: b,
  670. Message: m.Message(),
  671. })
  672. if err != nil {
  673. return protoreflect.Value{}, out, err
  674. }
  675. list.Append(m)
  676. out.n = n
  677. out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0
  678. return listv, out, nil
  679. }
  680. var coderGroupSliceValue = valueCoderFuncs{
  681. size: sizeGroupSliceValue,
  682. marshal: appendGroupSliceValue,
  683. unmarshal: consumeGroupSliceValue,
  684. isInit: isInitMessageSliceValue,
  685. merge: mergeMessageListValue,
  686. }
  687. func makeGroupSliceFieldCoder(fd protoreflect.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  688. num := fd.Number()
  689. if mi := getMessageInfo(ft); mi != nil {
  690. funcs := pointerCoderFuncs{
  691. size: sizeGroupSliceInfo,
  692. marshal: appendGroupSliceInfo,
  693. unmarshal: consumeGroupSliceInfo,
  694. merge: mergeMessageSlice,
  695. }
  696. if needsInitCheck(mi.Desc) {
  697. funcs.isInit = isInitMessageSliceInfo
  698. }
  699. return funcs
  700. }
  701. return pointerCoderFuncs{
  702. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  703. return sizeGroupSlice(p, ft, f.tagsize, opts)
  704. },
  705. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  706. return appendGroupSlice(b, p, f.wiretag, ft, opts)
  707. },
  708. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  709. return consumeGroupSlice(b, p, num, wtyp, ft, opts)
  710. },
  711. isInit: func(p pointer, f *coderFieldInfo) error {
  712. return isInitMessageSlice(p, ft)
  713. },
  714. merge: mergeMessageSlice,
  715. }
  716. }
  717. func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, opts marshalOptions) int {
  718. mopts := opts.Options()
  719. s := p.PointerSlice()
  720. n := 0
  721. for _, v := range s {
  722. m := asMessage(v.AsValueOf(messageType.Elem()))
  723. n += 2*tagsize + mopts.Size(m)
  724. }
  725. return n
  726. }
  727. func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) {
  728. s := p.PointerSlice()
  729. var err error
  730. for _, v := range s {
  731. m := asMessage(v.AsValueOf(messageType.Elem()))
  732. b = protowire.AppendVarint(b, wiretag) // start group
  733. b, err = opts.Options().MarshalAppend(b, m)
  734. if err != nil {
  735. return b, err
  736. }
  737. b = protowire.AppendVarint(b, wiretag+1) // end group
  738. }
  739. return b, nil
  740. }
  741. func consumeGroupSlice(b []byte, p pointer, num protowire.Number, wtyp protowire.Type, goType reflect.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  742. if wtyp != protowire.StartGroupType {
  743. return out, errUnknown
  744. }
  745. b, n := protowire.ConsumeGroup(num, b)
  746. if n < 0 {
  747. return out, errDecode
  748. }
  749. mp := reflect.New(goType.Elem())
  750. o, err := opts.Options().UnmarshalState(protoiface.UnmarshalInput{
  751. Buf: b,
  752. Message: asMessage(mp).ProtoReflect(),
  753. })
  754. if err != nil {
  755. return out, err
  756. }
  757. p.AppendPointerSlice(pointerOfValue(mp))
  758. out.n = n
  759. out.initialized = o.Flags&protoiface.UnmarshalInitialized != 0
  760. return out, nil
  761. }
  762. func sizeGroupSliceInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  763. s := p.PointerSlice()
  764. n := 0
  765. for _, v := range s {
  766. n += 2*f.tagsize + f.mi.sizePointer(v, opts)
  767. }
  768. return n
  769. }
  770. func appendGroupSliceInfo(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  771. s := p.PointerSlice()
  772. var err error
  773. for _, v := range s {
  774. b = protowire.AppendVarint(b, f.wiretag) // start group
  775. b, err = f.mi.marshalAppendPointer(b, v, opts)
  776. if err != nil {
  777. return b, err
  778. }
  779. b = protowire.AppendVarint(b, f.wiretag+1) // end group
  780. }
  781. return b, nil
  782. }
  783. func consumeGroupSliceInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  784. if wtyp != protowire.StartGroupType {
  785. return unmarshalOutput{}, errUnknown
  786. }
  787. m := reflect.New(f.mi.GoReflectType.Elem()).Interface()
  788. mp := pointerOfIface(m)
  789. out, err := f.mi.unmarshalPointer(b, mp, f.num, opts)
  790. if err != nil {
  791. return out, err
  792. }
  793. p.AppendPointerSlice(mp)
  794. return out, nil
  795. }
  796. func asMessage(v reflect.Value) protoreflect.ProtoMessage {
  797. if m, ok := v.Interface().(protoreflect.ProtoMessage); ok {
  798. return m
  799. }
  800. return legacyWrapMessage(v).Interface()
  801. }