From ed0c57913ad15105b834c4ab0febc90053540d98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 12 Jun 2024 16:38:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84any2string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- any.go | 89 +++++++++++++++++++++---------------------------------- define.go | 13 -------- go.mod | 6 ++-- go.sum | 6 ++++ 4 files changed, 42 insertions(+), 72 deletions(-) diff --git a/any.go b/any.go index 5fa0c60..d28e834 100644 --- a/any.go +++ b/any.go @@ -9,6 +9,7 @@ package wrapper import ( "fmt" + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/serialize" "reflect" ) @@ -18,7 +19,7 @@ import ( // Author : go_developer@163.com<白茶清欢> // // Date : 18:19 2023/6/1 -func AnyDataType(data interface{}) *AnyType { +func AnyDataType(data any) *AnyType { at := &AnyType{ data: data, } @@ -32,7 +33,7 @@ func AnyDataType(data interface{}) *AnyType { // // Date : 18:19 2023/6/1 type AnyType struct { - data interface{} + data any dataType string } @@ -56,28 +57,28 @@ func (at *AnyType) Type() string { return at.dataType } if at.IsNil() { - return DataTypeNil + return consts.DataTypeNil } reflectType := reflect.TypeOf(at.data) switch reflectType.Kind() { case reflect.String: - return DataTypeString + return consts.DataTypeString case reflect.Slice, reflect.Array: - return DataTypeSlice + return consts.DataTypeSliceAny case reflect.Map, reflect.Struct: - return DataTypeObject + return consts.DataTypeMapAnyAny case reflect.Pointer: - return DataTypePtr + return consts.DataTypePtr case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return DataTypeInt + return consts.DataTypeInt case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return DataTypeUint + return consts.DataTypeUint case reflect.Bool: - return DataTypeBool + return consts.DataTypeBool case reflect.Float32, reflect.Float64: - return DataTypeFloat + return consts.DataTypeFloat default: - return DataTypeUnknown + return consts.DataTypeUnknown } } @@ -87,51 +88,27 @@ func (at *AnyType) Type() string { // // Date : 18:32 2023/6/1 func (at *AnyType) ToString() String { - switch at.dataType { - case DataTypeNil: + dataType := at.Type() + switch dataType { + case consts.DataTypeUnknown, consts.DataTypeNil: return String("") - case DataTypeObject: - fallthrough - case DataTypeSlice: - fallthrough - case DataTypePtr: + case consts.DataTypeString: + return String(fmt.Sprintf("%v", at.data)) + case consts.DataTypeSliceAny: + var val []any + _ = serialize.JSON.Transition(at.data, &val) + return String(ArrayType[any](val).ToString().Value) + case consts.DataTypeMapAnyAny: + return String(EasyMap(at.data).ToString()) + case consts.DataTypeInt: + return String(Int(at.data.(int64)).ToString().Value) + case consts.DataTypeUint: + return String(Int(at.data.(uint)).ToString().Value) + case consts.DataTypeFloat: + return String(Float(at.data.(float64)).ToString().Value) + case consts.DataTypeBool: + return String(fmt.Sprintf("%v", at.data)) + default: return String(serialize.JSON.MarshalForString(at.data)) } - return String(fmt.Sprintf("%v", at.data)) -} - -// ToObject 任意类型转为对象 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:19 2023/10/11 -func (at *AnyType) ToObject() MapResult { - return MapResult{ - Value: EasyMapFromString(at.ToString().Value()), - Err: nil, - } -} - -func (at *AnyType) ToIntSlice() { - -} - -func (at *AnyType) ToUintSlice() { - -} - -func (at *AnyType) ToStringSlice() { - -} - -func (at *AnyType) ToFloatSlice() { - -} - -func (at *AnyType) ToBoolSlice() { - -} - -func (at *AnyType) ToAnySlice() { - } diff --git a/define.go b/define.go index 5a21d40..687cf0e 100644 --- a/define.go +++ b/define.go @@ -9,19 +9,6 @@ package wrapper import "time" -const ( - DataTypeUnknown = "unknown" - DataTypeNil = "nil" - DataTypePtr = "ptr" - DataTypeString = "string" - DataTypeInt = "int" - DataTypeUint = "uint" - DataTypeBool = "bool" - DataTypeFloat = "float" - DataTypeSlice = "slice" - DataTypeObject = "object" -) - // Int8Result ... // // Author : go_developer@163.com<白茶清欢> diff --git a/go.mod b/go.mod index 7febffc..36a5a44 100644 --- a/go.mod +++ b/go.mod @@ -5,17 +5,17 @@ go 1.21 toolchain go1.21.4 require ( + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 + git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 github.com/stretchr/testify v1.8.4 ) require ( - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc // indirect - git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect - github.com/BurntSushi/toml v1.3.2 // indirect + github.com/BurntSushi/toml v1.4.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect diff --git a/go.sum b/go.sum index ae21fca..a5b85f4 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,10 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240419080457-9d9562469008 h1:6z99+X git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240419080457-9d9562469008/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc h1:kPz9xiUVruM8kwbUUVpxyCTX8pGgyKt60K5zX77oyC4= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240501142503-e31a270e50cc/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608124542-4d97bd80dc68 h1:AaWKU0bKHnNot24OMhaOCBKtpfhz4o05DKHrRFgYd8M= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608124542-4d97bd80dc68/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMXOTy0NFKdodsGKA4gTNHJMfob3yRFYMXrZj7ek= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4 h1:93JYY8JLbFcrlq37q/uKyxs2r2e3modsjvfSbnZQ/UI= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240130062251-a87a97b0e8d4/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U= git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0= @@ -18,6 +22,8 @@ git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI= github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= +github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=