type convert
This commit is contained in:
+41
-31
@@ -6,16 +6,18 @@ const (
|
||||
noDepthLimited = -1
|
||||
)
|
||||
|
||||
type convertFunc func(any) (any, error)
|
||||
|
||||
type options struct {
|
||||
tagName string // 标签名
|
||||
maxDepth int // 最大复制深度
|
||||
ignoreEmpty bool // 复制时忽略空字段
|
||||
caseSensitive bool // 复制时大小写敏感
|
||||
must bool // 只复制具有must标识的字段
|
||||
convertersByName map[string]TypeConverter // 根据名称处理的类型转换器
|
||||
converters []TypeConverter // 根据源和目标类型处理的类型转换器
|
||||
fieldNameMapping map[string]string // 字段名转映射
|
||||
nameConverter func(string) string // 字段名转换器
|
||||
tagName string // 标签名
|
||||
maxDepth int // 最大复制深度
|
||||
ignoreEmpty bool // 复制时忽略空字段
|
||||
caseSensitive bool // 复制时大小写敏感
|
||||
must bool // 只复制具有must标识的字段
|
||||
fieldNameMapping map[string]string // 字段名转映射
|
||||
nameConverter func(string) string // 字段名转换函数
|
||||
converters map[converterPair]convertFunc // 根据源和目标类型处理的类型转换器v
|
||||
convertByName map[string]convertFunc // 根据名称处理的类型转换器
|
||||
}
|
||||
|
||||
type option func(*options)
|
||||
@@ -27,16 +29,18 @@ type TypeConverter struct {
|
||||
Fn func(src any) (dst any, err error)
|
||||
}
|
||||
|
||||
type FieldNameConverter struct {
|
||||
SrcFieldName string
|
||||
DstFieldName string
|
||||
type converterPair struct {
|
||||
Name string
|
||||
SrcType reflect.Type
|
||||
DstType reflect.Type
|
||||
}
|
||||
|
||||
func getOpt(opts ...option) *options {
|
||||
opt := &options{
|
||||
maxDepth: noDepthLimited,
|
||||
tagName: defaultTag,
|
||||
convertersByName: make(map[string]TypeConverter),
|
||||
maxDepth: noDepthLimited,
|
||||
tagName: defaultTag,
|
||||
convertByName: make(map[string]convertFunc),
|
||||
converters: make(map[converterPair]convertFunc),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
@@ -58,68 +62,74 @@ func (opt *options) NameConvert(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (opt options) TypeConvert(value reflect.Value) (reflect.Value, bool) {
|
||||
for _, c := range opt.converters {
|
||||
_ = c
|
||||
}
|
||||
return value, false
|
||||
}
|
||||
|
||||
func (opt *options) ExceedMaxDepth(depth int) bool {
|
||||
return opt.maxDepth != noDepthLimited && depth > opt.maxDepth
|
||||
}
|
||||
|
||||
// WithMaxDepth 设置最大复制深度
|
||||
func WithMaxDepth(depth int) option {
|
||||
return func(o *options) {
|
||||
o.maxDepth = depth
|
||||
}
|
||||
}
|
||||
|
||||
// WithIgnoreEmpty 复制时忽略空字段标识
|
||||
func WithIgnoreEmpty() option {
|
||||
return func(o *options) {
|
||||
o.ignoreEmpty = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithCaseInsensitive 复制时大小写不敏感标识
|
||||
func WithCaseSensitive() option {
|
||||
return func(o *options) {
|
||||
o.caseSensitive = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithMust 添加必须复制标识
|
||||
func WithMust() option {
|
||||
return func(o *options) {
|
||||
o.must = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithNameConvertByName 添加根据名称处理的类型转换器
|
||||
func WithTypeConvertByName(name string, f func(src any) (dst any, err error)) option {
|
||||
return func(o *options) {
|
||||
o.convertersByName[name] = TypeConverter{
|
||||
FieldName: name,
|
||||
Fn: f,
|
||||
o.convertByName[name] = f
|
||||
}
|
||||
}
|
||||
|
||||
// WithTypeConvert 添加根据源和目标类型处理的类型转换器
|
||||
func WithConverters(converters ...TypeConverter) option {
|
||||
return func(o *options) {
|
||||
for i := range converters {
|
||||
pair := converterPair{
|
||||
SrcType: reflect.TypeOf(converters[i].SrcType),
|
||||
DstType: reflect.TypeOf(converters[i].DstType),
|
||||
}
|
||||
|
||||
o.converters[pair] = converters[i].Fn
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithConverters(converters ...TypeConverter) option {
|
||||
return func(o *options) {
|
||||
o.converters = converters
|
||||
}
|
||||
}
|
||||
|
||||
// WithNameMapping 添加字段名映射
|
||||
func WithNameMapping(mappings map[string]string) option {
|
||||
return func(o *options) {
|
||||
o.fieldNameMapping = mappings
|
||||
}
|
||||
}
|
||||
|
||||
// WithNameConverter 添加字段名转换函数
|
||||
func WithNameFn(fn func(string) string) option {
|
||||
return func(o *options) {
|
||||
o.nameConverter = fn
|
||||
}
|
||||
}
|
||||
|
||||
// WithTagName 添加标签名
|
||||
func WithTagName(tagName string) option {
|
||||
return func(o *options) {
|
||||
o.tagName = tagName
|
||||
|
||||
Reference in New Issue
Block a user