You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool/nmapper/mapper.go

63 lines
1.4 KiB
Go

package nmapper
import "git.noahlan.cn/noahlan/ntool/nmapper/wrapper"
var defaultMapper *MapperObject
type Option func(m *MapperObject)
func init() {
defaultMapper = NewMapper()
}
// SetCustomTag 为默认的Mapper设置自定义tag
func SetCustomTag(tag string) {
defaultMapper.customTagName = tag
}
// SetTypeCheck 为默认的Mapper设置开启类型检查
func SetTypeCheck(enabled bool) {
defaultMapper.enabledTypeChecking = enabled
}
// SetTypeWrapper 为默认的Mapper设置类型转换器
func SetTypeWrapper(wrappers ...wrapper.TypeWrapper) {
defaultMapper.SetTypeWrapper(wrappers...)
}
// WithCustomTag 设置自定义 tag
func WithCustomTag(tag string) Option {
return func(m *MapperObject) {
m.customTagName = tag
}
}
// WithTypeCheck 开启类型安全
func WithTypeCheck() Option {
return func(m *MapperObject) {
m.enabledTypeChecking = true
}
}
// WithTypeWrapper 设置类型转换器
func WithTypeWrapper(wrappers ...wrapper.TypeWrapper) Option {
return func(m *MapperObject) {
m.SetTypeWrapper(wrappers...)
}
}
func Mapper(dst any, src any) error {
return defaultMapper.Mapper(dst, src)
}
func ParseMap(dst any, src map[string]any) error {
return defaultMapper.ParseMap(dst, src)
}
func ParseStrMap(dst any, src map[string]string) error {
defaultMapper.SetTypeWrapper(wrapper.StrWrapper()...)
defer defaultMapper.SetTypeWrapper()
return defaultMapper.ParseStrMap(dst, src)
}