From 824c0984818785645268e85f0c941dca9290cfe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 10 Aug 2023 16:58:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0struct=E7=9A=84=E5=8C=85?= =?UTF-8?q?=E8=A3=85=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define.go | 10 ++++++++ map.go | 34 +++++++++++++++++++++++++++ struct.go | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 struct.go diff --git a/define.go b/define.go index 7ae43ca..1c03b51 100644 --- a/define.go +++ b/define.go @@ -496,3 +496,13 @@ type AnySliceResult struct { Value []interface{} Err error } + +// MapResult 转map的结果 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:05 2023/8/10 +type MapResult struct { + Value Map + Err error +} diff --git a/map.go b/map.go index f2695c8..127b707 100644 --- a/map.go +++ b/map.go @@ -8,6 +8,8 @@ package wrapper import ( + "bytes" + "encoding/json" "errors" "git.zhangdeman.cn/zhangdeman/easymap" "reflect" @@ -43,6 +45,38 @@ func EasyMapWithError(mapData interface{}) (Map, error) { return m, nil } +// EasyMapFromStruct 从struct转map +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:11 2023/8/10 +func EasyMapFromStruct(data interface{}) Map { + byteData, _ := json.Marshal(data) + return EasyMapFromByte(byteData) +} + +// EasyMapFromString 从string转为Map +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:12 2023/8/10 +func EasyMapFromString(data string) Map { + return EasyMapFromByte([]byte(data)) +} + +// EasyMapFromByte 从字节数组转为Map +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:12 2023/8/10 +func EasyMapFromByte(data []byte) Map { + var tmpMap map[interface{}]interface{} + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.UseNumber() + _ = decoder.Decode(&tmpMap) + return EasyMap(tmpMap) +} + // Map ... // // Author : go_developer@163.com<白茶清欢> diff --git a/struct.go b/struct.go new file mode 100644 index 0000000..2b1b89c --- /dev/null +++ b/struct.go @@ -0,0 +1,69 @@ +// Package wrapper ... +// +// Description : wrapper ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-08-10 16:05 +package wrapper + +import ( + "errors" + "reflect" +) + +// NewStruct 包装的数据类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:07 2023/8/10 +func NewStruct(data interface{}) *Struct { + s, _ := NewStructWithError(data) + return s +} + +// NewStructWithError ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:17 2023/8/10 +func NewStructWithError(data interface{}) (*Struct, error) { + if data == nil { + return nil, errors.New("input data is nil") + } + reflectType := reflect.TypeOf(data) + if reflectType.Kind() == reflect.Ptr { + reflectType = reflectType.Elem() + if reflectType.Kind() != reflect.Struct { + return nil, errors.New("input data type is " + reflectType.Elem().Kind().String()) + } + } + return &Struct{data: data}, nil +} + +// Struct 结构体类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:05 2023/8/10 +type Struct struct { + data interface{} +} + +// ToMap 转为Map +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 16:08 2023/8/10 +func (s *Struct) ToMap() MapResult { + if nil == s.data { + return MapResult{ + Value: EasyMap(map[interface{}]interface{}{}), + Err: nil, + } + } + return MapResult{ + Value: EasyMapFromStruct(s.data), + Err: nil, + } +} -- 2.36.6