feature_any.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strconv"
  6. )
  7. // Any API is for maximum flexibility
  8. type Any struct {
  9. val interface{}
  10. Error error
  11. LastAccessed interface{}
  12. }
  13. // MakeAny creates Any instance
  14. func MakeAny(val interface{}) *Any {
  15. return &Any{val, nil, nil}
  16. }
  17. // Get extracts a json object from Any
  18. func (any *Any) Get(keys ...interface{}) interface{} {
  19. ret, err := getPath(any.val, keys...)
  20. any.LastAccessed = ret
  21. if err != nil {
  22. any.Error = err
  23. return ""
  24. }
  25. return ret
  26. }
  27. // GetValueType gets type of a value
  28. func (any *Any) GetValueType(keys ...interface{}) ValueType {
  29. ret, err := getPath(any.val, keys...)
  30. any.LastAccessed = ret
  31. if err != nil {
  32. any.Error = err
  33. return Invalid
  34. }
  35. switch reflect.TypeOf(ret).Kind() {
  36. case reflect.Uint8:
  37. return Number
  38. case reflect.Int8:
  39. return Number
  40. case reflect.Uint16:
  41. return Number
  42. case reflect.Int16:
  43. return Number
  44. case reflect.Uint32:
  45. return Number
  46. case reflect.Int32:
  47. return Number
  48. case reflect.Uint64:
  49. return Number
  50. case reflect.Int64:
  51. return Number
  52. case reflect.Int:
  53. return Number
  54. case reflect.Uint:
  55. return Number
  56. case reflect.Float32:
  57. return Number
  58. case reflect.Float64:
  59. return Number
  60. case reflect.String:
  61. return String
  62. case reflect.Bool:
  63. return Bool
  64. case reflect.Array:
  65. return Array
  66. case reflect.Struct:
  67. return Object
  68. default:
  69. return Invalid
  70. }
  71. }
  72. // ToString converts a json object to string
  73. func (any *Any) ToString(keys ...interface{}) string {
  74. ret, err := getPath(any.val, keys...)
  75. any.LastAccessed = ret
  76. if err != nil {
  77. any.Error = err
  78. return ""
  79. }
  80. switch ret := ret.(type) {
  81. case uint8:
  82. return strconv.FormatInt(int64(ret), 10)
  83. case int8:
  84. return strconv.FormatInt(int64(ret), 10)
  85. case uint16:
  86. return strconv.FormatInt(int64(ret), 10)
  87. case int16:
  88. return strconv.FormatInt(int64(ret), 10)
  89. case uint32:
  90. return strconv.FormatInt(int64(ret), 10)
  91. case int32:
  92. return strconv.FormatInt(int64(ret), 10)
  93. case uint64:
  94. return strconv.FormatUint(uint64(ret), 10)
  95. case int64:
  96. return strconv.FormatInt(int64(ret), 10)
  97. case int:
  98. return strconv.FormatInt(int64(ret), 10)
  99. case uint:
  100. return strconv.FormatInt(int64(ret), 10)
  101. case float32:
  102. return strconv.FormatFloat(float64(ret), 'E', -1, 32)
  103. case float64:
  104. return strconv.FormatFloat(ret, 'E', -1, 64)
  105. case string:
  106. return ret
  107. default:
  108. return fmt.Sprintf("%v", ret)
  109. }
  110. }
  111. // ToUint8 converts a json object to Uint8
  112. func (any *Any) ToUint8(keys ...interface{}) uint8 {
  113. ret, err := getPathAsInt64(any, keys...)
  114. if err != nil {
  115. any.Error = err
  116. return 0
  117. }
  118. return uint8(ret)
  119. }
  120. // ToInt8 converts a json object to Int8
  121. func (any *Any) ToInt8(keys ...interface{}) int8 {
  122. ret, err := getPathAsInt64(any, keys...)
  123. if err != nil {
  124. any.Error = err
  125. return 0
  126. }
  127. return int8(ret)
  128. }
  129. // ToUint16 converts a json object to Uint16
  130. func (any *Any) ToUint16(keys ...interface{}) uint16 {
  131. ret, err := getPathAsInt64(any, keys...)
  132. if err != nil {
  133. any.Error = err
  134. return 0
  135. }
  136. return uint16(ret)
  137. }
  138. // ToInt16 converts a json object to Int16
  139. func (any *Any) ToInt16(keys ...interface{}) int16 {
  140. ret, err := getPathAsInt64(any, keys...)
  141. if err != nil {
  142. any.Error = err
  143. return 0
  144. }
  145. return int16(ret)
  146. }
  147. // ToUint32 converts a json object to Uint32
  148. func (any *Any) ToUint32(keys ...interface{}) uint32 {
  149. ret, err := getPathAsInt64(any, keys...)
  150. if err != nil {
  151. any.Error = err
  152. return 0
  153. }
  154. return uint32(ret)
  155. }
  156. // ToInt32 converts a json object to Int32
  157. func (any *Any) ToInt32(keys ...interface{}) int32 {
  158. ret, err := getPathAsInt64(any, keys...)
  159. if err != nil {
  160. any.Error = err
  161. return 0
  162. }
  163. return int32(ret)
  164. }
  165. // ToUint64 converts a json object to Uint64
  166. func (any *Any) ToUint64(keys ...interface{}) uint64 {
  167. ret, err := getPathAsUint64(any, keys...)
  168. if err != nil {
  169. any.Error = err
  170. return 0
  171. }
  172. return uint64(ret)
  173. }
  174. // ToInt64 converts a json object to Int64
  175. func (any *Any) ToInt64(keys ...interface{}) int64 {
  176. ret, err := getPathAsInt64(any, keys...)
  177. if err != nil {
  178. any.Error = err
  179. return 0
  180. }
  181. return int64(ret)
  182. }
  183. // ToInt converts a json object to Int
  184. func (any *Any) ToInt(keys ...interface{}) int {
  185. ret, err := getPathAsInt64(any, keys...)
  186. if err != nil {
  187. any.Error = err
  188. return 0
  189. }
  190. return int(ret)
  191. }
  192. // ToUint converts a json object to Uint
  193. func (any *Any) ToUint(keys ...interface{}) uint {
  194. ret, err := getPathAsInt64(any, keys...)
  195. if err != nil {
  196. any.Error = err
  197. return 0
  198. }
  199. return uint(ret)
  200. }
  201. // ToFloat32 converts a json object to Float32
  202. func (any *Any) ToFloat32(keys ...interface{}) float32 {
  203. ret, err := getPathAsFloat64(any, keys...)
  204. if err != nil {
  205. any.Error = err
  206. return 0
  207. }
  208. return float32(ret)
  209. }
  210. // ToFloat64 converts a json object to Float64
  211. func (any *Any) ToFloat64(keys ...interface{}) float64 {
  212. ret, err := getPathAsFloat64(any, keys...)
  213. if err != nil {
  214. any.Error = err
  215. return 0
  216. }
  217. return ret
  218. }
  219. // ToBool converts a json object to Bool
  220. func (any *Any) ToBool(keys ...interface{}) bool {
  221. ret, err := getPath(any.val, keys...)
  222. any.LastAccessed = ret
  223. if err != nil {
  224. any.Error = err
  225. return false
  226. }
  227. typedRet, ok := ret.(bool)
  228. if !ok {
  229. any.Error = fmt.Errorf("%v is not bool", ret)
  230. return false
  231. }
  232. return typedRet
  233. }
  234. // IsNil judges whether a json object is nil
  235. func (any *Any) IsNil(keys ...interface{}) bool {
  236. ret, err := getPath(any.val, keys...)
  237. any.LastAccessed = ret
  238. if err != nil {
  239. any.Error = err
  240. return false
  241. }
  242. return reflect.ValueOf(ret).IsNil()
  243. }
  244. func getPathAsInt64(any *Any, keys ...interface{}) (int64, error) {
  245. ret, err := getPath(any.val, keys...)
  246. any.LastAccessed = ret
  247. if err != nil {
  248. any.Error = err
  249. return 0, err
  250. }
  251. switch ret := ret.(type) {
  252. case uint8:
  253. return int64(ret), nil
  254. case int8:
  255. return int64(ret), nil
  256. case uint16:
  257. return int64(ret), nil
  258. case int16:
  259. return int64(ret), nil
  260. case uint32:
  261. return int64(ret), nil
  262. case int32:
  263. return int64(ret), nil
  264. case uint64:
  265. return int64(ret), nil
  266. case int64:
  267. return int64(ret), nil
  268. case int:
  269. return int64(ret), nil
  270. case uint:
  271. return int64(ret), nil
  272. case float32:
  273. return int64(ret), nil
  274. case float64:
  275. return int64(ret), nil
  276. case string:
  277. intVal, err := strconv.ParseInt(ret, 10, 64)
  278. if err != nil {
  279. return 0, err
  280. }
  281. return intVal, nil
  282. default:
  283. return 0, fmt.Errorf("%v is not number", ret)
  284. }
  285. }
  286. func getPathAsUint64(any *Any, keys ...interface{}) (uint64, error) {
  287. ret, err := getPath(any.val, keys...)
  288. any.LastAccessed = ret
  289. if err != nil {
  290. any.Error = err
  291. return 0, err
  292. }
  293. switch ret := ret.(type) {
  294. case uint8:
  295. return uint64(ret), nil
  296. case int8:
  297. return uint64(ret), nil
  298. case uint16:
  299. return uint64(ret), nil
  300. case int16:
  301. return uint64(ret), nil
  302. case uint32:
  303. return uint64(ret), nil
  304. case int32:
  305. return uint64(ret), nil
  306. case uint64:
  307. return uint64(ret), nil
  308. case int64:
  309. return uint64(ret), nil
  310. case int:
  311. return uint64(ret), nil
  312. case uint:
  313. return uint64(ret), nil
  314. case float32:
  315. return uint64(ret), nil
  316. case float64:
  317. return uint64(ret), nil
  318. case string:
  319. intVal, err := strconv.ParseUint(ret, 10, 64)
  320. if err != nil {
  321. return 0, err
  322. }
  323. return intVal, nil
  324. default:
  325. return 0, fmt.Errorf("%v is not number", ret)
  326. }
  327. }
  328. func getPathAsFloat64(any *Any, keys ...interface{}) (float64, error) {
  329. ret, err := getPath(any.val, keys...)
  330. any.LastAccessed = ret
  331. if err != nil {
  332. any.Error = err
  333. return 0, err
  334. }
  335. switch ret := ret.(type) {
  336. case uint8:
  337. return float64(ret), nil
  338. case int8:
  339. return float64(ret), nil
  340. case uint16:
  341. return float64(ret), nil
  342. case int16:
  343. return float64(ret), nil
  344. case uint32:
  345. return float64(ret), nil
  346. case int32:
  347. return float64(ret), nil
  348. case uint64:
  349. return float64(ret), nil
  350. case int64:
  351. return float64(ret), nil
  352. case int:
  353. return float64(ret), nil
  354. case uint:
  355. return float64(ret), nil
  356. case float32:
  357. return float64(ret), nil
  358. case float64:
  359. return float64(ret), nil
  360. case string:
  361. floatVal, err := strconv.ParseFloat(ret, 64)
  362. if err != nil {
  363. return 0, err
  364. }
  365. return floatVal, nil
  366. default:
  367. return 0, fmt.Errorf("%v is not number", ret)
  368. }
  369. }
  370. func getPath(val interface{}, keys ...interface{}) (interface{}, error) {
  371. if len(keys) == 0 {
  372. return val, nil
  373. }
  374. switch key := keys[0].(type) {
  375. case string:
  376. nextVal, err := getFromMap(val, key)
  377. if err != nil {
  378. return nil, err
  379. }
  380. nextKeys := make([]interface{}, len(keys)-1)
  381. copy(nextKeys, keys[1:])
  382. return getPath(nextVal, nextKeys...)
  383. case int:
  384. nextVal, err := getFromArray(val, key)
  385. if err != nil {
  386. return nil, err
  387. }
  388. nextKeys := make([]interface{}, len(keys)-1)
  389. copy(nextKeys, keys[1:])
  390. return getPath(nextVal, nextKeys...)
  391. default:
  392. return nil, fmt.Errorf("%v is not string or int", keys[0])
  393. }
  394. }
  395. func getFromMap(val interface{}, key string) (interface{}, error) {
  396. mapVal, ok := val.(map[string]interface{})
  397. if !ok {
  398. return nil, fmt.Errorf("%v is not map[string]interface{}", val)
  399. }
  400. ret, found := mapVal[key]
  401. if !found {
  402. return nil, fmt.Errorf("%v not found in %v", key, mapVal)
  403. }
  404. return ret, nil
  405. }
  406. func getFromArray(val interface{}, key int) (interface{}, error) {
  407. arrayVal, ok := val.([]interface{})
  408. if !ok {
  409. return nil, fmt.Errorf("%v is not []interface{}", val)
  410. }
  411. if key >= len(arrayVal) {
  412. return nil, fmt.Errorf("%v exceed %v", key, arrayVal)
  413. }
  414. if key < 0 {
  415. return nil, fmt.Errorf("%v exceed %v", key, arrayVal)
  416. }
  417. return arrayVal[key], nil
  418. }