codec_field.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. pref "google.golang.org/protobuf/reflect/protoreflect"
  13. preg "google.golang.org/protobuf/reflect/protoregistry"
  14. piface "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 pref.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 pref.FieldDescriptor) pointerCoderFuncs {
  115. var once sync.Once
  116. var messageType pref.MessageType
  117. lazyInit := func() {
  118. once.Do(func() {
  119. messageName := fd.Message().FullName()
  120. messageType, _ = preg.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 pref.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. b = protowire.AppendVarint(b, f.wiretag)
  226. b = protowire.AppendVarint(b, uint64(f.mi.sizePointer(p.Elem(), opts)))
  227. return f.mi.marshalAppendPointer(b, p.Elem(), opts)
  228. }
  229. func consumeMessageInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
  230. if wtyp != protowire.BytesType {
  231. return out, errUnknown
  232. }
  233. v, n := protowire.ConsumeBytes(b)
  234. if n < 0 {
  235. return out, errDecode
  236. }
  237. if p.Elem().IsNil() {
  238. p.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem())))
  239. }
  240. o, err := f.mi.unmarshalPointer(v, p.Elem(), 0, opts)
  241. if err != nil {
  242. return out, err
  243. }
  244. out.n = n
  245. out.initialized = o.initialized
  246. return out, nil
  247. }
  248. func isInitMessageInfo(p pointer, f *coderFieldInfo) error {
  249. return f.mi.checkInitializedPointer(p.Elem())
  250. }
  251. func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int {
  252. return protowire.SizeBytes(proto.Size(m)) + tagsize
  253. }
  254. func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
  255. b = protowire.AppendVarint(b, wiretag)
  256. b = protowire.AppendVarint(b, uint64(proto.Size(m)))
  257. return opts.Options().MarshalAppend(b, m)
  258. }
  259. func consumeMessage(b []byte, m proto.Message, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  260. if wtyp != protowire.BytesType {
  261. return out, errUnknown
  262. }
  263. v, n := protowire.ConsumeBytes(b)
  264. if n < 0 {
  265. return out, errDecode
  266. }
  267. o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
  268. Buf: v,
  269. Message: m.ProtoReflect(),
  270. })
  271. if err != nil {
  272. return out, err
  273. }
  274. out.n = n
  275. out.initialized = o.Flags&piface.UnmarshalInitialized != 0
  276. return out, nil
  277. }
  278. func sizeMessageValue(v pref.Value, tagsize int, opts marshalOptions) int {
  279. m := v.Message().Interface()
  280. return sizeMessage(m, tagsize, opts)
  281. }
  282. func appendMessageValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  283. m := v.Message().Interface()
  284. return appendMessage(b, m, wiretag, opts)
  285. }
  286. func consumeMessageValue(b []byte, v pref.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (pref.Value, unmarshalOutput, error) {
  287. m := v.Message().Interface()
  288. out, err := consumeMessage(b, m, wtyp, opts)
  289. return v, out, err
  290. }
  291. func isInitMessageValue(v pref.Value) error {
  292. m := v.Message().Interface()
  293. return proto.CheckInitialized(m)
  294. }
  295. var coderMessageValue = valueCoderFuncs{
  296. size: sizeMessageValue,
  297. marshal: appendMessageValue,
  298. unmarshal: consumeMessageValue,
  299. isInit: isInitMessageValue,
  300. merge: mergeMessageValue,
  301. }
  302. func sizeGroupValue(v pref.Value, tagsize int, opts marshalOptions) int {
  303. m := v.Message().Interface()
  304. return sizeGroup(m, tagsize, opts)
  305. }
  306. func appendGroupValue(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  307. m := v.Message().Interface()
  308. return appendGroup(b, m, wiretag, opts)
  309. }
  310. func consumeGroupValue(b []byte, v pref.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (pref.Value, unmarshalOutput, error) {
  311. m := v.Message().Interface()
  312. out, err := consumeGroup(b, m, num, wtyp, opts)
  313. return v, out, err
  314. }
  315. var coderGroupValue = valueCoderFuncs{
  316. size: sizeGroupValue,
  317. marshal: appendGroupValue,
  318. unmarshal: consumeGroupValue,
  319. isInit: isInitMessageValue,
  320. merge: mergeMessageValue,
  321. }
  322. func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  323. num := fd.Number()
  324. if mi := getMessageInfo(ft); mi != nil {
  325. funcs := pointerCoderFuncs{
  326. size: sizeGroupType,
  327. marshal: appendGroupType,
  328. unmarshal: consumeGroupType,
  329. merge: mergeMessage,
  330. }
  331. if needsInitCheck(mi.Desc) {
  332. funcs.isInit = isInitMessageInfo
  333. }
  334. return funcs
  335. } else {
  336. return pointerCoderFuncs{
  337. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  338. m := asMessage(p.AsValueOf(ft).Elem())
  339. return sizeGroup(m, f.tagsize, opts)
  340. },
  341. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  342. m := asMessage(p.AsValueOf(ft).Elem())
  343. return appendGroup(b, m, f.wiretag, opts)
  344. },
  345. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  346. mp := p.AsValueOf(ft).Elem()
  347. if mp.IsNil() {
  348. mp.Set(reflect.New(ft.Elem()))
  349. }
  350. return consumeGroup(b, asMessage(mp), num, wtyp, opts)
  351. },
  352. isInit: func(p pointer, f *coderFieldInfo) error {
  353. m := asMessage(p.AsValueOf(ft).Elem())
  354. return proto.CheckInitialized(m)
  355. },
  356. merge: mergeMessage,
  357. }
  358. }
  359. }
  360. func sizeGroupType(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  361. return 2*f.tagsize + f.mi.sizePointer(p.Elem(), opts)
  362. }
  363. func appendGroupType(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  364. b = protowire.AppendVarint(b, f.wiretag) // start group
  365. b, err := f.mi.marshalAppendPointer(b, p.Elem(), opts)
  366. b = protowire.AppendVarint(b, f.wiretag+1) // end group
  367. return b, err
  368. }
  369. func consumeGroupType(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
  370. if wtyp != protowire.StartGroupType {
  371. return out, errUnknown
  372. }
  373. if p.Elem().IsNil() {
  374. p.SetPointer(pointerOfValue(reflect.New(f.mi.GoReflectType.Elem())))
  375. }
  376. return f.mi.unmarshalPointer(b, p.Elem(), f.num, opts)
  377. }
  378. func sizeGroup(m proto.Message, tagsize int, _ marshalOptions) int {
  379. return 2*tagsize + proto.Size(m)
  380. }
  381. func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) {
  382. b = protowire.AppendVarint(b, wiretag) // start group
  383. b, err := opts.Options().MarshalAppend(b, m)
  384. b = protowire.AppendVarint(b, wiretag+1) // end group
  385. return b, err
  386. }
  387. func consumeGroup(b []byte, m proto.Message, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  388. if wtyp != protowire.StartGroupType {
  389. return out, errUnknown
  390. }
  391. b, n := protowire.ConsumeGroup(num, b)
  392. if n < 0 {
  393. return out, errDecode
  394. }
  395. o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
  396. Buf: b,
  397. Message: m.ProtoReflect(),
  398. })
  399. if err != nil {
  400. return out, err
  401. }
  402. out.n = n
  403. out.initialized = o.Flags&piface.UnmarshalInitialized != 0
  404. return out, nil
  405. }
  406. func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  407. if mi := getMessageInfo(ft); mi != nil {
  408. funcs := pointerCoderFuncs{
  409. size: sizeMessageSliceInfo,
  410. marshal: appendMessageSliceInfo,
  411. unmarshal: consumeMessageSliceInfo,
  412. merge: mergeMessageSlice,
  413. }
  414. if needsInitCheck(mi.Desc) {
  415. funcs.isInit = isInitMessageSliceInfo
  416. }
  417. return funcs
  418. }
  419. return pointerCoderFuncs{
  420. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  421. return sizeMessageSlice(p, ft, f.tagsize, opts)
  422. },
  423. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  424. return appendMessageSlice(b, p, f.wiretag, ft, opts)
  425. },
  426. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  427. return consumeMessageSlice(b, p, ft, wtyp, opts)
  428. },
  429. isInit: func(p pointer, f *coderFieldInfo) error {
  430. return isInitMessageSlice(p, ft)
  431. },
  432. merge: mergeMessageSlice,
  433. }
  434. }
  435. func sizeMessageSliceInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  436. s := p.PointerSlice()
  437. n := 0
  438. for _, v := range s {
  439. n += protowire.SizeBytes(f.mi.sizePointer(v, opts)) + f.tagsize
  440. }
  441. return n
  442. }
  443. func appendMessageSliceInfo(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  444. s := p.PointerSlice()
  445. var err error
  446. for _, v := range s {
  447. b = protowire.AppendVarint(b, f.wiretag)
  448. siz := f.mi.sizePointer(v, opts)
  449. b = protowire.AppendVarint(b, uint64(siz))
  450. b, err = f.mi.marshalAppendPointer(b, v, opts)
  451. if err != nil {
  452. return b, err
  453. }
  454. }
  455. return b, nil
  456. }
  457. func consumeMessageSliceInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (out unmarshalOutput, err error) {
  458. if wtyp != protowire.BytesType {
  459. return out, errUnknown
  460. }
  461. v, n := protowire.ConsumeBytes(b)
  462. if n < 0 {
  463. return out, errDecode
  464. }
  465. m := reflect.New(f.mi.GoReflectType.Elem()).Interface()
  466. mp := pointerOfIface(m)
  467. o, err := f.mi.unmarshalPointer(v, mp, 0, opts)
  468. if err != nil {
  469. return out, err
  470. }
  471. p.AppendPointerSlice(mp)
  472. out.n = n
  473. out.initialized = o.initialized
  474. return out, nil
  475. }
  476. func isInitMessageSliceInfo(p pointer, f *coderFieldInfo) error {
  477. s := p.PointerSlice()
  478. for _, v := range s {
  479. if err := f.mi.checkInitializedPointer(v); err != nil {
  480. return err
  481. }
  482. }
  483. return nil
  484. }
  485. func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, _ marshalOptions) int {
  486. s := p.PointerSlice()
  487. n := 0
  488. for _, v := range s {
  489. m := asMessage(v.AsValueOf(goType.Elem()))
  490. n += protowire.SizeBytes(proto.Size(m)) + tagsize
  491. }
  492. return n
  493. }
  494. func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) {
  495. s := p.PointerSlice()
  496. var err error
  497. for _, v := range s {
  498. m := asMessage(v.AsValueOf(goType.Elem()))
  499. b = protowire.AppendVarint(b, wiretag)
  500. siz := proto.Size(m)
  501. b = protowire.AppendVarint(b, uint64(siz))
  502. b, err = opts.Options().MarshalAppend(b, m)
  503. if err != nil {
  504. return b, err
  505. }
  506. }
  507. return b, nil
  508. }
  509. func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  510. if wtyp != protowire.BytesType {
  511. return out, errUnknown
  512. }
  513. v, n := protowire.ConsumeBytes(b)
  514. if n < 0 {
  515. return out, errDecode
  516. }
  517. mp := reflect.New(goType.Elem())
  518. o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
  519. Buf: v,
  520. Message: asMessage(mp).ProtoReflect(),
  521. })
  522. if err != nil {
  523. return out, err
  524. }
  525. p.AppendPointerSlice(pointerOfValue(mp))
  526. out.n = n
  527. out.initialized = o.Flags&piface.UnmarshalInitialized != 0
  528. return out, nil
  529. }
  530. func isInitMessageSlice(p pointer, goType reflect.Type) error {
  531. s := p.PointerSlice()
  532. for _, v := range s {
  533. m := asMessage(v.AsValueOf(goType.Elem()))
  534. if err := proto.CheckInitialized(m); err != nil {
  535. return err
  536. }
  537. }
  538. return nil
  539. }
  540. // Slices of messages
  541. func sizeMessageSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int {
  542. list := listv.List()
  543. n := 0
  544. for i, llen := 0, list.Len(); i < llen; i++ {
  545. m := list.Get(i).Message().Interface()
  546. n += protowire.SizeBytes(proto.Size(m)) + tagsize
  547. }
  548. return n
  549. }
  550. func appendMessageSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  551. list := listv.List()
  552. mopts := opts.Options()
  553. for i, llen := 0, list.Len(); i < llen; i++ {
  554. m := list.Get(i).Message().Interface()
  555. b = protowire.AppendVarint(b, wiretag)
  556. siz := proto.Size(m)
  557. b = protowire.AppendVarint(b, uint64(siz))
  558. var err error
  559. b, err = mopts.MarshalAppend(b, m)
  560. if err != nil {
  561. return b, err
  562. }
  563. }
  564. return b, nil
  565. }
  566. func consumeMessageSliceValue(b []byte, listv pref.Value, _ protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ pref.Value, out unmarshalOutput, err error) {
  567. list := listv.List()
  568. if wtyp != protowire.BytesType {
  569. return pref.Value{}, out, errUnknown
  570. }
  571. v, n := protowire.ConsumeBytes(b)
  572. if n < 0 {
  573. return pref.Value{}, out, errDecode
  574. }
  575. m := list.NewElement()
  576. o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
  577. Buf: v,
  578. Message: m.Message(),
  579. })
  580. if err != nil {
  581. return pref.Value{}, out, err
  582. }
  583. list.Append(m)
  584. out.n = n
  585. out.initialized = o.Flags&piface.UnmarshalInitialized != 0
  586. return listv, out, nil
  587. }
  588. func isInitMessageSliceValue(listv pref.Value) error {
  589. list := listv.List()
  590. for i, llen := 0, list.Len(); i < llen; i++ {
  591. m := list.Get(i).Message().Interface()
  592. if err := proto.CheckInitialized(m); err != nil {
  593. return err
  594. }
  595. }
  596. return nil
  597. }
  598. var coderMessageSliceValue = valueCoderFuncs{
  599. size: sizeMessageSliceValue,
  600. marshal: appendMessageSliceValue,
  601. unmarshal: consumeMessageSliceValue,
  602. isInit: isInitMessageSliceValue,
  603. merge: mergeMessageListValue,
  604. }
  605. func sizeGroupSliceValue(listv pref.Value, tagsize int, opts marshalOptions) int {
  606. list := listv.List()
  607. n := 0
  608. for i, llen := 0, list.Len(); i < llen; i++ {
  609. m := list.Get(i).Message().Interface()
  610. n += 2*tagsize + proto.Size(m)
  611. }
  612. return n
  613. }
  614. func appendGroupSliceValue(b []byte, listv pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error) {
  615. list := listv.List()
  616. mopts := opts.Options()
  617. for i, llen := 0, list.Len(); i < llen; i++ {
  618. m := list.Get(i).Message().Interface()
  619. b = protowire.AppendVarint(b, wiretag) // start group
  620. var err error
  621. b, err = mopts.MarshalAppend(b, m)
  622. if err != nil {
  623. return b, err
  624. }
  625. b = protowire.AppendVarint(b, wiretag+1) // end group
  626. }
  627. return b, nil
  628. }
  629. func consumeGroupSliceValue(b []byte, listv pref.Value, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (_ pref.Value, out unmarshalOutput, err error) {
  630. list := listv.List()
  631. if wtyp != protowire.StartGroupType {
  632. return pref.Value{}, out, errUnknown
  633. }
  634. b, n := protowire.ConsumeGroup(num, b)
  635. if n < 0 {
  636. return pref.Value{}, out, errDecode
  637. }
  638. m := list.NewElement()
  639. o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
  640. Buf: b,
  641. Message: m.Message(),
  642. })
  643. if err != nil {
  644. return pref.Value{}, out, err
  645. }
  646. list.Append(m)
  647. out.n = n
  648. out.initialized = o.Flags&piface.UnmarshalInitialized != 0
  649. return listv, out, nil
  650. }
  651. var coderGroupSliceValue = valueCoderFuncs{
  652. size: sizeGroupSliceValue,
  653. marshal: appendGroupSliceValue,
  654. unmarshal: consumeGroupSliceValue,
  655. isInit: isInitMessageSliceValue,
  656. merge: mergeMessageListValue,
  657. }
  658. func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  659. num := fd.Number()
  660. if mi := getMessageInfo(ft); mi != nil {
  661. funcs := pointerCoderFuncs{
  662. size: sizeGroupSliceInfo,
  663. marshal: appendGroupSliceInfo,
  664. unmarshal: consumeGroupSliceInfo,
  665. merge: mergeMessageSlice,
  666. }
  667. if needsInitCheck(mi.Desc) {
  668. funcs.isInit = isInitMessageSliceInfo
  669. }
  670. return funcs
  671. }
  672. return pointerCoderFuncs{
  673. size: func(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  674. return sizeGroupSlice(p, ft, f.tagsize, opts)
  675. },
  676. marshal: func(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  677. return appendGroupSlice(b, p, f.wiretag, ft, opts)
  678. },
  679. unmarshal: func(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  680. return consumeGroupSlice(b, p, num, wtyp, ft, opts)
  681. },
  682. isInit: func(p pointer, f *coderFieldInfo) error {
  683. return isInitMessageSlice(p, ft)
  684. },
  685. merge: mergeMessageSlice,
  686. }
  687. }
  688. func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, _ marshalOptions) int {
  689. s := p.PointerSlice()
  690. n := 0
  691. for _, v := range s {
  692. m := asMessage(v.AsValueOf(messageType.Elem()))
  693. n += 2*tagsize + proto.Size(m)
  694. }
  695. return n
  696. }
  697. func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) {
  698. s := p.PointerSlice()
  699. var err error
  700. for _, v := range s {
  701. m := asMessage(v.AsValueOf(messageType.Elem()))
  702. b = protowire.AppendVarint(b, wiretag) // start group
  703. b, err = opts.Options().MarshalAppend(b, m)
  704. if err != nil {
  705. return b, err
  706. }
  707. b = protowire.AppendVarint(b, wiretag+1) // end group
  708. }
  709. return b, nil
  710. }
  711. func consumeGroupSlice(b []byte, p pointer, num protowire.Number, wtyp protowire.Type, goType reflect.Type, opts unmarshalOptions) (out unmarshalOutput, err error) {
  712. if wtyp != protowire.StartGroupType {
  713. return out, errUnknown
  714. }
  715. b, n := protowire.ConsumeGroup(num, b)
  716. if n < 0 {
  717. return out, errDecode
  718. }
  719. mp := reflect.New(goType.Elem())
  720. o, err := opts.Options().UnmarshalState(piface.UnmarshalInput{
  721. Buf: b,
  722. Message: asMessage(mp).ProtoReflect(),
  723. })
  724. if err != nil {
  725. return out, err
  726. }
  727. p.AppendPointerSlice(pointerOfValue(mp))
  728. out.n = n
  729. out.initialized = o.Flags&piface.UnmarshalInitialized != 0
  730. return out, nil
  731. }
  732. func sizeGroupSliceInfo(p pointer, f *coderFieldInfo, opts marshalOptions) int {
  733. s := p.PointerSlice()
  734. n := 0
  735. for _, v := range s {
  736. n += 2*f.tagsize + f.mi.sizePointer(v, opts)
  737. }
  738. return n
  739. }
  740. func appendGroupSliceInfo(b []byte, p pointer, f *coderFieldInfo, opts marshalOptions) ([]byte, error) {
  741. s := p.PointerSlice()
  742. var err error
  743. for _, v := range s {
  744. b = protowire.AppendVarint(b, f.wiretag) // start group
  745. b, err = f.mi.marshalAppendPointer(b, v, opts)
  746. if err != nil {
  747. return b, err
  748. }
  749. b = protowire.AppendVarint(b, f.wiretag+1) // end group
  750. }
  751. return b, nil
  752. }
  753. func consumeGroupSliceInfo(b []byte, p pointer, wtyp protowire.Type, f *coderFieldInfo, opts unmarshalOptions) (unmarshalOutput, error) {
  754. if wtyp != protowire.StartGroupType {
  755. return unmarshalOutput{}, errUnknown
  756. }
  757. m := reflect.New(f.mi.GoReflectType.Elem()).Interface()
  758. mp := pointerOfIface(m)
  759. out, err := f.mi.unmarshalPointer(b, mp, f.num, opts)
  760. if err != nil {
  761. return out, err
  762. }
  763. p.AppendPointerSlice(mp)
  764. return out, nil
  765. }
  766. func asMessage(v reflect.Value) pref.ProtoMessage {
  767. if m, ok := v.Interface().(pref.ProtoMessage); ok {
  768. return m
  769. }
  770. return legacyWrapMessage(v).Interface()
  771. }