You've already forked json-iterator
mirror of
https://github.com/json-iterator/go.git
synced 2025-12-05 22:56:06 +02:00
make any easier to work with
This commit is contained in:
148
any.go
148
any.go
@@ -3,15 +3,20 @@ package jsoniter
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Any struct {
|
||||
Val interface{}
|
||||
val interface{}
|
||||
Error error
|
||||
}
|
||||
|
||||
func (any *Any) GetObject(keys ...interface{}) interface{} {
|
||||
ret, err := getPath(any.Val, keys...)
|
||||
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 "";
|
||||
@@ -19,22 +24,91 @@ func (any *Any) GetObject(keys ...interface{}) interface{} {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (any *Any) GetString(keys ...interface{}) string {
|
||||
ret, err := getPath(any.Val, keys...)
|
||||
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 "";
|
||||
}
|
||||
typedRet, ok := ret.(string)
|
||||
if !ok {
|
||||
any.Error = fmt.Errorf("%v is not string", ret);
|
||||
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)
|
||||
}
|
||||
return typedRet
|
||||
}
|
||||
|
||||
func (any *Any) GetUint8(keys ...interface{}) uint8 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToUint8(keys ...interface{}) uint8 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -42,8 +116,8 @@ func (any *Any) GetUint8(keys ...interface{}) uint8 {
|
||||
return uint8(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetInt8(keys ...interface{}) int8 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToInt8(keys ...interface{}) int8 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -51,8 +125,8 @@ func (any *Any) GetInt8(keys ...interface{}) int8 {
|
||||
return int8(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetUint16(keys ...interface{}) uint16 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToUint16(keys ...interface{}) uint16 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -60,8 +134,8 @@ func (any *Any) GetUint16(keys ...interface{}) uint16 {
|
||||
return uint16(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetInt16(keys ...interface{}) int16 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToInt16(keys ...interface{}) int16 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -69,8 +143,8 @@ func (any *Any) GetInt16(keys ...interface{}) int16 {
|
||||
return int16(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetUint32(keys ...interface{}) uint32 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToUint32(keys ...interface{}) uint32 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -78,8 +152,8 @@ func (any *Any) GetUint32(keys ...interface{}) uint32 {
|
||||
return uint32(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetInt32(keys ...interface{}) int32 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToInt32(keys ...interface{}) int32 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -87,8 +161,8 @@ func (any *Any) GetInt32(keys ...interface{}) int32 {
|
||||
return int32(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetUint64(keys ...interface{}) uint64 {
|
||||
ret, err := getPathAsUint64(any.Val, keys...)
|
||||
func (any *Any) ToUint64(keys ...interface{}) uint64 {
|
||||
ret, err := getPathAsUint64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -96,8 +170,8 @@ func (any *Any) GetUint64(keys ...interface{}) uint64 {
|
||||
return uint64(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetInt64(keys ...interface{}) int64 {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToInt64(keys ...interface{}) int64 {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -105,8 +179,8 @@ func (any *Any) GetInt64(keys ...interface{}) int64 {
|
||||
return int64(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetInt(keys ...interface{}) int {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToInt(keys ...interface{}) int {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -114,8 +188,8 @@ func (any *Any) GetInt(keys ...interface{}) int {
|
||||
return int(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetUint(keys ...interface{}) uint {
|
||||
ret, err := getPathAsInt64(any.Val, keys...)
|
||||
func (any *Any) ToUint(keys ...interface{}) uint {
|
||||
ret, err := getPathAsInt64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -123,8 +197,8 @@ func (any *Any) GetUint(keys ...interface{}) uint {
|
||||
return uint(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetFloat32(keys ...interface{}) float32 {
|
||||
ret, err := getPathAsFloat64(any.Val, keys...)
|
||||
func (any *Any) ToFloat32(keys ...interface{}) float32 {
|
||||
ret, err := getPathAsFloat64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -132,8 +206,8 @@ func (any *Any) GetFloat32(keys ...interface{}) float32 {
|
||||
return float32(ret)
|
||||
}
|
||||
|
||||
func (any *Any) GetFloat64(keys ...interface{}) float64 {
|
||||
ret, err := getPathAsFloat64(any.Val, keys...)
|
||||
func (any *Any) ToFloat64(keys ...interface{}) float64 {
|
||||
ret, err := getPathAsFloat64(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return 0;
|
||||
@@ -141,8 +215,8 @@ func (any *Any) GetFloat64(keys ...interface{}) float64 {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (any *Any) GetBool(keys ...interface{}) bool {
|
||||
ret, err := getPath(any.Val, keys...)
|
||||
func (any *Any) ToBool(keys ...interface{}) bool {
|
||||
ret, err := getPath(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return false;
|
||||
@@ -156,7 +230,7 @@ func (any *Any) GetBool(keys ...interface{}) bool {
|
||||
}
|
||||
|
||||
func (any *Any) IsNull(keys ...interface{}) bool {
|
||||
ret, err := getPath(any.Val, keys...)
|
||||
ret, err := getPath(any.val, keys...)
|
||||
if err != nil {
|
||||
any.Error = err
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user