Files
structx/entity.go
T
2025-09-21 12:06:32 +08:00

78 lines
1.9 KiB
Go

package structx
import (
"reflect"
"sync"
)
// ChangeInfo 变更信息
type ChangeInfo struct {
Old string `json:"old"`
New string `json:"new"`
Val any `json:"val"`
}
// FieldInfo 字段信息
type FieldInfo struct {
Index []int
Name string
IsPtr bool
FieldType reflect.Type
IsSlice bool
IsArray bool
}
// converterFunc 类型转换函数
type converterFunc func(reflect.Value, string) (interface{}, error)
var (
typeConverters = map[reflect.Kind]converterFunc{
reflect.Bool: convertBool,
reflect.Int: convertInt[int],
reflect.Int8: convertInt[int8],
reflect.Int16: convertInt[int16],
reflect.Int32: convertInt[int32],
reflect.Int64: convertInt[int64],
reflect.Uint: convertUint[uint],
reflect.Uint8: convertUint[uint8],
reflect.Uint16: convertUint[uint16],
reflect.Uint32: convertUint[uint32],
reflect.Uint64: convertUint[uint64],
reflect.Float32: convertFloat[float32],
reflect.Float64: convertFloat[float64],
reflect.String: convertString,
reflect.Slice: convertSlice,
reflect.Array: convertArray,
reflect.Map: convertMap,
}
typeInfoCache = make(map[reflect.Type]map[string]FieldInfo)
cacheMutex = &sync.RWMutex{}
basicStructTypes = map[string]bool{
"time.Time": true,
"github.com/shopspring/decimal.Decimal": true,
"sql.NullString": true,
"sql.NullInt64": true,
"sql.NullBool": true,
"sql.NullFloat64": true,
}
basicKinds = map[reflect.Kind]bool{
reflect.String: true,
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Float32: true,
reflect.Float64: true,
reflect.Bool: true,
}
)