codec_tables.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. "google.golang.org/protobuf/internal/encoding/wire"
  9. "google.golang.org/protobuf/internal/strs"
  10. pref "google.golang.org/protobuf/reflect/protoreflect"
  11. )
  12. // pointerCoderFuncs is a set of pointer encoding functions.
  13. type pointerCoderFuncs struct {
  14. size func(p pointer, tagsize int, opts marshalOptions) int
  15. marshal func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error)
  16. unmarshal func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error)
  17. isInit func(p pointer) error
  18. }
  19. // valueCoderFuncs is a set of protoreflect.Value encoding functions.
  20. type valueCoderFuncs struct {
  21. size func(v pref.Value, tagsize int, opts marshalOptions) int
  22. marshal func(b []byte, v pref.Value, wiretag uint64, opts marshalOptions) ([]byte, error)
  23. unmarshal func(b []byte, v pref.Value, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (pref.Value, int, error)
  24. isInit func(v pref.Value) error
  25. }
  26. // fieldCoder returns pointer functions for a field, used for operating on
  27. // struct fields.
  28. func fieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs {
  29. switch {
  30. case fd.IsMap():
  31. return encoderFuncsForMap(fd, ft)
  32. case fd.Cardinality() == pref.Repeated && !fd.IsPacked():
  33. // Repeated fields (not packed).
  34. if ft.Kind() != reflect.Slice {
  35. break
  36. }
  37. ft := ft.Elem()
  38. switch fd.Kind() {
  39. case pref.BoolKind:
  40. if ft.Kind() == reflect.Bool {
  41. return coderBoolSlice
  42. }
  43. case pref.EnumKind:
  44. if ft.Kind() == reflect.Int32 {
  45. return coderEnumSlice
  46. }
  47. case pref.Int32Kind:
  48. if ft.Kind() == reflect.Int32 {
  49. return coderInt32Slice
  50. }
  51. case pref.Sint32Kind:
  52. if ft.Kind() == reflect.Int32 {
  53. return coderSint32Slice
  54. }
  55. case pref.Uint32Kind:
  56. if ft.Kind() == reflect.Uint32 {
  57. return coderUint32Slice
  58. }
  59. case pref.Int64Kind:
  60. if ft.Kind() == reflect.Int64 {
  61. return coderInt64Slice
  62. }
  63. case pref.Sint64Kind:
  64. if ft.Kind() == reflect.Int64 {
  65. return coderSint64Slice
  66. }
  67. case pref.Uint64Kind:
  68. if ft.Kind() == reflect.Uint64 {
  69. return coderUint64Slice
  70. }
  71. case pref.Sfixed32Kind:
  72. if ft.Kind() == reflect.Int32 {
  73. return coderSfixed32Slice
  74. }
  75. case pref.Fixed32Kind:
  76. if ft.Kind() == reflect.Uint32 {
  77. return coderFixed32Slice
  78. }
  79. case pref.FloatKind:
  80. if ft.Kind() == reflect.Float32 {
  81. return coderFloatSlice
  82. }
  83. case pref.Sfixed64Kind:
  84. if ft.Kind() == reflect.Int64 {
  85. return coderSfixed64Slice
  86. }
  87. case pref.Fixed64Kind:
  88. if ft.Kind() == reflect.Uint64 {
  89. return coderFixed64Slice
  90. }
  91. case pref.DoubleKind:
  92. if ft.Kind() == reflect.Float64 {
  93. return coderDoubleSlice
  94. }
  95. case pref.StringKind:
  96. if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
  97. return coderStringSliceValidateUTF8
  98. }
  99. if ft.Kind() == reflect.String {
  100. return coderStringSlice
  101. }
  102. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) {
  103. return coderBytesSliceValidateUTF8
  104. }
  105. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
  106. return coderBytesSlice
  107. }
  108. case pref.BytesKind:
  109. if ft.Kind() == reflect.String {
  110. return coderStringSlice
  111. }
  112. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
  113. return coderBytesSlice
  114. }
  115. case pref.MessageKind:
  116. return makeMessageSliceFieldCoder(fd, ft)
  117. case pref.GroupKind:
  118. return makeGroupSliceFieldCoder(fd, ft)
  119. }
  120. case fd.Cardinality() == pref.Repeated && fd.IsPacked():
  121. // Packed repeated fields.
  122. //
  123. // Only repeated fields of primitive numeric types
  124. // (Varint, Fixed32, or Fixed64 wire type) can be packed.
  125. if ft.Kind() != reflect.Slice {
  126. break
  127. }
  128. ft := ft.Elem()
  129. switch fd.Kind() {
  130. case pref.BoolKind:
  131. if ft.Kind() == reflect.Bool {
  132. return coderBoolPackedSlice
  133. }
  134. case pref.EnumKind:
  135. if ft.Kind() == reflect.Int32 {
  136. return coderEnumPackedSlice
  137. }
  138. case pref.Int32Kind:
  139. if ft.Kind() == reflect.Int32 {
  140. return coderInt32PackedSlice
  141. }
  142. case pref.Sint32Kind:
  143. if ft.Kind() == reflect.Int32 {
  144. return coderSint32PackedSlice
  145. }
  146. case pref.Uint32Kind:
  147. if ft.Kind() == reflect.Uint32 {
  148. return coderUint32PackedSlice
  149. }
  150. case pref.Int64Kind:
  151. if ft.Kind() == reflect.Int64 {
  152. return coderInt64PackedSlice
  153. }
  154. case pref.Sint64Kind:
  155. if ft.Kind() == reflect.Int64 {
  156. return coderSint64PackedSlice
  157. }
  158. case pref.Uint64Kind:
  159. if ft.Kind() == reflect.Uint64 {
  160. return coderUint64PackedSlice
  161. }
  162. case pref.Sfixed32Kind:
  163. if ft.Kind() == reflect.Int32 {
  164. return coderSfixed32PackedSlice
  165. }
  166. case pref.Fixed32Kind:
  167. if ft.Kind() == reflect.Uint32 {
  168. return coderFixed32PackedSlice
  169. }
  170. case pref.FloatKind:
  171. if ft.Kind() == reflect.Float32 {
  172. return coderFloatPackedSlice
  173. }
  174. case pref.Sfixed64Kind:
  175. if ft.Kind() == reflect.Int64 {
  176. return coderSfixed64PackedSlice
  177. }
  178. case pref.Fixed64Kind:
  179. if ft.Kind() == reflect.Uint64 {
  180. return coderFixed64PackedSlice
  181. }
  182. case pref.DoubleKind:
  183. if ft.Kind() == reflect.Float64 {
  184. return coderDoublePackedSlice
  185. }
  186. }
  187. case fd.Kind() == pref.MessageKind:
  188. return makeMessageFieldCoder(fd, ft)
  189. case fd.Kind() == pref.GroupKind:
  190. return makeGroupFieldCoder(fd, ft)
  191. case fd.Syntax() == pref.Proto3 && fd.ContainingOneof() == nil:
  192. // Populated oneof fields always encode even if set to the zero value,
  193. // which normally are not encoded in proto3.
  194. switch fd.Kind() {
  195. case pref.BoolKind:
  196. if ft.Kind() == reflect.Bool {
  197. return coderBoolNoZero
  198. }
  199. case pref.EnumKind:
  200. if ft.Kind() == reflect.Int32 {
  201. return coderEnumNoZero
  202. }
  203. case pref.Int32Kind:
  204. if ft.Kind() == reflect.Int32 {
  205. return coderInt32NoZero
  206. }
  207. case pref.Sint32Kind:
  208. if ft.Kind() == reflect.Int32 {
  209. return coderSint32NoZero
  210. }
  211. case pref.Uint32Kind:
  212. if ft.Kind() == reflect.Uint32 {
  213. return coderUint32NoZero
  214. }
  215. case pref.Int64Kind:
  216. if ft.Kind() == reflect.Int64 {
  217. return coderInt64NoZero
  218. }
  219. case pref.Sint64Kind:
  220. if ft.Kind() == reflect.Int64 {
  221. return coderSint64NoZero
  222. }
  223. case pref.Uint64Kind:
  224. if ft.Kind() == reflect.Uint64 {
  225. return coderUint64NoZero
  226. }
  227. case pref.Sfixed32Kind:
  228. if ft.Kind() == reflect.Int32 {
  229. return coderSfixed32NoZero
  230. }
  231. case pref.Fixed32Kind:
  232. if ft.Kind() == reflect.Uint32 {
  233. return coderFixed32NoZero
  234. }
  235. case pref.FloatKind:
  236. if ft.Kind() == reflect.Float32 {
  237. return coderFloatNoZero
  238. }
  239. case pref.Sfixed64Kind:
  240. if ft.Kind() == reflect.Int64 {
  241. return coderSfixed64NoZero
  242. }
  243. case pref.Fixed64Kind:
  244. if ft.Kind() == reflect.Uint64 {
  245. return coderFixed64NoZero
  246. }
  247. case pref.DoubleKind:
  248. if ft.Kind() == reflect.Float64 {
  249. return coderDoubleNoZero
  250. }
  251. case pref.StringKind:
  252. if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
  253. return coderStringNoZeroValidateUTF8
  254. }
  255. if ft.Kind() == reflect.String {
  256. return coderStringNoZero
  257. }
  258. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) {
  259. return coderBytesNoZeroValidateUTF8
  260. }
  261. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
  262. return coderBytesNoZero
  263. }
  264. case pref.BytesKind:
  265. if ft.Kind() == reflect.String {
  266. return coderStringNoZero
  267. }
  268. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
  269. return coderBytesNoZero
  270. }
  271. }
  272. case ft.Kind() == reflect.Ptr:
  273. ft := ft.Elem()
  274. switch fd.Kind() {
  275. case pref.BoolKind:
  276. if ft.Kind() == reflect.Bool {
  277. return coderBoolPtr
  278. }
  279. case pref.EnumKind:
  280. if ft.Kind() == reflect.Int32 {
  281. return coderEnumPtr
  282. }
  283. case pref.Int32Kind:
  284. if ft.Kind() == reflect.Int32 {
  285. return coderInt32Ptr
  286. }
  287. case pref.Sint32Kind:
  288. if ft.Kind() == reflect.Int32 {
  289. return coderSint32Ptr
  290. }
  291. case pref.Uint32Kind:
  292. if ft.Kind() == reflect.Uint32 {
  293. return coderUint32Ptr
  294. }
  295. case pref.Int64Kind:
  296. if ft.Kind() == reflect.Int64 {
  297. return coderInt64Ptr
  298. }
  299. case pref.Sint64Kind:
  300. if ft.Kind() == reflect.Int64 {
  301. return coderSint64Ptr
  302. }
  303. case pref.Uint64Kind:
  304. if ft.Kind() == reflect.Uint64 {
  305. return coderUint64Ptr
  306. }
  307. case pref.Sfixed32Kind:
  308. if ft.Kind() == reflect.Int32 {
  309. return coderSfixed32Ptr
  310. }
  311. case pref.Fixed32Kind:
  312. if ft.Kind() == reflect.Uint32 {
  313. return coderFixed32Ptr
  314. }
  315. case pref.FloatKind:
  316. if ft.Kind() == reflect.Float32 {
  317. return coderFloatPtr
  318. }
  319. case pref.Sfixed64Kind:
  320. if ft.Kind() == reflect.Int64 {
  321. return coderSfixed64Ptr
  322. }
  323. case pref.Fixed64Kind:
  324. if ft.Kind() == reflect.Uint64 {
  325. return coderFixed64Ptr
  326. }
  327. case pref.DoubleKind:
  328. if ft.Kind() == reflect.Float64 {
  329. return coderDoublePtr
  330. }
  331. case pref.StringKind:
  332. if ft.Kind() == reflect.String {
  333. return coderStringPtr
  334. }
  335. case pref.BytesKind:
  336. if ft.Kind() == reflect.String {
  337. return coderStringPtr
  338. }
  339. }
  340. default:
  341. switch fd.Kind() {
  342. case pref.BoolKind:
  343. if ft.Kind() == reflect.Bool {
  344. return coderBool
  345. }
  346. case pref.EnumKind:
  347. if ft.Kind() == reflect.Int32 {
  348. return coderEnum
  349. }
  350. case pref.Int32Kind:
  351. if ft.Kind() == reflect.Int32 {
  352. return coderInt32
  353. }
  354. case pref.Sint32Kind:
  355. if ft.Kind() == reflect.Int32 {
  356. return coderSint32
  357. }
  358. case pref.Uint32Kind:
  359. if ft.Kind() == reflect.Uint32 {
  360. return coderUint32
  361. }
  362. case pref.Int64Kind:
  363. if ft.Kind() == reflect.Int64 {
  364. return coderInt64
  365. }
  366. case pref.Sint64Kind:
  367. if ft.Kind() == reflect.Int64 {
  368. return coderSint64
  369. }
  370. case pref.Uint64Kind:
  371. if ft.Kind() == reflect.Uint64 {
  372. return coderUint64
  373. }
  374. case pref.Sfixed32Kind:
  375. if ft.Kind() == reflect.Int32 {
  376. return coderSfixed32
  377. }
  378. case pref.Fixed32Kind:
  379. if ft.Kind() == reflect.Uint32 {
  380. return coderFixed32
  381. }
  382. case pref.FloatKind:
  383. if ft.Kind() == reflect.Float32 {
  384. return coderFloat
  385. }
  386. case pref.Sfixed64Kind:
  387. if ft.Kind() == reflect.Int64 {
  388. return coderSfixed64
  389. }
  390. case pref.Fixed64Kind:
  391. if ft.Kind() == reflect.Uint64 {
  392. return coderFixed64
  393. }
  394. case pref.DoubleKind:
  395. if ft.Kind() == reflect.Float64 {
  396. return coderDouble
  397. }
  398. case pref.StringKind:
  399. if ft.Kind() == reflect.String && strs.EnforceUTF8(fd) {
  400. return coderStringValidateUTF8
  401. }
  402. if ft.Kind() == reflect.String {
  403. return coderString
  404. }
  405. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 && strs.EnforceUTF8(fd) {
  406. return coderBytesValidateUTF8
  407. }
  408. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
  409. return coderBytes
  410. }
  411. case pref.BytesKind:
  412. if ft.Kind() == reflect.String {
  413. return coderString
  414. }
  415. if ft.Kind() == reflect.Slice && ft.Elem().Kind() == reflect.Uint8 {
  416. return coderBytes
  417. }
  418. }
  419. }
  420. panic(fmt.Sprintf("invalid type: no encoder for %v %v %v/%v", fd.FullName(), fd.Cardinality(), fd.Kind(), ft))
  421. }
  422. // encoderFuncsForValue returns value functions for a field, used for
  423. // extension values and map encoding.
  424. func encoderFuncsForValue(fd pref.FieldDescriptor) valueCoderFuncs {
  425. switch {
  426. case fd.Cardinality() == pref.Repeated && !fd.IsPacked():
  427. switch fd.Kind() {
  428. case pref.BoolKind:
  429. return coderBoolSliceValue
  430. case pref.EnumKind:
  431. return coderEnumSliceValue
  432. case pref.Int32Kind:
  433. return coderInt32SliceValue
  434. case pref.Sint32Kind:
  435. return coderSint32SliceValue
  436. case pref.Uint32Kind:
  437. return coderUint32SliceValue
  438. case pref.Int64Kind:
  439. return coderInt64SliceValue
  440. case pref.Sint64Kind:
  441. return coderSint64SliceValue
  442. case pref.Uint64Kind:
  443. return coderUint64SliceValue
  444. case pref.Sfixed32Kind:
  445. return coderSfixed32SliceValue
  446. case pref.Fixed32Kind:
  447. return coderFixed32SliceValue
  448. case pref.FloatKind:
  449. return coderFloatSliceValue
  450. case pref.Sfixed64Kind:
  451. return coderSfixed64SliceValue
  452. case pref.Fixed64Kind:
  453. return coderFixed64SliceValue
  454. case pref.DoubleKind:
  455. return coderDoubleSliceValue
  456. case pref.StringKind:
  457. // We don't have a UTF-8 validating coder for repeated string fields.
  458. // Value coders are used for extensions and maps.
  459. // Extensions are never proto3, and maps never contain lists.
  460. return coderStringSliceValue
  461. case pref.BytesKind:
  462. return coderBytesSliceValue
  463. case pref.MessageKind:
  464. return coderMessageSliceValue
  465. case pref.GroupKind:
  466. return coderGroupSliceValue
  467. }
  468. case fd.Cardinality() == pref.Repeated && fd.IsPacked():
  469. switch fd.Kind() {
  470. case pref.BoolKind:
  471. return coderBoolPackedSliceValue
  472. case pref.EnumKind:
  473. return coderEnumPackedSliceValue
  474. case pref.Int32Kind:
  475. return coderInt32PackedSliceValue
  476. case pref.Sint32Kind:
  477. return coderSint32PackedSliceValue
  478. case pref.Uint32Kind:
  479. return coderUint32PackedSliceValue
  480. case pref.Int64Kind:
  481. return coderInt64PackedSliceValue
  482. case pref.Sint64Kind:
  483. return coderSint64PackedSliceValue
  484. case pref.Uint64Kind:
  485. return coderUint64PackedSliceValue
  486. case pref.Sfixed32Kind:
  487. return coderSfixed32PackedSliceValue
  488. case pref.Fixed32Kind:
  489. return coderFixed32PackedSliceValue
  490. case pref.FloatKind:
  491. return coderFloatPackedSliceValue
  492. case pref.Sfixed64Kind:
  493. return coderSfixed64PackedSliceValue
  494. case pref.Fixed64Kind:
  495. return coderFixed64PackedSliceValue
  496. case pref.DoubleKind:
  497. return coderDoublePackedSliceValue
  498. }
  499. default:
  500. switch fd.Kind() {
  501. default:
  502. case pref.BoolKind:
  503. return coderBoolValue
  504. case pref.EnumKind:
  505. return coderEnumValue
  506. case pref.Int32Kind:
  507. return coderInt32Value
  508. case pref.Sint32Kind:
  509. return coderSint32Value
  510. case pref.Uint32Kind:
  511. return coderUint32Value
  512. case pref.Int64Kind:
  513. return coderInt64Value
  514. case pref.Sint64Kind:
  515. return coderSint64Value
  516. case pref.Uint64Kind:
  517. return coderUint64Value
  518. case pref.Sfixed32Kind:
  519. return coderSfixed32Value
  520. case pref.Fixed32Kind:
  521. return coderFixed32Value
  522. case pref.FloatKind:
  523. return coderFloatValue
  524. case pref.Sfixed64Kind:
  525. return coderSfixed64Value
  526. case pref.Fixed64Kind:
  527. return coderFixed64Value
  528. case pref.DoubleKind:
  529. return coderDoubleValue
  530. case pref.StringKind:
  531. if strs.EnforceUTF8(fd) {
  532. return coderStringValueValidateUTF8
  533. }
  534. return coderStringValue
  535. case pref.BytesKind:
  536. return coderBytesValue
  537. case pref.MessageKind:
  538. return coderMessageValue
  539. case pref.GroupKind:
  540. return coderGroupValue
  541. }
  542. }
  543. panic(fmt.Sprintf("invalid field: no encoder for %v %v %v", fd.FullName(), fd.Cardinality(), fd.Kind()))
  544. }