any.go 8.0 KB

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