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"
|
2023-06-02 16:40:22 +08:00
|
|
|
"errors"
|
2023-08-11 13:56:40 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
2023-06-01 18:47:53 +08:00
|
|
|
"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
|
2024-06-08 20:06:35 +08:00
|
|
|
func ObjectData(data any) *ObjectType {
|
2023-06-01 18:47:53 +08:00
|
|
|
ot := &ObjectType{
|
2023-06-02 16:40:22 +08:00
|
|
|
source: data,
|
2024-06-08 20:06:35 +08:00
|
|
|
data: map[any]any{},
|
2023-06-02 16:40:22 +08:00
|
|
|
byteData: []byte{},
|
|
|
|
isValid: true,
|
|
|
|
invalidErr: errors.New("data is invalid"),
|
2023-06-01 18:47:53 +08:00
|
|
|
}
|
|
|
|
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:
|
2023-11-28 16:27:31 +08:00
|
|
|
// 数据类型不是 nil / map / struct 之一
|
2023-06-01 18:47:53 +08:00
|
|
|
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 {
|
2024-06-08 20:06:35 +08:00
|
|
|
source any
|
|
|
|
data map[any]any
|
2023-06-02 16:40:22 +08:00
|
|
|
byteData []byte
|
|
|
|
isValid bool
|
|
|
|
invalidErr error
|
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 {
|
2024-08-13 16:30:16 +08:00
|
|
|
if ot.source == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(ot.source).IsNil()
|
2023-06-01 18:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToString 转字符串
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 18:51 2023/6/1
|
2023-06-02 16:40:22 +08:00
|
|
|
func (ot *ObjectType) ToString() StringResult {
|
2023-06-01 18:52:48 +08:00
|
|
|
if ot.IsNil() {
|
2023-06-02 16:40:22 +08:00
|
|
|
return StringResult{
|
|
|
|
Value: "nil",
|
|
|
|
Err: nil,
|
|
|
|
}
|
2023-06-01 18:52:48 +08:00
|
|
|
}
|
|
|
|
if !ot.IsValid() {
|
|
|
|
// 非法对象数据
|
2023-06-02 16:40:22 +08:00
|
|
|
return StringResult{
|
|
|
|
Value: "",
|
|
|
|
Err: errors.New("data is invalid"),
|
|
|
|
}
|
2023-06-01 18:52:48 +08:00
|
|
|
}
|
2023-06-02 16:40:22 +08:00
|
|
|
return StringResult{
|
|
|
|
Value: string(ot.byteData),
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMapStringAny ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:17 2023/6/2
|
|
|
|
func (ot *ObjectType) ToMapStringAny() ObjectResult {
|
|
|
|
res := ObjectResult{
|
2024-06-08 20:06:35 +08:00
|
|
|
Value: map[string]any{},
|
2023-06-02 16:40:22 +08:00
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
if ot.IsNil() {
|
|
|
|
return res
|
|
|
|
}
|
2023-08-11 13:56:40 +08:00
|
|
|
res.Err = serialize.JSON.UnmarshalWithNumber(ot.byteData, &res.Value)
|
2023-06-02 16:40:22 +08:00
|
|
|
return res
|
2023-06-01 18:52:48 +08:00
|
|
|
}
|
2023-06-02 16:43:18 +08:00
|
|
|
|
|
|
|
// ToStruct ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:41 2023/6/2
|
2024-06-08 20:06:35 +08:00
|
|
|
func (ot *ObjectType) ToStruct(receiver any) error {
|
2023-06-02 16:43:18 +08:00
|
|
|
if nil == receiver {
|
|
|
|
return errors.New("receiver is nil")
|
|
|
|
}
|
|
|
|
if ot.IsNil() {
|
|
|
|
return errors.New("data is nil")
|
|
|
|
}
|
2023-08-11 13:56:40 +08:00
|
|
|
return serialize.JSON.UnmarshalWithNumber(ot.byteData, receiver)
|
2023-06-02 16:43:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToStructIgnoreErr ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:42 2023/6/2
|
2024-06-08 20:06:35 +08:00
|
|
|
func (ot *ObjectType) ToStructIgnoreErr(receiver any) {
|
2023-06-02 16:43:18 +08:00
|
|
|
if nil == receiver {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ot.IsNil() {
|
|
|
|
return
|
|
|
|
}
|
2023-08-11 13:56:40 +08:00
|
|
|
_ = serialize.JSON.UnmarshalWithNumber(ot.byteData, receiver)
|
2023-06-02 16:43:18 +08:00
|
|
|
}
|