energy/pkgs/json/json.go

646 lines
13 KiB
Go
Raw Normal View History

2023-03-10 14:42:15 +08:00
//----------------------------------------
//
// Copyright © yanghy. All Rights Reserved.
//
// Licensed under Apache License Version 2.0, January 2004
//
// https://www.apache.org/licenses/LICENSE-2.0
//
//----------------------------------------
2023-05-31 17:41:14 +08:00
// Go implements JSON serialization and JSON deserialization based on map and slice
// key string, value any
// JSON
2023-03-10 14:42:15 +08:00
package json
import (
2023-03-10 15:46:07 +08:00
"encoding/json"
2023-03-10 14:42:15 +08:00
"github.com/energye/energy/common"
. "github.com/energye/energy/consts"
2023-03-24 09:24:03 +08:00
jsoniter "github.com/json-iterator/go" //json-iterator or encoding/json
2023-03-10 14:42:15 +08:00
"reflect"
"strconv"
)
2023-05-31 17:41:14 +08:00
// BaseJSON
// JSON base object
2023-03-10 15:10:40 +08:00
type BaseJSON interface {
2023-03-12 22:52:37 +08:00
Size() int //返回数据数量
2023-05-31 17:41:14 +08:00
Type() reflect.Kind //当前对象数量类型
2023-03-12 22:52:37 +08:00
Data() any //返回原始数据
2023-03-25 22:35:06 +08:00
JsonData() *JsonData //返回原始JsonData数据结构
2023-03-24 22:23:23 +08:00
SetValue(value any) //设置值
2023-03-12 22:52:37 +08:00
String() string //返回 string 类型值
2023-03-14 23:07:51 +08:00
Int() int //返回 int 类型值, 把所有数字类型都转换 int 返回
2023-03-24 22:23:23 +08:00
Int64() int64 //返回 uint 类型值, 把所有数字类型都转换 int64 返回
2023-03-14 23:07:51 +08:00
UInt() uint //返回 uint 类型值, 把所有数字类型都转换 uint 返回
2023-03-24 22:23:23 +08:00
UInt64() uint64 //返回 uint 类型值, 把所有数字类型都转换 uint64 返回
2023-03-14 23:07:51 +08:00
Bytes() []byte //转换为 '[]byte' 并返回, 任意类型都将转换
Float() float64 //返回 float64 类型值 把所有数字类型都转换 float64 返回
2023-03-12 22:52:37 +08:00
Bool() bool //返回 bool 类型值
JSONObject() JSONObject //返回 JSONObject 对象类型
JSONArray() JSONArray //返回 JSONArray 对象类型
2023-05-31 17:41:14 +08:00
JSON() JSON //返回 JSON 对象类型
2023-03-12 22:52:37 +08:00
ToJSONString() string //转换为JSON字符串并返回
IsString() bool //当前对象是否为 string
IsInt() bool //当前对象是否为 int
IsUInt() bool //当前对象是否为 uint
IsBytes() bool //当前对象是否为 []byte
IsFloat() bool //当前对象是否为 float64
IsBool() bool //当前对象是否为 bool
IsObject() bool //当前对象是否为 JSONObject
IsArray() bool //当前对象是否为 JSONArray
Clear() //清空所有数据,保留原始数据类型
Free() //释放数据空间,且类型失效,当前对象不可用
2023-03-10 14:42:15 +08:00
}
2023-03-10 15:10:40 +08:00
// JSON Object
type JSON interface {
JSONArray
JSONObject
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
// JsonData
// Data structure
2023-03-24 22:23:23 +08:00
type JsonData struct {
2023-05-31 17:41:14 +08:00
t reflect.Kind // type
s int // size
v any // value
pKey string // object parent key
pIndex int // array parent index
p *JsonData // parent
d bool // is recursion
}
// NewJsonData
// create Json
// t: data type
// s: data size
// v: data value
func NewJsonData(t reflect.Kind, s int, v any) *JsonData {
return &JsonData{t: t, s: s, v: v}
}
// NewJSON
// return JSON Object, JSONArray or JSONObject
// data:
// []byte("{...}") object
// []byte("[...]") array
2023-03-10 15:10:40 +08:00
func NewJSON(data []byte) JSON {
2023-03-10 14:42:15 +08:00
if data == nil {
return nil
}
var v any
if err := jsoniter.Unmarshal(data, &v); err == nil {
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Slice:
if v, ok := v.([]any); ok {
2023-05-31 17:41:14 +08:00
return &JsonData{t: reflect.Slice, v: v, s: len(v)}
2023-03-10 14:42:15 +08:00
}
case reflect.Map:
if v, ok := v.(map[string]any); ok {
2023-05-31 17:41:14 +08:00
return &JsonData{t: reflect.Map, v: v, s: len(v)}
2023-03-10 14:42:15 +08:00
}
}
}
return nil
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) Size() int {
2023-05-31 17:41:14 +08:00
return m.s
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) Type() reflect.Kind {
return m.t
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) Data() any {
2023-05-31 17:41:14 +08:00
return m.v
2023-03-10 14:42:15 +08:00
}
2023-03-25 22:35:06 +08:00
func (m *JsonData) JsonData() *JsonData {
return m
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) JSON() JSON {
return m
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) String() string {
if m.IsString() {
return m.v.(string)
2023-03-10 14:42:15 +08:00
}
return ""
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) Int() (r int) {
r, _ = toInt(m.v)
return
2023-03-23 17:33:33 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) Int64() (r int64) {
r, _ = toInt64(m.v)
return
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) UInt() (r uint) {
r, _ = toUInt(m.v)
return
2023-03-23 17:33:33 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) UInt64() (r uint64) {
r, _ = toUInt64(m.v)
return
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) Bytes() []byte {
// TODO 需要改进, 直接返回 [] byte
return toBytes(m.ConvertToData())
//return m.toBytes(m.v)
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) Float() (r float64) {
r, _ = toFloat64(m.v)
return
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) Bool() (r bool) {
r, _ = toBool(m.v)
return
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) JSONObject() JSONObject {
if m.IsObject() {
return m
2023-03-10 14:42:15 +08:00
}
return nil
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) JSONArray() JSONArray {
if m.IsArray() {
return m
2023-03-10 14:42:15 +08:00
}
return nil
}
2023-03-24 23:08:44 +08:00
func (m *JsonData) modifyParentValue() {
2023-03-25 22:35:06 +08:00
if m.p != nil {
if m.p.IsArray() {
2023-05-31 17:41:14 +08:00
m.p.SetByIndex(m.pIndex, m.v)
2023-03-25 22:35:06 +08:00
} else if m.p.IsObject() {
2023-05-31 17:41:14 +08:00
m.p.Set(m.pKey, m.v)
2023-03-24 23:08:44 +08:00
}
}
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) SetValue(value any) {
switch value.(type) {
2023-05-31 17:41:14 +08:00
case JsonData:
v := value.(JsonData)
m.t = v.t
m.v = v.v
m.s = v.s
m.p = v.p
m.pIndex = v.pIndex
m.pKey = v.pKey
m.modifyParentValue()
case *JsonData:
v := value.(*JsonData)
m.t = v.t
m.v = v.v
m.s = v.s
m.p = v.p
m.pIndex = v.pIndex
m.pKey = v.pKey
m.modifyParentValue()
case JSON:
v := value.(JSON).JsonData()
m.t = v.t
m.v = v.v
m.s = v.s
m.p = v.p
m.pIndex = v.pIndex
m.pKey = v.pKey
m.modifyParentValue()
case JSONObject:
v := value.(JSONObject).JsonData()
m.t = v.t
m.v = v.v
m.s = v.s
m.p = v.p
m.pIndex = v.pIndex
m.pKey = v.pKey
m.modifyParentValue()
case JSONArray:
v := value.(JSONArray).JsonData()
m.t = v.t
m.v = v.v
m.s = v.s
m.p = v.p
m.pIndex = v.pIndex
m.pKey = v.pKey
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case json.Number:
if v, err := value.(json.Number).Int64(); err == nil {
2023-05-31 17:41:14 +08:00
m.t = reflect.Int
m.v = v
m.s = 8
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
}
case string:
2023-03-24 23:08:44 +08:00
v := value.(string)
2023-05-31 17:41:14 +08:00
m.t = reflect.String
m.v = v
m.s = len(v)
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case int, int8, int16, int32, int64:
2023-05-31 17:41:14 +08:00
m.t = reflect.Int
m.v, _ = toInt(value)
m.s = strconv.IntSize
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-05-31 17:41:14 +08:00
case uint, uint8, uint16, uint32, uint64, uintptr:
m.t = reflect.Uint
m.v, _ = toUInt(value)
m.s = strconv.IntSize
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case []byte:
2023-05-31 17:41:14 +08:00
m.t = SLICE_BYTE
m.v = value.([]byte)
m.s = len(value.([]byte))
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case float32, float64:
2023-05-31 17:41:14 +08:00
m.t = reflect.Float64
m.v, _ = toFloat64(value)
m.s = 8
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case bool:
2023-05-31 17:41:14 +08:00
m.t = reflect.Bool
m.v = value
m.s = 1
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case []any:
2023-05-31 17:41:14 +08:00
m.t = reflect.Slice
m.v = value
m.s = len(value.([]any))
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
case map[string]any:
2023-05-31 17:41:14 +08:00
m.t = reflect.Map
m.v = value
m.s = len(value.(map[string]any))
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
default:
if v := NewJSONArray(value); v != nil {
2023-05-31 17:41:14 +08:00
m.t = v.Type()
m.v = v.Data()
m.s = v.Size()
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
} else if v := NewJSONObject(value); v != nil {
2023-05-31 17:41:14 +08:00
m.t = v.Type()
m.v = v.Data()
m.s = v.Size()
2023-03-24 23:08:44 +08:00
m.modifyParentValue()
2023-03-24 22:23:23 +08:00
}
}
}
2023-05-31 17:41:14 +08:00
func (m *JsonData) ToJSONString() string {
return string(m.Bytes())
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
// ConvertToData to map / slice / value
func (m *JsonData) ConvertToData() any {
2023-03-10 15:10:40 +08:00
if m.IsObject() {
2023-05-31 17:41:14 +08:00
result := make(map[string]any, m.s)
for k, _ := range m.v.(map[string]any) {
v := m.GetByKey(k)
if v == nil {
result[k] = nil
continue
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
if v.IsObject() || v.IsArray() {
if v.JsonData().d {
result[k] = nil
continue
}
v.JsonData().d = true
result[k] = v.JsonData().ConvertToData()
v.JsonData().d = false
2023-03-10 15:46:07 +08:00
} else {
2023-05-31 17:41:14 +08:00
result[k] = v.JsonData().v
2023-03-10 14:42:15 +08:00
}
}
2023-05-31 17:41:14 +08:00
return result
} else if m.IsArray() {
result := make([]any, m.s, m.s)
for i, _ := range m.v.([]any) {
v := m.GetByIndex(i)
if v == nil {
result[i] = nil
continue
}
if v.IsObject() || v.IsArray() {
if v.JsonData().d {
result[i] = nil
continue
}
v.JsonData().d = true
result[i] = v.JsonData().ConvertToData()
v.JsonData().d = false
} else {
result[i] = v.JsonData().v
2023-03-13 10:50:45 +08:00
}
2023-03-10 14:42:15 +08:00
}
return result
2023-05-31 17:41:14 +08:00
} else {
return m.v
2023-03-10 14:42:15 +08:00
}
2023-03-11 15:30:10 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsString() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.String
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsInt() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.Int
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsUInt() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.Uint
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsBytes() bool {
2023-05-31 17:41:14 +08:00
return m.t == SLICE_BYTE
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsFloat() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.Float64
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsBool() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.Bool
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsObject() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.Map
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) IsArray() bool {
2023-05-31 17:41:14 +08:00
return m.t == reflect.Slice
2023-03-10 14:42:15 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) Clear() {
2023-03-10 15:10:40 +08:00
if m.IsObject() {
2023-05-31 17:41:14 +08:00
m.v = make(map[string]any, 0)
2023-03-10 15:10:40 +08:00
} else if m.IsArray() {
2023-05-31 17:41:14 +08:00
m.v = make([]any, 0)
2023-03-10 15:10:40 +08:00
} else {
2023-05-31 17:41:14 +08:00
m.v = nil
2023-03-10 15:10:40 +08:00
}
2023-05-31 17:41:14 +08:00
m.s = 0
2023-03-12 21:02:35 +08:00
}
2023-03-24 22:23:23 +08:00
func (m *JsonData) Free() {
2023-03-14 17:30:45 +08:00
if m == nil {
return
}
2023-05-31 17:41:14 +08:00
m.v = nil
m.s = 0
m.t = reflect.Invalid
2023-03-10 15:10:40 +08:00
}
2023-05-31 17:41:14 +08:00
func toBytes(s any) []byte {
2023-03-14 23:07:51 +08:00
switch s.(type) {
case []byte:
return s.([]byte)
case string:
return []byte(s.(string))
case bool:
return []byte{common.BoolToByte(s.(bool))}
case float32:
return common.Float32ToBytes(s.(float32))
case float64:
return common.Float64ToBytes(s.(float64))
case int:
return common.IntToBytes(s.(int))
case int8:
return common.Int8ToBytes(s.(int8))
case int16:
return common.Int16ToBytes(s.(int16))
case int32:
return common.Int32ToBytes(s.(int32))
case int64:
return common.Int64ToBytes(s.(int64))
case uint:
return common.UIntToBytes(s.(uint))
case uint8:
return common.UInt8ToBytes(s.(uint8))
case uint16:
return common.UInt16ToBytes(s.(uint16))
case uint32:
return common.UInt32ToBytes(s.(uint32))
case uint64:
return common.UInt64ToBytes(s.(uint64))
2023-05-31 17:41:14 +08:00
default:
2023-03-14 23:07:51 +08:00
if r, err := jsoniter.Marshal(s); err == nil {
return r
}
}
return nil
}
2023-05-31 17:41:14 +08:00
func toFloat64(s any) (result float64, ok bool) {
ok = true
2023-03-10 14:42:15 +08:00
switch s.(type) {
case float32:
2023-05-31 17:41:14 +08:00
result = float64(s.(float32))
2023-03-10 14:42:15 +08:00
case float64:
2023-05-31 17:41:14 +08:00
result = s.(float64)
2023-03-10 14:42:15 +08:00
case int:
2023-05-31 17:41:14 +08:00
result = float64(s.(int))
2023-03-10 14:42:15 +08:00
case int8:
2023-05-31 17:41:14 +08:00
result = float64(s.(int8))
2023-03-10 14:42:15 +08:00
case int16:
2023-05-31 17:41:14 +08:00
result = float64(s.(int16))
2023-03-10 14:42:15 +08:00
case int32:
2023-05-31 17:41:14 +08:00
result = float64(s.(int32))
2023-03-10 14:42:15 +08:00
case int64:
2023-05-31 17:41:14 +08:00
result = float64(s.(int64))
2023-03-10 14:42:15 +08:00
case uint:
2023-05-31 17:41:14 +08:00
result = float64(s.(uint))
2023-03-10 14:42:15 +08:00
case uint8:
2023-05-31 17:41:14 +08:00
result = float64(s.(uint8))
2023-03-10 14:42:15 +08:00
case uint16:
2023-05-31 17:41:14 +08:00
result = float64(s.(uint16))
2023-03-10 14:42:15 +08:00
case uint32:
2023-05-31 17:41:14 +08:00
result = float64(s.(uint32))
2023-03-10 14:42:15 +08:00
case uint64:
2023-05-31 17:41:14 +08:00
result = float64(s.(uint64))
default:
ok = false
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
return
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func toInt(s any) (result int, ok bool) {
ok = true
2023-03-10 14:42:15 +08:00
switch s.(type) {
case float32:
2023-05-31 17:41:14 +08:00
result = int(s.(float32))
2023-03-10 14:42:15 +08:00
case float64:
2023-05-31 17:41:14 +08:00
result = int(s.(float64))
2023-03-10 14:42:15 +08:00
case int:
2023-05-31 17:41:14 +08:00
result = s.(int)
2023-03-10 14:42:15 +08:00
case int8:
2023-05-31 17:41:14 +08:00
result = int(s.(int8))
2023-03-10 14:42:15 +08:00
case int16:
2023-05-31 17:41:14 +08:00
result = int(s.(int16))
2023-03-10 14:42:15 +08:00
case int32:
2023-05-31 17:41:14 +08:00
result = int(s.(int32))
2023-03-10 14:42:15 +08:00
case int64:
2023-05-31 17:41:14 +08:00
result = int(s.(int64))
2023-03-10 14:42:15 +08:00
case uint:
2023-05-31 17:41:14 +08:00
result = int(s.(uint))
2023-03-10 14:42:15 +08:00
case uint8:
2023-05-31 17:41:14 +08:00
result = int(s.(uint8))
2023-03-10 14:42:15 +08:00
case uint16:
2023-05-31 17:41:14 +08:00
result = int(s.(uint16))
2023-03-10 14:42:15 +08:00
case uint32:
2023-05-31 17:41:14 +08:00
result = int(s.(uint32))
2023-03-10 14:42:15 +08:00
case uint64:
2023-05-31 17:41:14 +08:00
result = int(s.(uint64))
default:
ok = false
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
return
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
func toInt64(s any) (result int64, ok bool) {
ok = true
2023-03-10 14:42:15 +08:00
switch s.(type) {
case float32:
2023-05-31 17:41:14 +08:00
result = int64(s.(float32))
2023-03-10 14:42:15 +08:00
case float64:
2023-05-31 17:41:14 +08:00
result = int64(s.(float64))
2023-03-10 14:42:15 +08:00
case int:
2023-05-31 17:41:14 +08:00
result = int64(s.(int))
2023-03-10 14:42:15 +08:00
case int8:
2023-05-31 17:41:14 +08:00
result = int64(s.(int8))
2023-03-10 14:42:15 +08:00
case int16:
2023-05-31 17:41:14 +08:00
result = int64(s.(int16))
2023-03-10 14:42:15 +08:00
case int32:
2023-05-31 17:41:14 +08:00
result = int64(s.(int32))
2023-03-10 14:42:15 +08:00
case int64:
2023-05-31 17:41:14 +08:00
result = s.(int64)
2023-03-10 14:42:15 +08:00
case uint:
2023-05-31 17:41:14 +08:00
result = int64(s.(uint))
2023-03-10 14:42:15 +08:00
case uint8:
2023-05-31 17:41:14 +08:00
result = int64(s.(uint8))
2023-03-10 14:42:15 +08:00
case uint16:
2023-05-31 17:41:14 +08:00
result = int64(s.(uint16))
2023-03-10 14:42:15 +08:00
case uint32:
2023-05-31 17:41:14 +08:00
result = int64(s.(uint32))
2023-03-10 14:42:15 +08:00
case uint64:
2023-05-31 17:41:14 +08:00
result = int64(s.(uint64))
default:
ok = false
2023-03-10 14:42:15 +08:00
}
2023-05-31 17:41:14 +08:00
return
2023-03-10 14:42:15 +08:00
}
2023-03-23 17:33:33 +08:00
2023-05-31 17:41:14 +08:00
func toUInt(s any) (result uint, ok bool) {
ok = true
2023-03-23 17:33:33 +08:00
switch s.(type) {
case float32:
2023-05-31 17:41:14 +08:00
result = uint(s.(float32))
2023-03-23 17:33:33 +08:00
case float64:
2023-05-31 17:41:14 +08:00
result = uint(s.(float64))
2023-03-23 17:33:33 +08:00
case int:
2023-05-31 17:41:14 +08:00
result = uint(s.(int))
2023-03-23 17:33:33 +08:00
case int8:
2023-05-31 17:41:14 +08:00
result = uint(s.(int8))
2023-03-23 17:33:33 +08:00
case int16:
2023-05-31 17:41:14 +08:00
result = uint(s.(int16))
2023-03-23 17:33:33 +08:00
case int32:
2023-05-31 17:41:14 +08:00
result = uint(s.(int32))
2023-03-23 17:33:33 +08:00
case int64:
2023-05-31 17:41:14 +08:00
result = uint(s.(int64))
2023-03-23 17:33:33 +08:00
case uint:
2023-05-31 17:41:14 +08:00
result = s.(uint)
2023-03-23 17:33:33 +08:00
case uint8:
2023-05-31 17:41:14 +08:00
result = uint(s.(uint8))
2023-03-23 17:33:33 +08:00
case uint16:
2023-05-31 17:41:14 +08:00
result = uint(s.(uint16))
2023-03-23 17:33:33 +08:00
case uint32:
2023-05-31 17:41:14 +08:00
result = uint(s.(uint32))
2023-03-23 17:33:33 +08:00
case uint64:
2023-05-31 17:41:14 +08:00
result = uint(s.(uint64))
default:
ok = false
2023-03-23 17:33:33 +08:00
}
2023-05-31 17:41:14 +08:00
return
2023-03-23 17:33:33 +08:00
}
2023-05-31 17:41:14 +08:00
func toUInt64(s any) (result uint64, ok bool) {
ok = true
2023-03-23 17:33:33 +08:00
switch s.(type) {
case float32:
2023-05-31 17:41:14 +08:00
result = uint64(s.(float32))
2023-03-23 17:33:33 +08:00
case float64:
2023-05-31 17:41:14 +08:00
result = uint64(s.(float64))
2023-03-23 17:33:33 +08:00
case int:
2023-05-31 17:41:14 +08:00
result = uint64(s.(int))
2023-03-23 17:33:33 +08:00
case int8:
2023-05-31 17:41:14 +08:00
result = uint64(s.(int8))
2023-03-23 17:33:33 +08:00
case int16:
2023-05-31 17:41:14 +08:00
result = uint64(s.(int16))
2023-03-23 17:33:33 +08:00
case int32:
2023-05-31 17:41:14 +08:00
result = uint64(s.(int32))
2023-03-23 17:33:33 +08:00
case int64:
2023-05-31 17:41:14 +08:00
result = uint64(s.(int64))
2023-03-23 17:33:33 +08:00
case uint:
2023-05-31 17:41:14 +08:00
result = uint64(s.(uint))
2023-03-23 17:33:33 +08:00
case uint8:
2023-05-31 17:41:14 +08:00
result = uint64(s.(uint8))
2023-03-23 17:33:33 +08:00
case uint16:
2023-05-31 17:41:14 +08:00
result = uint64(s.(uint16))
2023-03-23 17:33:33 +08:00
case uint32:
2023-05-31 17:41:14 +08:00
result = uint64(s.(uint32))
2023-03-23 17:33:33 +08:00
case uint64:
2023-05-31 17:41:14 +08:00
result = s.(uint64)
default:
ok = false
2023-03-23 17:33:33 +08:00
}
2023-05-31 17:41:14 +08:00
return
}
func toBool(s any) (result, ok bool) {
ok = true
switch s.(type) {
case bool:
result = s.(bool)
case []uint8:
if v := s.([]uint8); len(v) > 0 {
result = v[0] != 0
}
case string:
result = s.(string) != ""
default:
if v, vok := toFloat64(s); vok {
result = v > 0
} else {
ok = false
}
}
return
}
func isBaseType(v any) bool {
switch v.(type) {
case string, float32, float64, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr:
return true
}
return false
2023-03-23 17:33:33 +08:00
}