http urls monitor.

reflect_native.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package jsoniter
  2. import (
  3. "encoding/base64"
  4. "reflect"
  5. "strconv"
  6. "unsafe"
  7. "github.com/modern-go/reflect2"
  8. )
  9. const ptrSize = 32 << uintptr(^uintptr(0)>>63)
  10. func createEncoderOfNative(ctx *ctx, typ reflect2.Type) ValEncoder {
  11. if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
  12. sliceDecoder := decoderOfSlice(ctx, typ)
  13. return &base64Codec{sliceDecoder: sliceDecoder}
  14. }
  15. typeName := typ.String()
  16. kind := typ.Kind()
  17. switch kind {
  18. case reflect.String:
  19. if typeName != "string" {
  20. return encoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
  21. }
  22. return &stringCodec{}
  23. case reflect.Int:
  24. if typeName != "int" {
  25. return encoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
  26. }
  27. if strconv.IntSize == 32 {
  28. return &int32Codec{}
  29. }
  30. return &int64Codec{}
  31. case reflect.Int8:
  32. if typeName != "int8" {
  33. return encoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
  34. }
  35. return &int8Codec{}
  36. case reflect.Int16:
  37. if typeName != "int16" {
  38. return encoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
  39. }
  40. return &int16Codec{}
  41. case reflect.Int32:
  42. if typeName != "int32" {
  43. return encoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
  44. }
  45. return &int32Codec{}
  46. case reflect.Int64:
  47. if typeName != "int64" {
  48. return encoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
  49. }
  50. return &int64Codec{}
  51. case reflect.Uint:
  52. if typeName != "uint" {
  53. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
  54. }
  55. if strconv.IntSize == 32 {
  56. return &uint32Codec{}
  57. }
  58. return &uint64Codec{}
  59. case reflect.Uint8:
  60. if typeName != "uint8" {
  61. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
  62. }
  63. return &uint8Codec{}
  64. case reflect.Uint16:
  65. if typeName != "uint16" {
  66. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
  67. }
  68. return &uint16Codec{}
  69. case reflect.Uint32:
  70. if typeName != "uint32" {
  71. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
  72. }
  73. return &uint32Codec{}
  74. case reflect.Uintptr:
  75. if typeName != "uintptr" {
  76. return encoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
  77. }
  78. if ptrSize == 32 {
  79. return &uint32Codec{}
  80. }
  81. return &uint64Codec{}
  82. case reflect.Uint64:
  83. if typeName != "uint64" {
  84. return encoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
  85. }
  86. return &uint64Codec{}
  87. case reflect.Float32:
  88. if typeName != "float32" {
  89. return encoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
  90. }
  91. return &float32Codec{}
  92. case reflect.Float64:
  93. if typeName != "float64" {
  94. return encoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
  95. }
  96. return &float64Codec{}
  97. case reflect.Bool:
  98. if typeName != "bool" {
  99. return encoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
  100. }
  101. return &boolCodec{}
  102. }
  103. return nil
  104. }
  105. func createDecoderOfNative(ctx *ctx, typ reflect2.Type) ValDecoder {
  106. if typ.Kind() == reflect.Slice && typ.(reflect2.SliceType).Elem().Kind() == reflect.Uint8 {
  107. sliceDecoder := decoderOfSlice(ctx, typ)
  108. return &base64Codec{sliceDecoder: sliceDecoder}
  109. }
  110. typeName := typ.String()
  111. switch typ.Kind() {
  112. case reflect.String:
  113. if typeName != "string" {
  114. return decoderOfType(ctx, reflect2.TypeOfPtr((*string)(nil)).Elem())
  115. }
  116. return &stringCodec{}
  117. case reflect.Int:
  118. if typeName != "int" {
  119. return decoderOfType(ctx, reflect2.TypeOfPtr((*int)(nil)).Elem())
  120. }
  121. if strconv.IntSize == 32 {
  122. return &int32Codec{}
  123. }
  124. return &int64Codec{}
  125. case reflect.Int8:
  126. if typeName != "int8" {
  127. return decoderOfType(ctx, reflect2.TypeOfPtr((*int8)(nil)).Elem())
  128. }
  129. return &int8Codec{}
  130. case reflect.Int16:
  131. if typeName != "int16" {
  132. return decoderOfType(ctx, reflect2.TypeOfPtr((*int16)(nil)).Elem())
  133. }
  134. return &int16Codec{}
  135. case reflect.Int32:
  136. if typeName != "int32" {
  137. return decoderOfType(ctx, reflect2.TypeOfPtr((*int32)(nil)).Elem())
  138. }
  139. return &int32Codec{}
  140. case reflect.Int64:
  141. if typeName != "int64" {
  142. return decoderOfType(ctx, reflect2.TypeOfPtr((*int64)(nil)).Elem())
  143. }
  144. return &int64Codec{}
  145. case reflect.Uint:
  146. if typeName != "uint" {
  147. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint)(nil)).Elem())
  148. }
  149. if strconv.IntSize == 32 {
  150. return &uint32Codec{}
  151. }
  152. return &uint64Codec{}
  153. case reflect.Uint8:
  154. if typeName != "uint8" {
  155. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint8)(nil)).Elem())
  156. }
  157. return &uint8Codec{}
  158. case reflect.Uint16:
  159. if typeName != "uint16" {
  160. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint16)(nil)).Elem())
  161. }
  162. return &uint16Codec{}
  163. case reflect.Uint32:
  164. if typeName != "uint32" {
  165. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint32)(nil)).Elem())
  166. }
  167. return &uint32Codec{}
  168. case reflect.Uintptr:
  169. if typeName != "uintptr" {
  170. return decoderOfType(ctx, reflect2.TypeOfPtr((*uintptr)(nil)).Elem())
  171. }
  172. if ptrSize == 32 {
  173. return &uint32Codec{}
  174. }
  175. return &uint64Codec{}
  176. case reflect.Uint64:
  177. if typeName != "uint64" {
  178. return decoderOfType(ctx, reflect2.TypeOfPtr((*uint64)(nil)).Elem())
  179. }
  180. return &uint64Codec{}
  181. case reflect.Float32:
  182. if typeName != "float32" {
  183. return decoderOfType(ctx, reflect2.TypeOfPtr((*float32)(nil)).Elem())
  184. }
  185. return &float32Codec{}
  186. case reflect.Float64:
  187. if typeName != "float64" {
  188. return decoderOfType(ctx, reflect2.TypeOfPtr((*float64)(nil)).Elem())
  189. }
  190. return &float64Codec{}
  191. case reflect.Bool:
  192. if typeName != "bool" {
  193. return decoderOfType(ctx, reflect2.TypeOfPtr((*bool)(nil)).Elem())
  194. }
  195. return &boolCodec{}
  196. }
  197. return nil
  198. }
  199. type stringCodec struct {
  200. }
  201. func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  202. *((*string)(ptr)) = iter.ReadString()
  203. }
  204. func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  205. str := *((*string)(ptr))
  206. stream.WriteString(str)
  207. }
  208. func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
  209. return *((*string)(ptr)) == ""
  210. }
  211. type int8Codec struct {
  212. }
  213. func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  214. if !iter.ReadNil() {
  215. *((*int8)(ptr)) = iter.ReadInt8()
  216. }
  217. }
  218. func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  219. stream.WriteInt8(*((*int8)(ptr)))
  220. }
  221. func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  222. return *((*int8)(ptr)) == 0
  223. }
  224. type int16Codec struct {
  225. }
  226. func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  227. if !iter.ReadNil() {
  228. *((*int16)(ptr)) = iter.ReadInt16()
  229. }
  230. }
  231. func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  232. stream.WriteInt16(*((*int16)(ptr)))
  233. }
  234. func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  235. return *((*int16)(ptr)) == 0
  236. }
  237. type int32Codec struct {
  238. }
  239. func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  240. if !iter.ReadNil() {
  241. *((*int32)(ptr)) = iter.ReadInt32()
  242. }
  243. }
  244. func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  245. stream.WriteInt32(*((*int32)(ptr)))
  246. }
  247. func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  248. return *((*int32)(ptr)) == 0
  249. }
  250. type int64Codec struct {
  251. }
  252. func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  253. if !iter.ReadNil() {
  254. *((*int64)(ptr)) = iter.ReadInt64()
  255. }
  256. }
  257. func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  258. stream.WriteInt64(*((*int64)(ptr)))
  259. }
  260. func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  261. return *((*int64)(ptr)) == 0
  262. }
  263. type uint8Codec struct {
  264. }
  265. func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  266. if !iter.ReadNil() {
  267. *((*uint8)(ptr)) = iter.ReadUint8()
  268. }
  269. }
  270. func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  271. stream.WriteUint8(*((*uint8)(ptr)))
  272. }
  273. func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  274. return *((*uint8)(ptr)) == 0
  275. }
  276. type uint16Codec struct {
  277. }
  278. func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  279. if !iter.ReadNil() {
  280. *((*uint16)(ptr)) = iter.ReadUint16()
  281. }
  282. }
  283. func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  284. stream.WriteUint16(*((*uint16)(ptr)))
  285. }
  286. func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  287. return *((*uint16)(ptr)) == 0
  288. }
  289. type uint32Codec struct {
  290. }
  291. func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  292. if !iter.ReadNil() {
  293. *((*uint32)(ptr)) = iter.ReadUint32()
  294. }
  295. }
  296. func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  297. stream.WriteUint32(*((*uint32)(ptr)))
  298. }
  299. func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  300. return *((*uint32)(ptr)) == 0
  301. }
  302. type uint64Codec struct {
  303. }
  304. func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  305. if !iter.ReadNil() {
  306. *((*uint64)(ptr)) = iter.ReadUint64()
  307. }
  308. }
  309. func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  310. stream.WriteUint64(*((*uint64)(ptr)))
  311. }
  312. func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  313. return *((*uint64)(ptr)) == 0
  314. }
  315. type float32Codec struct {
  316. }
  317. func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  318. if !iter.ReadNil() {
  319. *((*float32)(ptr)) = iter.ReadFloat32()
  320. }
  321. }
  322. func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  323. stream.WriteFloat32(*((*float32)(ptr)))
  324. }
  325. func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  326. return *((*float32)(ptr)) == 0
  327. }
  328. type float64Codec struct {
  329. }
  330. func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  331. if !iter.ReadNil() {
  332. *((*float64)(ptr)) = iter.ReadFloat64()
  333. }
  334. }
  335. func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  336. stream.WriteFloat64(*((*float64)(ptr)))
  337. }
  338. func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  339. return *((*float64)(ptr)) == 0
  340. }
  341. type boolCodec struct {
  342. }
  343. func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  344. if !iter.ReadNil() {
  345. *((*bool)(ptr)) = iter.ReadBool()
  346. }
  347. }
  348. func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  349. stream.WriteBool(*((*bool)(ptr)))
  350. }
  351. func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
  352. return !(*((*bool)(ptr)))
  353. }
  354. type base64Codec struct {
  355. sliceType *reflect2.UnsafeSliceType
  356. sliceDecoder ValDecoder
  357. }
  358. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  359. if iter.ReadNil() {
  360. codec.sliceType.UnsafeSetNil(ptr)
  361. return
  362. }
  363. switch iter.WhatIsNext() {
  364. case StringValue:
  365. src := iter.ReadString()
  366. dst, err := base64.StdEncoding.DecodeString(src)
  367. if err != nil {
  368. iter.ReportError("decode base64", err.Error())
  369. } else {
  370. codec.sliceType.UnsafeSet(ptr, unsafe.Pointer(&dst))
  371. }
  372. case ArrayValue:
  373. codec.sliceDecoder.Decode(ptr, iter)
  374. default:
  375. iter.ReportError("base64Codec", "invalid input")
  376. }
  377. }
  378. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  379. src := *((*[]byte)(ptr))
  380. if len(src) == 0 {
  381. stream.WriteNil()
  382. return
  383. }
  384. encoding := base64.StdEncoding
  385. stream.writeByte('"')
  386. size := encoding.EncodedLen(len(src))
  387. buf := make([]byte, size)
  388. encoding.Encode(buf, src)
  389. stream.buf = append(stream.buf, buf...)
  390. stream.writeByte('"')
  391. }
  392. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  393. return len(*((*[]byte)(ptr))) == 0
  394. }