default options

This commit is contained in:
2025-09-30 10:24:35 +08:00
parent 715033f650
commit 2b90888901

View File

@@ -15,6 +15,7 @@ type options struct {
caseSensitive bool // 复制时大小写敏感 caseSensitive bool // 复制时大小写敏感
must bool // 只复制具有must标识的字段 must bool // 只复制具有must标识的字段
detectCircularRefs bool // 检测循环引用 detectCircularRefs bool // 检测循环引用
skipFields []string // 忽略字段
fieldNameMapping map[string]string // 字段名转映射 fieldNameMapping map[string]string // 字段名转映射
converters map[converterPair]convertFunc // 根据源和目标类型处理的类型转换器v converters map[converterPair]convertFunc // 根据源和目标类型处理的类型转换器v
convertByName map[string]convertFunc // 根据名称处理的类型转换器 convertByName map[string]convertFunc // 根据名称处理的类型转换器
@@ -22,6 +23,13 @@ type options struct {
timeFormats map[reflect.Type]string // 时间格式 timeFormats map[reflect.Type]string // 时间格式
} }
var (
defaultOptions = &options{
tagName: defaultTag,
maxDepth: noDepthLimited,
}
)
type option func(*options) type option func(*options)
type TypeConverter struct { type TypeConverter struct {
@@ -38,6 +46,10 @@ type converterPair struct {
} }
func getOpt(opts ...option) *options { func getOpt(opts ...option) *options {
if len(opts) == 0 {
return defaultOptions
}
opt := &options{ opt := &options{
maxDepth: noDepthLimited, maxDepth: noDepthLimited,
tagName: defaultTag, tagName: defaultTag,
@@ -186,6 +198,12 @@ func WithDefaultTimeFormat(format string) option {
} }
} }
func WitSkipFields(fields ...string) option {
return func(o *options) {
o.skipFields = fields
}
}
// WithTagName 添加标签名 // WithTagName 添加标签名
func WithTagName(tagName string) option { func WithTagName(tagName string) option {
return func(o *options) { return func(o *options) {