2023-06-01 18:35:45 +08:00
|
|
|
// Package wrapper ...
|
|
|
|
//
|
|
|
|
// Description : wrapper ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2023-06-01 18:33
|
|
|
|
package wrapper
|
|
|
|
|
2023-06-01 18:47:53 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
|
|
|
)
|
2023-06-01 18:35:45 +08:00
|
|
|
|
2023-06-01 18:47:53 +08:00
|
|
|
// ObjectData 对象类型, 支持 nil / Map / Struct
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:36 2023/6/1
|
|
|
|
func ObjectData(data interface{}) *ObjectType {
|
|
|
|
ot := &ObjectType{
|
|
|
|
source: data,
|
|
|
|
data: map[interface{}]interface{}{},
|
|
|
|
byteData: []byte{},
|
|
|
|
isValid: true,
|
|
|
|
}
|
|
|
|
if nil == ot {
|
|
|
|
return ot
|
|
|
|
}
|
|
|
|
reflectType := reflect.TypeOf(data)
|
|
|
|
switch reflectType.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
fallthrough
|
|
|
|
case reflect.Struct:
|
|
|
|
ot.byteData, _ = json.Marshal(ot.source)
|
|
|
|
default:
|
|
|
|
// 数据类型不是 nil / map / struct 质疑
|
|
|
|
ot.isValid = false
|
|
|
|
}
|
|
|
|
return ot
|
2023-06-01 18:35:45 +08:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:47:53 +08:00
|
|
|
// ObjectType ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:38 2023/6/1
|
2023-06-01 18:35:45 +08:00
|
|
|
type ObjectType struct {
|
2023-06-01 18:47:53 +08:00
|
|
|
source interface{}
|
|
|
|
data map[interface{}]interface{}
|
|
|
|
byteData []byte
|
|
|
|
isValid bool
|
2023-06-01 18:35:45 +08:00
|
|
|
}
|
2023-06-01 18:52:48 +08:00
|
|
|
|
|
|
|
// IsValid 是否有效对象数据
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:49 2023/6/1
|
|
|
|
func (ot *ObjectType) IsValid() bool {
|
|
|
|
return ot.isValid
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil 是否为nil
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:50 2023/6/1
|
|
|
|
func (ot *ObjectType) IsNil() bool {
|
|
|
|
return ot.source == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToString 转字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:51 2023/6/1
|
|
|
|
func (ot *ObjectType) ToString() string {
|
|
|
|
if ot.IsNil() {
|
|
|
|
return "nil"
|
|
|
|
}
|
|
|
|
if !ot.IsValid() {
|
|
|
|
// 非法对象数据
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(ot.byteData)
|
|
|
|
}
|