2023-08-10 16:58:13 +08:00
|
|
|
// 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
|
2024-06-08 20:06:35 +08:00
|
|
|
func NewStruct(data any) *Struct {
|
2023-08-10 16:58:13 +08:00
|
|
|
s, _ := NewStructWithError(data)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStructWithError ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:17 2023/8/10
|
2024-06-08 20:06:35 +08:00
|
|
|
func NewStructWithError(data any) (*Struct, error) {
|
2023-08-10 16:58:13 +08:00
|
|
|
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 {
|
2024-06-08 20:06:35 +08:00
|
|
|
data any
|
2023-08-10 16:58:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToMap 转为Map
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 16:08 2023/8/10
|
|
|
|
func (s *Struct) ToMap() MapResult {
|
|
|
|
if nil == s.data {
|
|
|
|
return MapResult{
|
2024-06-08 20:06:35 +08:00
|
|
|
Value: EasyMap(map[any]any{}),
|
2023-08-10 16:58:13 +08:00
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MapResult{
|
|
|
|
Value: EasyMapFromStruct(s.data),
|
|
|
|
Err: nil,
|
|
|
|
}
|
|
|
|
}
|