另客网go项目公用的代码库

reflect_extension.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "github.com/modern-go/reflect2"
  5. "reflect"
  6. "sort"
  7. "strings"
  8. "unicode"
  9. "unsafe"
  10. )
  11. var typeDecoders = map[string]ValDecoder{}
  12. var fieldDecoders = map[string]ValDecoder{}
  13. var typeEncoders = map[string]ValEncoder{}
  14. var fieldEncoders = map[string]ValEncoder{}
  15. var extensions = []Extension{}
  16. // StructDescriptor describe how should we encode/decode the struct
  17. type StructDescriptor struct {
  18. Type reflect2.Type
  19. Fields []*Binding
  20. }
  21. // GetField get one field from the descriptor by its name.
  22. // Can not use map here to keep field orders.
  23. func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding {
  24. for _, binding := range structDescriptor.Fields {
  25. if binding.Field.Name() == fieldName {
  26. return binding
  27. }
  28. }
  29. return nil
  30. }
  31. // Binding describe how should we encode/decode the struct field
  32. type Binding struct {
  33. levels []int
  34. Field reflect2.StructField
  35. FromNames []string
  36. ToNames []string
  37. Encoder ValEncoder
  38. Decoder ValDecoder
  39. }
  40. // Extension the one for all SPI. Customize encoding/decoding by specifying alternate encoder/decoder.
  41. // Can also rename fields by UpdateStructDescriptor.
  42. type Extension interface {
  43. UpdateStructDescriptor(structDescriptor *StructDescriptor)
  44. CreateMapKeyDecoder(typ reflect2.Type) ValDecoder
  45. CreateMapKeyEncoder(typ reflect2.Type) ValEncoder
  46. CreateDecoder(typ reflect2.Type) ValDecoder
  47. CreateEncoder(typ reflect2.Type) ValEncoder
  48. DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder
  49. DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder
  50. }
  51. // DummyExtension embed this type get dummy implementation for all methods of Extension
  52. type DummyExtension struct {
  53. }
  54. // UpdateStructDescriptor No-op
  55. func (extension *DummyExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
  56. }
  57. // CreateMapKeyDecoder No-op
  58. func (extension *DummyExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder {
  59. return nil
  60. }
  61. // CreateMapKeyEncoder No-op
  62. func (extension *DummyExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder {
  63. return nil
  64. }
  65. // CreateDecoder No-op
  66. func (extension *DummyExtension) CreateDecoder(typ reflect2.Type) ValDecoder {
  67. return nil
  68. }
  69. // CreateEncoder No-op
  70. func (extension *DummyExtension) CreateEncoder(typ reflect2.Type) ValEncoder {
  71. return nil
  72. }
  73. // DecorateDecoder No-op
  74. func (extension *DummyExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder {
  75. return decoder
  76. }
  77. // DecorateEncoder No-op
  78. func (extension *DummyExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder {
  79. return encoder
  80. }
  81. type EncoderExtension map[reflect2.Type]ValEncoder
  82. // UpdateStructDescriptor No-op
  83. func (extension EncoderExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
  84. }
  85. // CreateDecoder No-op
  86. func (extension EncoderExtension) CreateDecoder(typ reflect2.Type) ValDecoder {
  87. return nil
  88. }
  89. // CreateEncoder get encoder from map
  90. func (extension EncoderExtension) CreateEncoder(typ reflect2.Type) ValEncoder {
  91. return extension[typ]
  92. }
  93. // CreateMapKeyDecoder No-op
  94. func (extension EncoderExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder {
  95. return nil
  96. }
  97. // CreateMapKeyEncoder No-op
  98. func (extension EncoderExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder {
  99. return nil
  100. }
  101. // DecorateDecoder No-op
  102. func (extension EncoderExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder {
  103. return decoder
  104. }
  105. // DecorateEncoder No-op
  106. func (extension EncoderExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder {
  107. return encoder
  108. }
  109. type DecoderExtension map[reflect2.Type]ValDecoder
  110. // UpdateStructDescriptor No-op
  111. func (extension DecoderExtension) UpdateStructDescriptor(structDescriptor *StructDescriptor) {
  112. }
  113. // CreateMapKeyDecoder No-op
  114. func (extension DecoderExtension) CreateMapKeyDecoder(typ reflect2.Type) ValDecoder {
  115. return nil
  116. }
  117. // CreateMapKeyEncoder No-op
  118. func (extension DecoderExtension) CreateMapKeyEncoder(typ reflect2.Type) ValEncoder {
  119. return nil
  120. }
  121. // CreateDecoder get decoder from map
  122. func (extension DecoderExtension) CreateDecoder(typ reflect2.Type) ValDecoder {
  123. return extension[typ]
  124. }
  125. // CreateEncoder No-op
  126. func (extension DecoderExtension) CreateEncoder(typ reflect2.Type) ValEncoder {
  127. return nil
  128. }
  129. // DecorateDecoder No-op
  130. func (extension DecoderExtension) DecorateDecoder(typ reflect2.Type, decoder ValDecoder) ValDecoder {
  131. return decoder
  132. }
  133. // DecorateEncoder No-op
  134. func (extension DecoderExtension) DecorateEncoder(typ reflect2.Type, encoder ValEncoder) ValEncoder {
  135. return encoder
  136. }
  137. type funcDecoder struct {
  138. fun DecoderFunc
  139. }
  140. func (decoder *funcDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  141. decoder.fun(ptr, iter)
  142. }
  143. type funcEncoder struct {
  144. fun EncoderFunc
  145. isEmptyFunc func(ptr unsafe.Pointer) bool
  146. }
  147. func (encoder *funcEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  148. encoder.fun(ptr, stream)
  149. }
  150. func (encoder *funcEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  151. if encoder.isEmptyFunc == nil {
  152. return false
  153. }
  154. return encoder.isEmptyFunc(ptr)
  155. }
  156. // DecoderFunc the function form of TypeDecoder
  157. type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
  158. // EncoderFunc the function form of TypeEncoder
  159. type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
  160. // RegisterTypeDecoderFunc register TypeDecoder for a type with function
  161. func RegisterTypeDecoderFunc(typ string, fun DecoderFunc) {
  162. typeDecoders[typ] = &funcDecoder{fun}
  163. }
  164. // RegisterTypeDecoder register TypeDecoder for a typ
  165. func RegisterTypeDecoder(typ string, decoder ValDecoder) {
  166. typeDecoders[typ] = decoder
  167. }
  168. // RegisterFieldDecoderFunc register TypeDecoder for a struct field with function
  169. func RegisterFieldDecoderFunc(typ string, field string, fun DecoderFunc) {
  170. RegisterFieldDecoder(typ, field, &funcDecoder{fun})
  171. }
  172. // RegisterFieldDecoder register TypeDecoder for a struct field
  173. func RegisterFieldDecoder(typ string, field string, decoder ValDecoder) {
  174. fieldDecoders[fmt.Sprintf("%s/%s", typ, field)] = decoder
  175. }
  176. // RegisterTypeEncoderFunc register TypeEncoder for a type with encode/isEmpty function
  177. func RegisterTypeEncoderFunc(typ string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
  178. typeEncoders[typ] = &funcEncoder{fun, isEmptyFunc}
  179. }
  180. // RegisterTypeEncoder register TypeEncoder for a type
  181. func RegisterTypeEncoder(typ string, encoder ValEncoder) {
  182. typeEncoders[typ] = encoder
  183. }
  184. // RegisterFieldEncoderFunc register TypeEncoder for a struct field with encode/isEmpty function
  185. func RegisterFieldEncoderFunc(typ string, field string, fun EncoderFunc, isEmptyFunc func(unsafe.Pointer) bool) {
  186. RegisterFieldEncoder(typ, field, &funcEncoder{fun, isEmptyFunc})
  187. }
  188. // RegisterFieldEncoder register TypeEncoder for a struct field
  189. func RegisterFieldEncoder(typ string, field string, encoder ValEncoder) {
  190. fieldEncoders[fmt.Sprintf("%s/%s", typ, field)] = encoder
  191. }
  192. // RegisterExtension register extension
  193. func RegisterExtension(extension Extension) {
  194. extensions = append(extensions, extension)
  195. }
  196. func getTypeDecoderFromExtension(ctx *ctx, typ reflect2.Type) ValDecoder {
  197. decoder := _getTypeDecoderFromExtension(ctx, typ)
  198. if decoder != nil {
  199. for _, extension := range extensions {
  200. decoder = extension.DecorateDecoder(typ, decoder)
  201. }
  202. decoder = ctx.decoderExtension.DecorateDecoder(typ, decoder)
  203. for _, extension := range ctx.extraExtensions {
  204. decoder = extension.DecorateDecoder(typ, decoder)
  205. }
  206. }
  207. return decoder
  208. }
  209. func _getTypeDecoderFromExtension(ctx *ctx, typ reflect2.Type) ValDecoder {
  210. for _, extension := range extensions {
  211. decoder := extension.CreateDecoder(typ)
  212. if decoder != nil {
  213. return decoder
  214. }
  215. }
  216. decoder := ctx.decoderExtension.CreateDecoder(typ)
  217. if decoder != nil {
  218. return decoder
  219. }
  220. for _, extension := range ctx.extraExtensions {
  221. decoder := extension.CreateDecoder(typ)
  222. if decoder != nil {
  223. return decoder
  224. }
  225. }
  226. typeName := typ.String()
  227. decoder = typeDecoders[typeName]
  228. if decoder != nil {
  229. return decoder
  230. }
  231. if typ.Kind() == reflect.Ptr {
  232. ptrType := typ.(*reflect2.UnsafePtrType)
  233. decoder := typeDecoders[ptrType.Elem().String()]
  234. if decoder != nil {
  235. return &OptionalDecoder{ptrType.Elem(), decoder}
  236. }
  237. }
  238. return nil
  239. }
  240. func getTypeEncoderFromExtension(ctx *ctx, typ reflect2.Type) ValEncoder {
  241. encoder := _getTypeEncoderFromExtension(ctx, typ)
  242. if encoder != nil {
  243. for _, extension := range extensions {
  244. encoder = extension.DecorateEncoder(typ, encoder)
  245. }
  246. encoder = ctx.encoderExtension.DecorateEncoder(typ, encoder)
  247. for _, extension := range ctx.extraExtensions {
  248. encoder = extension.DecorateEncoder(typ, encoder)
  249. }
  250. }
  251. return encoder
  252. }
  253. func _getTypeEncoderFromExtension(ctx *ctx, typ reflect2.Type) ValEncoder {
  254. for _, extension := range extensions {
  255. encoder := extension.CreateEncoder(typ)
  256. if encoder != nil {
  257. return encoder
  258. }
  259. }
  260. encoder := ctx.encoderExtension.CreateEncoder(typ)
  261. if encoder != nil {
  262. return encoder
  263. }
  264. for _, extension := range ctx.extraExtensions {
  265. encoder := extension.CreateEncoder(typ)
  266. if encoder != nil {
  267. return encoder
  268. }
  269. }
  270. typeName := typ.String()
  271. encoder = typeEncoders[typeName]
  272. if encoder != nil {
  273. return encoder
  274. }
  275. if typ.Kind() == reflect.Ptr {
  276. typePtr := typ.(*reflect2.UnsafePtrType)
  277. encoder := typeEncoders[typePtr.Elem().String()]
  278. if encoder != nil {
  279. return &OptionalEncoder{encoder}
  280. }
  281. }
  282. return nil
  283. }
  284. func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
  285. structType := typ.(*reflect2.UnsafeStructType)
  286. embeddedBindings := []*Binding{}
  287. bindings := []*Binding{}
  288. for i := 0; i < structType.NumField(); i++ {
  289. field := structType.Field(i)
  290. tag, hastag := field.Tag().Lookup(ctx.getTagKey())
  291. if ctx.onlyTaggedField && !hastag {
  292. continue
  293. }
  294. tagParts := strings.Split(tag, ",")
  295. if tag == "-" {
  296. continue
  297. }
  298. if field.Anonymous() && (tag == "" || tagParts[0] == "") {
  299. if field.Type().Kind() == reflect.Struct {
  300. structDescriptor := describeStruct(ctx, field.Type())
  301. for _, binding := range structDescriptor.Fields {
  302. binding.levels = append([]int{i}, binding.levels...)
  303. omitempty := binding.Encoder.(*structFieldEncoder).omitempty
  304. binding.Encoder = &structFieldEncoder{field, binding.Encoder, omitempty}
  305. binding.Decoder = &structFieldDecoder{field, binding.Decoder}
  306. embeddedBindings = append(embeddedBindings, binding)
  307. }
  308. continue
  309. } else if field.Type().Kind() == reflect.Ptr {
  310. ptrType := field.Type().(*reflect2.UnsafePtrType)
  311. if ptrType.Elem().Kind() == reflect.Struct {
  312. structDescriptor := describeStruct(ctx, ptrType.Elem())
  313. for _, binding := range structDescriptor.Fields {
  314. binding.levels = append([]int{i}, binding.levels...)
  315. omitempty := binding.Encoder.(*structFieldEncoder).omitempty
  316. binding.Encoder = &dereferenceEncoder{binding.Encoder}
  317. binding.Encoder = &structFieldEncoder{field, binding.Encoder, omitempty}
  318. binding.Decoder = &dereferenceDecoder{ptrType.Elem(), binding.Decoder}
  319. binding.Decoder = &structFieldDecoder{field, binding.Decoder}
  320. embeddedBindings = append(embeddedBindings, binding)
  321. }
  322. continue
  323. }
  324. }
  325. }
  326. fieldNames := calcFieldNames(field.Name(), tagParts[0], tag)
  327. fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name())
  328. decoder := fieldDecoders[fieldCacheKey]
  329. if decoder == nil {
  330. decoder = decoderOfType(ctx.append(field.Name()), field.Type())
  331. }
  332. encoder := fieldEncoders[fieldCacheKey]
  333. if encoder == nil {
  334. encoder = encoderOfType(ctx.append(field.Name()), field.Type())
  335. }
  336. binding := &Binding{
  337. Field: field,
  338. FromNames: fieldNames,
  339. ToNames: fieldNames,
  340. Decoder: decoder,
  341. Encoder: encoder,
  342. }
  343. binding.levels = []int{i}
  344. bindings = append(bindings, binding)
  345. }
  346. return createStructDescriptor(ctx, typ, bindings, embeddedBindings)
  347. }
  348. func createStructDescriptor(ctx *ctx, typ reflect2.Type, bindings []*Binding, embeddedBindings []*Binding) *StructDescriptor {
  349. structDescriptor := &StructDescriptor{
  350. Type: typ,
  351. Fields: bindings,
  352. }
  353. for _, extension := range extensions {
  354. extension.UpdateStructDescriptor(structDescriptor)
  355. }
  356. ctx.encoderExtension.UpdateStructDescriptor(structDescriptor)
  357. ctx.decoderExtension.UpdateStructDescriptor(structDescriptor)
  358. for _, extension := range ctx.extraExtensions {
  359. extension.UpdateStructDescriptor(structDescriptor)
  360. }
  361. processTags(structDescriptor, ctx.frozenConfig)
  362. // merge normal & embedded bindings & sort with original order
  363. allBindings := sortableBindings(append(embeddedBindings, structDescriptor.Fields...))
  364. sort.Sort(allBindings)
  365. structDescriptor.Fields = allBindings
  366. return structDescriptor
  367. }
  368. type sortableBindings []*Binding
  369. func (bindings sortableBindings) Len() int {
  370. return len(bindings)
  371. }
  372. func (bindings sortableBindings) Less(i, j int) bool {
  373. left := bindings[i].levels
  374. right := bindings[j].levels
  375. k := 0
  376. for {
  377. if left[k] < right[k] {
  378. return true
  379. } else if left[k] > right[k] {
  380. return false
  381. }
  382. k++
  383. }
  384. }
  385. func (bindings sortableBindings) Swap(i, j int) {
  386. bindings[i], bindings[j] = bindings[j], bindings[i]
  387. }
  388. func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
  389. for _, binding := range structDescriptor.Fields {
  390. shouldOmitEmpty := false
  391. tagParts := strings.Split(binding.Field.Tag().Get(cfg.getTagKey()), ",")
  392. for _, tagPart := range tagParts[1:] {
  393. if tagPart == "omitempty" {
  394. shouldOmitEmpty = true
  395. } else if tagPart == "string" {
  396. if binding.Field.Type().Kind() == reflect.String {
  397. binding.Decoder = &stringModeStringDecoder{binding.Decoder, cfg}
  398. binding.Encoder = &stringModeStringEncoder{binding.Encoder, cfg}
  399. } else {
  400. binding.Decoder = &stringModeNumberDecoder{binding.Decoder}
  401. binding.Encoder = &stringModeNumberEncoder{binding.Encoder}
  402. }
  403. }
  404. }
  405. binding.Decoder = &structFieldDecoder{binding.Field, binding.Decoder}
  406. binding.Encoder = &structFieldEncoder{binding.Field, binding.Encoder, shouldOmitEmpty}
  407. }
  408. }
  409. func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string {
  410. // ignore?
  411. if wholeTag == "-" {
  412. return []string{}
  413. }
  414. // rename?
  415. var fieldNames []string
  416. if tagProvidedFieldName == "" {
  417. fieldNames = []string{originalFieldName}
  418. } else {
  419. fieldNames = []string{tagProvidedFieldName}
  420. }
  421. // private?
  422. isNotExported := unicode.IsLower(rune(originalFieldName[0]))
  423. if isNotExported {
  424. fieldNames = []string{}
  425. }
  426. return fieldNames
  427. }