energy/pkgs/json/json.go

684 lines
14 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
//
//----------------------------------------
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"
jsoniter "github.com/json-iterator/go"
"reflect"
"strconv"
2023-03-10 15:46:07 +08:00
"strings"
2023-03-10 14:42:15 +08:00
)
2023-03-10 15:10:40 +08:00
type BaseJSON interface {
2023-03-10 14:42:15 +08:00
Size() int
Type() GO_VALUE_TYPE
Data() any
String() string
Int() int
UInt() uint
Bytes() []byte
Float() float64
Bool() bool
2023-03-11 15:30:10 +08:00
JSONObject() JSONObject
JSONArray() JSONArray
ToJSONString() string
2023-03-10 14:42:15 +08:00
IsString() bool
IsInt() bool
IsUInt() bool
IsBytes() bool
IsFloat() bool
IsBool() bool
2023-03-10 15:10:40 +08:00
IsObject() bool
2023-03-10 14:42:15 +08:00
IsArray() bool
2023-03-10 15:10:40 +08:00
Clear()
2023-03-10 14:42:15 +08:00
}
2023-03-10 15:10:40 +08:00
type JSONArray interface {
BaseJSON
2023-03-11 15:30:10 +08:00
Add(value ...any)
SetByIndex(index int, value any)
2023-03-10 14:42:15 +08:00
RemoveByIndex(index int)
2023-03-10 15:10:40 +08:00
GetStringByIndex(index int) string
GetIntByIndex(index int) int
GetUIntByIndex(index int) uint
GetBytesByIndex(index int) []byte
GetFloatByIndex(index int) float64
GetBoolByIndex(index int) bool
GetArrayByIndex(index int) JSONArray
GetObjectByIndex(index int) JSONObject
GetByIndex(index int) JSON
}
type JSONObject interface {
BaseJSON
2023-03-10 14:42:15 +08:00
Set(key, value string)
RemoveByKey(key string)
2023-03-10 15:10:40 +08:00
GetStringByKey(key string) string
GetIntByKey(key string) int
GetUIntByKey(key string) uint
GetBytesByKey(key string) []byte
GetFloatByKey(key string) float64
GetBoolByKey(key string) bool
GetArrayByKey(key string) JSONArray
GetObjectByKey(key string) JSONObject
GetByKey(key string) JSON
2023-03-10 14:42:15 +08:00
Keys() []string
}
2023-03-10 15:10:40 +08:00
// JSON Object
type JSON interface {
JSONArray
JSONObject
2023-03-10 14:42:15 +08:00
}
2023-03-10 15:10:40 +08:00
type jsonData struct {
2023-03-10 14:42:15 +08:00
T GO_VALUE_TYPE // type
S int // size
V any // value
}
2023-03-10 15:10:40 +08:00
// NewJSON 返回 jsonData 对象JSONArray or JSONObject
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-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_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-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
}
return nil
}
2023-03-10 15:10:40 +08:00
// NewJSONArray 字节JSONArray / 数组 / 切片 转换
func NewJSONArray(value any) JSONArray {
2023-03-10 14:42:15 +08:00
if value != nil {
// 如果 []byte 就必须是 字节JSONArray
switch value.(type) {
case []byte:
if v := NewJSON(value.([]byte)); v != nil {
2023-03-11 15:30:10 +08:00
return v.JSONArray()
2023-03-10 14:42:15 +08:00
} else {
return nil
}
}
rv := reflect.ValueOf(value)
if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
return nil
}
//目的是为了转为any类型
if byt, err := jsoniter.Marshal(value); err == nil {
var v []any
if err = jsoniter.Unmarshal(byt, &v); err == nil {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
}
2023-03-11 15:30:10 +08:00
return &jsonData{T: GO_VALUE_SLICE, V: make([]any, 0), S: 0}
2023-03-10 14:42:15 +08:00
}
2023-03-11 15:30:10 +08:00
// NewJSONObject 字节JSONObject / 结构 / JSONObject 转换
2023-03-10 15:10:40 +08:00
func NewJSONObject(value any) JSONObject {
2023-03-10 14:42:15 +08:00
if value != nil {
// 如果 []byte 就必须是 字节JSONObject
switch value.(type) {
case []byte:
if v := NewJSON(value.([]byte)); v != nil {
2023-03-11 15:30:10 +08:00
return v.JSONObject()
2023-03-10 14:42:15 +08:00
} else {
return nil
}
}
rv := reflect.ValueOf(value)
kind := rv.Kind()
if kind != reflect.Map && kind != reflect.Struct {
return nil
}
//目的是为了转为any类型
if byt, err := jsoniter.Marshal(value); err == nil {
var v map[string]any
if err = jsoniter.Unmarshal(byt, &v); err == nil {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
}
2023-03-11 15:30:10 +08:00
return &jsonData{T: GO_VALUE_MAP, V: make(map[string]any), S: 0}
2023-03-10 14:42:15 +08:00
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Size() int {
2023-03-10 14:42:15 +08:00
return m.S
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Type() GO_VALUE_TYPE {
2023-03-10 14:42:15 +08:00
return m.T
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Data() any {
2023-03-10 14:42:15 +08:00
return m.V
}
2023-03-11 15:30:10 +08:00
func (m *jsonData) Add(value ...any) {
2023-03-10 14:42:15 +08:00
if m.IsArray() {
2023-03-11 15:30:10 +08:00
m.V = append(m.V.([]any), value...)
m.S += len(value)
}
}
func (m *jsonData) SetByIndex(index int, value any) {
if m.IsArray() && index < m.S {
switch value.(type) {
case JSON:
m.V.([]any)[index] = value.(JSON).Data()
case JSONObject:
m.V.([]any)[index] = value.(JSONObject).Data()
case JSONArray:
m.V.([]any)[index] = value.(JSONArray).Data()
default:
m.V.([]any)[index] = value
}
2023-03-10 14:42:15 +08:00
}
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) RemoveByIndex(index int) {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index >= 0 && index < m.S {
v := m.V.([]any)
m.V = append(v[:index], v[index+1:]...)
m.S--
}
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetStringByIndex(index int) string {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
if r, ok := m.V.([]any)[index].(string); ok {
return r
}
}
return ""
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetIntByIndex(index int) int {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
return m.toInt(m.V.([]any)[index])
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetUIntByIndex(index int) uint {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
return m.toUInt(m.V.([]any)[index])
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetBytesByIndex(index int) []byte {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
return m.toBytes(m.V.([]any)[index])
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetFloatByIndex(index int) float64 {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
return m.toFloat64(m.V.([]any)[index])
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetBoolByIndex(index int) bool {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
s := m.V.([]any)[index]
switch s.(type) {
case bool:
return s.(bool)
}
}
return false
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetArrayByIndex(index int) JSONArray {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
if v, ok := m.V.([]any)[index].([]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetObjectByIndex(index int) JSONObject {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
if v, ok := m.V.([]any)[index].(map[string]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetByIndex(index int) JSON {
2023-03-10 14:42:15 +08:00
if m.IsArray() && index < m.S {
s := m.V.([]any)[index]
switch s.(type) {
2023-03-10 15:46:07 +08:00
case json.Number:
if v, err := s.(json.Number).Int64(); err == nil {
return &jsonData{T: GO_VALUE_INT, V: v, S: strconv.IntSize}
}
2023-03-10 14:42:15 +08:00
case string:
if r, ok := s.(string); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_STRING, V: r, S: len(r)}
2023-03-10 14:42:15 +08:00
}
case int, int8, int16, int32, int64:
2023-03-10 15:10:40 +08:00
if s := m.GetIntByIndex(index); s != 0 {
return &jsonData{T: GO_VALUE_INT, V: s, S: strconv.IntSize}
2023-03-10 14:42:15 +08:00
}
case uint, uint8, uint16, uint32, uint64:
2023-03-10 15:10:40 +08:00
if s := m.GetUIntByIndex(index); s != 0 {
return &jsonData{T: GO_VALUE_UINT, V: s, S: strconv.IntSize}
2023-03-10 14:42:15 +08:00
}
case []byte:
2023-03-10 15:10:40 +08:00
if s := m.GetBytesByIndex(index); s != nil {
return &jsonData{T: GO_VALUE_SLICE_BYTE, V: s, S: len(s)}
2023-03-10 14:42:15 +08:00
}
case float32, float64:
2023-03-10 15:46:07 +08:00
//不带有 . 转为 int 类型
sv := m.toFloat64(s)
if strings.Index(strconv.FormatFloat(sv, 'G', -1, 64), ".") != -1 {
return &jsonData{T: GO_VALUE_FLOAT64, V: sv, S: 8}
} else {
return &jsonData{T: GO_VALUE_INT, V: sv, S: strconv.IntSize}
2023-03-10 14:42:15 +08:00
}
case bool:
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_BOOL, V: m.GetBoolByIndex(index), S: 1}
2023-03-10 14:42:15 +08:00
case []any:
if index < m.S {
if v, ok := m.V.([]any)[index].([]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
case map[string]any:
if v, ok := s.(map[string]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Set(key, value string) {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
m.V.(map[string]any)[key] = value
m.S++
}
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) RemoveByKey(key string) {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
if _, ok := m.V.(map[string]any)[key]; ok {
delete(m.V.(map[string]any), key)
m.S--
}
}
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetStringByKey(key string) string {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
if r, ok := m.V.(map[string]any)[key].(string); ok {
return r
}
}
return ""
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetIntByKey(key string) int {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
return m.toInt(m.V.(map[string]any)[key])
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetUIntByKey(key string) uint {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
return m.toUInt(m.V.(map[string]any)[key])
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetBytesByKey(key string) []byte {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
return m.toBytes(m.V.(map[string]any)[key])
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetFloatByKey(key string) float64 {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
return m.toFloat64(m.V.(map[string]any)[key])
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetBoolByKey(key string) bool {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
if s, ok := m.V.(map[string]any)[key].(bool); ok {
return s
}
}
return false
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetArrayByKey(key string) JSONArray {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
if v, ok := m.V.(map[string]any)[key].([]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetObjectByKey(key string) JSONObject {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
if v, ok := m.V.(map[string]any)[key].(map[string]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) GetByKey(key string) JSON {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
s := m.V.(map[string]any)[key]
switch s.(type) {
2023-03-10 15:46:07 +08:00
case json.Number:
if v, err := s.(json.Number).Int64(); err == nil {
return &jsonData{T: GO_VALUE_INT, V: v, S: strconv.IntSize}
}
2023-03-10 14:42:15 +08:00
case string:
if r, ok := s.(string); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_STRING, V: r, S: len(r)}
2023-03-10 14:42:15 +08:00
}
case int, int8, int16, int32, int64:
2023-03-10 15:10:40 +08:00
if s := m.GetIntByKey(key); s != 0 {
return &jsonData{T: GO_VALUE_INT, V: s, S: strconv.IntSize}
2023-03-10 14:42:15 +08:00
}
case uint, uint8, uint16, uint32, uint64:
2023-03-10 15:10:40 +08:00
if s := m.GetUIntByKey(key); s != 0 {
return &jsonData{T: GO_VALUE_UINT, V: s, S: strconv.IntSize}
2023-03-10 14:42:15 +08:00
}
case []byte:
2023-03-10 15:10:40 +08:00
if s := m.GetBytesByKey(key); s != nil {
return &jsonData{T: GO_VALUE_SLICE_BYTE, V: s, S: len(s)}
2023-03-10 14:42:15 +08:00
}
case float32, float64:
2023-03-10 15:46:07 +08:00
//不带有 . 转为 int 类型
sv := m.toFloat64(s)
if strings.Index(strconv.FormatFloat(sv, 'G', -1, 64), ".") != -1 {
return &jsonData{T: GO_VALUE_FLOAT64, V: sv, S: 8}
} else {
return &jsonData{T: GO_VALUE_INT, V: int(sv), S: strconv.IntSize}
2023-03-10 14:42:15 +08:00
}
case bool:
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_BOOL, V: m.GetBytesByKey(key), S: 1}
2023-03-10 14:42:15 +08:00
case []any:
if v, ok := m.V.(map[string]any)[key].([]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_SLICE, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
case map[string]any:
if v, ok := s.(map[string]any); ok {
2023-03-10 15:10:40 +08:00
return &jsonData{T: GO_VALUE_MAP, V: v, S: len(v)}
2023-03-10 14:42:15 +08:00
}
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) String() string {
2023-03-10 14:42:15 +08:00
if m.IsString() {
return m.V.(string)
}
return ""
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Int() int {
2023-03-10 14:42:15 +08:00
return m.toInt(m.V)
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) UInt() uint {
2023-03-10 14:42:15 +08:00
return m.toUInt(m.V)
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Bytes() []byte {
2023-03-11 00:02:03 +08:00
return m.toBytes(m.V)
2023-03-10 14:42:15 +08:00
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Float() float64 {
2023-03-10 14:42:15 +08:00
return m.toFloat64(m.V)
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Bool() bool {
2023-03-10 14:42:15 +08:00
if m.IsBool() {
return m.V.(bool)
}
return false
}
2023-03-11 15:30:10 +08:00
func (m *jsonData) JSONObject() JSONObject {
2023-03-10 15:10:40 +08:00
if m.IsObject() {
2023-03-10 14:42:15 +08:00
return m
}
return nil
}
2023-03-11 15:30:10 +08:00
func (m *jsonData) JSONArray() JSONArray {
2023-03-10 14:42:15 +08:00
if m.IsArray() {
return m
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) toBytes(s any) []byte {
2023-03-10 14:42:15 +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))
default: // slice or map or other
if r, err := jsoniter.Marshal(s); err == nil {
return r
}
}
return nil
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Keys() []string {
if m.IsObject() {
2023-03-10 14:42:15 +08:00
var result []string
for key, _ := range m.V.(map[string]any) {
result = append(result, key)
}
return result
}
return nil
}
2023-03-11 15:30:10 +08:00
func (m *jsonData) ToJSONString() string {
return string(m.Bytes())
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsString() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_STRING
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsInt() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_INT
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsUInt() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_UINT
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsBytes() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_SLICE_BYTE
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsFloat() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_FLOAT64
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsBool() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_BOOL
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsObject() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_MAP
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) IsArray() bool {
2023-03-10 14:42:15 +08:00
return m.T == GO_VALUE_SLICE
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) Clear() {
if m.IsObject() {
m.V = make(map[string]any)
} else if m.IsArray() {
m.V = make([]any, 0)
} else {
m.V = nil
}
m.S = 0
m.T = GO_VALUE_INVALID
}
func (m *jsonData) toFloat64(s any) float64 {
2023-03-10 14:42:15 +08:00
switch s.(type) {
case float32:
return float64(s.(float32))
case float64:
return s.(float64)
case int:
return float64(s.(int))
case int8:
return float64(s.(int8))
case int16:
return float64(s.(int16))
case int32:
return float64(s.(int32))
case int64:
return float64(s.(int64))
case uint:
return float64(s.(uint))
case uint8:
return float64(s.(uint8))
case uint16:
return float64(s.(uint16))
case uint32:
return float64(s.(uint32))
case uint64:
return float64(s.(uint64))
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) toInt(s any) int {
2023-03-10 14:42:15 +08:00
switch s.(type) {
case float32:
return int(s.(float32))
case float64:
return int(s.(float64))
case int:
return s.(int)
case int8:
return int(s.(int8))
case int16:
return int(s.(int16))
case int32:
return int(s.(int32))
case int64:
return int(s.(int64))
case uint:
return int(s.(uint))
case uint8:
return int(s.(uint8))
case uint16:
return int(s.(uint16))
case uint32:
return int(s.(uint32))
case uint64:
return int(s.(uint64))
}
return 0
}
2023-03-10 15:10:40 +08:00
func (m *jsonData) toUInt(s any) uint {
2023-03-10 14:42:15 +08:00
switch s.(type) {
case float32:
return uint(s.(float32))
case float64:
return uint(s.(float64))
case int:
return uint(s.(int))
case int8:
return uint(s.(int8))
case int16:
return uint(s.(int16))
case int32:
return uint(s.(int32))
case int64:
return uint(s.(int64))
case uint:
return s.(uint)
case uint8:
return uint(s.(uint8))
case uint16:
return uint(s.(uint16))
case uint32:
return uint(s.(uint32))
case uint64:
return uint(s.(uint64))
}
return 0
}