| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- package jsoniter
- import (
- "fmt"
- "reflect"
- "strconv"
- )
- type Any struct {
- val interface{}
- Error error
- }
- func any(val interface{}) Any {
- return Any{val, nil}
- }
- func (any *Any) Get(keys ...interface{}) interface{} {
- ret, err := getPath(any.val, keys...)
- if err != nil {
- any.Error = err
- return "";
- }
- return ret
- }
- func (any *Any) GetValueType(keys ...interface{}) ValueType {
- ret, err := getPath(any.val, keys...)
- if err != nil {
- any.Error = err
- return Invalid;
- }
- switch reflect.TypeOf(ret).Kind() {
- case reflect.Uint8:
- return Number;
- case reflect.Int8:
- return Number;
- case reflect.Uint16:
- return Number;
- case reflect.Int16:
- return Number;
- case reflect.Uint32:
- return Number;
- case reflect.Int32:
- return Number;
- case reflect.Uint64:
- return Number;
- case reflect.Int64:
- return Number;
- case reflect.Int:
- return Number;
- case reflect.Uint:
- return Number;
- case reflect.Float32:
- return Number;
- case reflect.Float64:
- return Number;
- case reflect.String:
- return String;
- case reflect.Bool:
- return Bool;
- case reflect.Array:
- return Array;
- case reflect.Struct:
- return Object;
- default:
- return Invalid
- }
- }
- func (any *Any) ToString(keys ...interface{}) string {
- ret, err := getPath(any.val, keys...)
- if err != nil {
- any.Error = err
- return "";
- }
- switch ret := ret.(type) {
- case uint8:
- return strconv.FormatInt(int64(ret), 10);
- case int8:
- return strconv.FormatInt(int64(ret), 10);
- case uint16:
- return strconv.FormatInt(int64(ret), 10);
- case int16:
- return strconv.FormatInt(int64(ret), 10);
- case uint32:
- return strconv.FormatInt(int64(ret), 10);
- case int32:
- return strconv.FormatInt(int64(ret), 10);
- case uint64:
- return strconv.FormatUint(uint64(ret), 10);
- case int64:
- return strconv.FormatInt(int64(ret), 10);
- case int:
- return strconv.FormatInt(int64(ret), 10);
- case uint:
- return strconv.FormatInt(int64(ret), 10);
- case float32:
- return strconv.FormatFloat(float64(ret), 'E', -1, 32);
- case float64:
- return strconv.FormatFloat(ret, 'E', -1, 64);
- case string:
- return ret
- default:
- return fmt.Sprintf("%v", ret)
- }
- }
- func (any *Any) ToUint8(keys ...interface{}) uint8 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return uint8(ret)
- }
- func (any *Any) ToInt8(keys ...interface{}) int8 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return int8(ret)
- }
- func (any *Any) ToUint16(keys ...interface{}) uint16 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return uint16(ret)
- }
- func (any *Any) ToInt16(keys ...interface{}) int16 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return int16(ret)
- }
- func (any *Any) ToUint32(keys ...interface{}) uint32 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return uint32(ret)
- }
- func (any *Any) ToInt32(keys ...interface{}) int32 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return int32(ret)
- }
- func (any *Any) ToUint64(keys ...interface{}) uint64 {
- ret, err := getPathAsUint64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return uint64(ret)
- }
- func (any *Any) ToInt64(keys ...interface{}) int64 {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return int64(ret)
- }
- func (any *Any) ToInt(keys ...interface{}) int {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return int(ret)
- }
- func (any *Any) ToUint(keys ...interface{}) uint {
- ret, err := getPathAsInt64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return uint(ret)
- }
- func (any *Any) ToFloat32(keys ...interface{}) float32 {
- ret, err := getPathAsFloat64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return float32(ret)
- }
- func (any *Any) ToFloat64(keys ...interface{}) float64 {
- ret, err := getPathAsFloat64(any.val, keys...)
- if err != nil {
- any.Error = err
- return 0;
- }
- return ret
- }
- func (any *Any) ToBool(keys ...interface{}) bool {
- ret, err := getPath(any.val, keys...)
- if err != nil {
- any.Error = err
- return false;
- }
- typedRet, ok := ret.(bool)
- if !ok {
- any.Error = fmt.Errorf("%v is not bool", ret)
- return false;
- }
- return typedRet
- }
- func (any *Any) IsNull(keys ...interface{}) bool {
- ret, err := getPath(any.val, keys...)
- if err != nil {
- any.Error = err
- return false;
- }
- return reflect.ValueOf(ret).IsNil()
- }
- func getPathAsInt64(val interface{}, keys ...interface{}) (int64, error) {
- ret, err := getPath(val, keys...)
- if err != nil {
- return 0, err
- }
- switch ret := ret.(type) {
- case uint8:
- return int64(ret), nil;
- case int8:
- return int64(ret), nil;
- case uint16:
- return int64(ret), nil;
- case int16:
- return int64(ret), nil;
- case uint32:
- return int64(ret), nil;
- case int32:
- return int64(ret), nil;
- case uint64:
- return int64(ret), nil;
- case int64:
- return int64(ret), nil;
- case int:
- return int64(ret), nil;
- case uint:
- return int64(ret), nil;
- case float32:
- return int64(ret), nil;
- case float64:
- return int64(ret), nil;
- default:
- return 0, fmt.Errorf("%v is not number", ret)
- }
- }
- func getPathAsUint64(val interface{}, keys ...interface{}) (uint64, error) {
- ret, err := getPath(val, keys...)
- if err != nil {
- return 0, err
- }
- switch ret := ret.(type) {
- case uint8:
- return uint64(ret), nil;
- case int8:
- return uint64(ret), nil;
- case uint16:
- return uint64(ret), nil;
- case int16:
- return uint64(ret), nil;
- case uint32:
- return uint64(ret), nil;
- case int32:
- return uint64(ret), nil;
- case uint64:
- return uint64(ret), nil;
- case int64:
- return uint64(ret), nil;
- case int:
- return uint64(ret), nil;
- case uint:
- return uint64(ret), nil;
- case float32:
- return uint64(ret), nil;
- case float64:
- return uint64(ret), nil;
- default:
- return 0, fmt.Errorf("%v is not number", ret)
- }
- }
- func getPathAsFloat64(val interface{}, keys ...interface{}) (float64, error) {
- ret, err := getPath(val, keys...)
- if err != nil {
- return 0, err
- }
- switch ret := ret.(type) {
- case uint8:
- return float64(ret), nil;
- case int8:
- return float64(ret), nil;
- case uint16:
- return float64(ret), nil;
- case int16:
- return float64(ret), nil;
- case uint32:
- return float64(ret), nil;
- case int32:
- return float64(ret), nil;
- case uint64:
- return float64(ret), nil;
- case int64:
- return float64(ret), nil;
- case int:
- return float64(ret), nil;
- case uint:
- return float64(ret), nil;
- case float32:
- return float64(ret), nil;
- case float64:
- return float64(ret), nil;
- default:
- return 0, fmt.Errorf("%v is not number", ret)
- }
- }
- func getPath(val interface{}, keys ...interface{}) (interface{}, error) {
- if (len(keys) == 0) {
- return val, nil;
- }
- switch key := keys[0].(type) {
- case string:
- nextVal, err := getFromMap(val, key)
- if err != nil {
- return nil, err
- }
- nextKeys := make([]interface{}, len(keys) - 1)
- copy(nextKeys, keys[1:])
- return getPath(nextVal, nextKeys...)
- case int:
- nextVal, err := getFromArray(val, key)
- if err != nil {
- return nil, err
- }
- nextKeys := make([]interface{}, len(keys) - 1)
- copy(nextKeys, keys[1:])
- return getPath(nextVal, nextKeys...)
- default:
- return nil, fmt.Errorf("%v is not string or int", keys[0]);
- }
- return getPath(val, keys);
- }
- func getFromMap(val interface{}, key string) (interface{}, error) {
- mapVal, ok := val.(map[string]interface{})
- if !ok {
- return nil, fmt.Errorf("%v is not map[string]interface{}", val)
- }
- ret, found := mapVal[key]
- if !found {
- return nil, fmt.Errorf("%v not found in %v", key, mapVal)
- }
- return ret, nil
- }
- func getFromArray(val interface{}, key int) (interface{}, error) {
- arrayVal, ok := val.([]interface{})
- if !ok {
- return nil, fmt.Errorf("%v is not []interface{}", val)
- }
- if key >= len(arrayVal) {
- return nil, fmt.Errorf("%v exceed %v", key, arrayVal)
- }
- if key < 0 {
- return nil, fmt.Errorf("%v exceed %v", key, arrayVal)
- }
- return arrayVal[key], nil
- }
|