feat: 相关操作升级为泛型实现
This commit is contained in:
46
op_struct/struct.go
Normal file
46
op_struct/struct.go
Normal file
@ -0,0 +1,46 @@
|
||||
// Package op_struct ...
|
||||
//
|
||||
// Description : wrapper ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2023-08-10 16:05
|
||||
package op_struct
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper/op_map"
|
||||
)
|
||||
|
||||
// NewStruct 包装的数据类型
|
||||
func NewStruct(data any) *Struct {
|
||||
s, _ := NewStructWithError(data)
|
||||
return s
|
||||
}
|
||||
|
||||
// NewStructWithError ...
|
||||
func NewStructWithError(data any) (*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 结构体类型
|
||||
type Struct struct {
|
||||
data any
|
||||
}
|
||||
|
||||
// ToMap 转为Map
|
||||
func ToMap(s *Struct) op_map.Map {
|
||||
return op_map.EasyMap(s.data)
|
||||
}
|
Reference in New Issue
Block a user