增加struct的包装类型
This commit is contained in:
69
struct.go
Normal file
69
struct.go
Normal file
@ -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,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user