增加转换为map

This commit is contained in:
白茶清欢 2023-06-02 16:40:22 +08:00
parent a7bceb192c
commit bb18c3cbfc

View File

@ -9,6 +9,8 @@ package wrapper
import ( import (
"encoding/json" "encoding/json"
"errors"
"git.zhangdeman.cn/zhangdeman/util"
"reflect" "reflect"
) )
@ -19,10 +21,11 @@ import (
// Date : 18:36 2023/6/1 // Date : 18:36 2023/6/1
func ObjectData(data interface{}) *ObjectType { func ObjectData(data interface{}) *ObjectType {
ot := &ObjectType{ ot := &ObjectType{
source: data, source: data,
data: map[interface{}]interface{}{}, data: map[interface{}]interface{}{},
byteData: []byte{}, byteData: []byte{},
isValid: true, isValid: true,
invalidErr: errors.New("data is invalid"),
} }
if nil == ot { if nil == ot {
return ot return ot
@ -46,10 +49,11 @@ func ObjectData(data interface{}) *ObjectType {
// //
// Date : 18:38 2023/6/1 // Date : 18:38 2023/6/1
type ObjectType struct { type ObjectType struct {
source interface{} source interface{}
data map[interface{}]interface{} data map[interface{}]interface{}
byteData []byte byteData []byte
isValid bool isValid bool
invalidErr error
} }
// IsValid 是否有效对象数据 // IsValid 是否有效对象数据
@ -75,13 +79,39 @@ func (ot *ObjectType) IsNil() bool {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:51 2023/6/1 // Date : 18:51 2023/6/1
func (ot *ObjectType) ToString() string { func (ot *ObjectType) ToString() StringResult {
if ot.IsNil() { if ot.IsNil() {
return "nil" return StringResult{
Value: "nil",
Err: nil,
}
} }
if !ot.IsValid() { if !ot.IsValid() {
// 非法对象数据 // 非法对象数据
return "" return StringResult{
Value: "",
Err: errors.New("data is invalid"),
}
}
return StringResult{
Value: string(ot.byteData),
Err: nil,
} }
return string(ot.byteData) }
// ToMapStringAny ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:17 2023/6/2
func (ot *ObjectType) ToMapStringAny() ObjectResult {
res := ObjectResult{
Value: map[string]interface{}{},
Err: nil,
}
if ot.IsNil() {
return res
}
res.Err = util.JSON.UnmarshalWithNumber(ot.byteData, &res.Value)
return res
} }