reflect_extension.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. for _, extension := range ctx.extensions {
  203. decoder = extension.DecorateDecoder(typ, decoder)
  204. }
  205. }
  206. return decoder
  207. }
  208. func _getTypeDecoderFromExtension(ctx *ctx, typ reflect2.Type) ValDecoder {
  209. for _, extension := range extensions {
  210. decoder := extension.CreateDecoder(typ)
  211. if decoder != nil {
  212. return decoder
  213. }
  214. }
  215. for _, extension := range ctx.extensions {
  216. decoder := extension.CreateDecoder(typ)
  217. if decoder != nil {
  218. return decoder
  219. }
  220. }
  221. typeName := typ.String()
  222. decoder := typeDecoders[typeName]
  223. if decoder != nil {
  224. return decoder
  225. }
  226. if typ.Kind() == reflect.Ptr {
  227. ptrType := typ.(*reflect2.UnsafePtrType)
  228. decoder := typeDecoders[ptrType.Elem().String()]
  229. if decoder != nil {
  230. return &OptionalDecoder{ptrType.Elem(), decoder}
  231. }
  232. }
  233. return nil
  234. }
  235. func getTypeEncoderFromExtension(ctx *ctx, typ reflect2.Type) ValEncoder {
  236. encoder := _getTypeEncoderFromExtension(ctx, typ)
  237. if encoder != nil {
  238. for _, extension := range extensions {
  239. encoder = extension.DecorateEncoder(typ, encoder)
  240. }
  241. for _, extension := range ctx.extensions {
  242. encoder = extension.DecorateEncoder(typ, encoder)
  243. }
  244. }
  245. return encoder
  246. }
  247. func _getTypeEncoderFromExtension(ctx *ctx, typ reflect2.Type) ValEncoder {
  248. for _, extension := range extensions {
  249. encoder := extension.CreateEncoder(typ)
  250. if encoder != nil {
  251. return encoder
  252. }
  253. }
  254. for _, extension := range ctx.extensions {
  255. encoder := extension.CreateEncoder(typ)
  256. if encoder != nil {
  257. return encoder
  258. }
  259. }
  260. typeName := typ.String()
  261. encoder := typeEncoders[typeName]
  262. if encoder != nil {
  263. return encoder
  264. }
  265. if typ.Kind() == reflect.Ptr {
  266. typePtr := typ.(*reflect2.UnsafePtrType)
  267. encoder := typeEncoders[typePtr.Elem().String()]
  268. if encoder != nil {
  269. return &OptionalEncoder{encoder}
  270. }
  271. }
  272. return nil
  273. }
  274. func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
  275. structType := typ.(*reflect2.UnsafeStructType)
  276. embeddedBindings := []*Binding{}
  277. bindings := []*Binding{}
  278. for i := 0; i < structType.NumField(); i++ {
  279. field := structType.Field(i)
  280. tag, hastag := field.Tag().Lookup(ctx.getTagKey())
  281. if ctx.onlyTaggedField && !hastag {
  282. continue
  283. }
  284. tagParts := strings.Split(tag, ",")
  285. if tag == "-" {
  286. continue
  287. }
  288. if field.Anonymous() && (tag == "" || tagParts[0] == "") {
  289. if field.Type().Kind() == reflect.Struct {
  290. structDescriptor := describeStruct(ctx, field.Type())
  291. for _, binding := range structDescriptor.Fields {
  292. binding.levels = append([]int{i}, binding.levels...)
  293. omitempty := binding.Encoder.(*structFieldEncoder).omitempty
  294. binding.Encoder = &structFieldEncoder{field, binding.Encoder, omitempty}
  295. binding.Decoder = &structFieldDecoder{field, binding.Decoder}
  296. embeddedBindings = append(embeddedBindings, binding)
  297. }
  298. continue
  299. } else if field.Type().Kind() == reflect.Ptr {
  300. ptrType := field.Type().(*reflect2.UnsafePtrType)
  301. if ptrType.Elem().Kind() == reflect.Struct {
  302. structDescriptor := describeStruct(ctx, ptrType.Elem())
  303. for _, binding := range structDescriptor.Fields {
  304. binding.levels = append([]int{i}, binding.levels...)
  305. omitempty := binding.Encoder.(*structFieldEncoder).omitempty
  306. binding.Encoder = &dereferenceEncoder{binding.Encoder}
  307. binding.Encoder = &structFieldEncoder{field, binding.Encoder, omitempty}
  308. binding.Decoder = &dereferenceDecoder{ptrType.Elem(), binding.Decoder}
  309. binding.Decoder = &structFieldDecoder{field, binding.Decoder}
  310. embeddedBindings = append(embeddedBindings, binding)
  311. }
  312. continue
  313. }
  314. }
  315. }
  316. fieldNames := calcFieldNames(field.Name(), tagParts[0], tag)
  317. fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name())
  318. decoder := fieldDecoders[fieldCacheKey]
  319. if decoder == nil {
  320. decoder = decoderOfType(ctx.append(field.Name()), field.Type())
  321. }
  322. encoder := fieldEncoders[fieldCacheKey]
  323. if encoder == nil {
  324. encoder = encoderOfType(ctx.append(field.Name()), field.Type())
  325. }
  326. binding := &Binding{
  327. Field: field,
  328. FromNames: fieldNames,
  329. ToNames: fieldNames,
  330. Decoder: decoder,
  331. Encoder: encoder,
  332. }
  333. binding.levels = []int{i}
  334. bindings = append(bindings, binding)
  335. }
  336. return createStructDescriptor(ctx, typ, bindings, embeddedBindings)
  337. }
  338. func createStructDescriptor(ctx *ctx, typ reflect2.Type, bindings []*Binding, embeddedBindings []*Binding) *StructDescriptor {
  339. structDescriptor := &StructDescriptor{
  340. Type: typ,
  341. Fields: bindings,
  342. }
  343. for _, extension := range extensions {
  344. extension.UpdateStructDescriptor(structDescriptor)
  345. }
  346. for _, extension := range ctx.extensions {
  347. extension.UpdateStructDescriptor(structDescriptor)
  348. }
  349. processTags(structDescriptor, ctx.frozenConfig)
  350. // merge normal & embedded bindings & sort with original order
  351. allBindings := sortableBindings(append(embeddedBindings, structDescriptor.Fields...))
  352. sort.Sort(allBindings)
  353. structDescriptor.Fields = allBindings
  354. return structDescriptor
  355. }
  356. type sortableBindings []*Binding
  357. func (bindings sortableBindings) Len() int {
  358. return len(bindings)
  359. }
  360. func (bindings sortableBindings) Less(i, j int) bool {
  361. left := bindings[i].levels
  362. right := bindings[j].levels
  363. k := 0
  364. for {
  365. if left[k] < right[k] {
  366. return true
  367. } else if left[k] > right[k] {
  368. return false
  369. }
  370. k++
  371. }
  372. }
  373. func (bindings sortableBindings) Swap(i, j int) {
  374. bindings[i], bindings[j] = bindings[j], bindings[i]
  375. }
  376. func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
  377. for _, binding := range structDescriptor.Fields {
  378. shouldOmitEmpty := false
  379. tagParts := strings.Split(binding.Field.Tag().Get(cfg.getTagKey()), ",")
  380. for _, tagPart := range tagParts[1:] {
  381. if tagPart == "omitempty" {
  382. shouldOmitEmpty = true
  383. } else if tagPart == "string" {
  384. if binding.Field.Type().Kind() == reflect.String {
  385. binding.Decoder = &stringModeStringDecoder{binding.Decoder, cfg}
  386. binding.Encoder = &stringModeStringEncoder{binding.Encoder, cfg}
  387. } else {
  388. binding.Decoder = &stringModeNumberDecoder{binding.Decoder}
  389. binding.Encoder = &stringModeNumberEncoder{binding.Encoder}
  390. }
  391. }
  392. }
  393. binding.Decoder = &structFieldDecoder{binding.Field, binding.Decoder}
  394. binding.Encoder = &structFieldEncoder{binding.Field, binding.Encoder, shouldOmitEmpty}
  395. }
  396. }
  397. func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string {
  398. // ignore?
  399. if wholeTag == "-" {
  400. return []string{}
  401. }
  402. // rename?
  403. var fieldNames []string
  404. if tagProvidedFieldName == "" {
  405. fieldNames = []string{originalFieldName}
  406. } else {
  407. fieldNames = []string{tagProvidedFieldName}
  408. }
  409. // private?
  410. isNotExported := unicode.IsLower(rune(originalFieldName[0]))
  411. if isNotExported {
  412. fieldNames = []string{}
  413. }
  414. return fieldNames
  415. }