optimize
This commit is contained in:
@@ -3,6 +3,15 @@
|
||||
Golang object deep copy library
|
||||
|
||||
|
||||
1. 支持深度复制,包括结构体、切片、数组、映射、指针、接口等。
|
||||
2. 支持通过标签(tag)来指定字段复制行为。
|
||||
3. 支持忽略空值。
|
||||
4. 支持字段名称转换(如驼峰转下划线)。
|
||||
5. 支持自定义转换函数。
|
||||
6. 支持时间类型的特殊处理。
|
||||
7. 支持循环引用的检测。
|
||||
8. 支持最大深度限制。
|
||||
|
||||
- map->map
|
||||
- slice->slice
|
||||
- struct->struct
|
||||
|
||||
@@ -275,3 +275,17 @@ func BenchmarkMapCopy(b *testing.B) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkStruct2Map(b *testing.B) {
|
||||
var src = Person{
|
||||
Name: "John",
|
||||
Age: 30,
|
||||
}
|
||||
|
||||
for b.Loop() {
|
||||
var dst map[string]any
|
||||
if err := Copy(&dst, src); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
87
copier_time_test.go
Normal file
87
copier_time_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package copier_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.charlienet.top/go/copier"
|
||||
)
|
||||
|
||||
type MyString string
|
||||
type MyTime time.Time
|
||||
|
||||
func TestTime(t *testing.T) {
|
||||
// 示例1:为内置字符串类型设置时间格式
|
||||
now := time.Now()
|
||||
var str string
|
||||
copier.Copy(&str, now, copier.WithStringTimeFormat("2006-01-02 15:04:05"))
|
||||
fmt.Printf("时间转字符串: %s\n", str)
|
||||
|
||||
// 示例2:为自定义字符串类型设置时间格式
|
||||
var myStr MyString
|
||||
copier.Copy(&myStr, now, copier.WithTimeFormat(MyString(""), "2006/01/02"))
|
||||
fmt.Printf("时间转自定义字符串: %s\n", myStr)
|
||||
|
||||
// 示例3:设置默认时间格式
|
||||
var str1 string
|
||||
copier.Copy(&str1, now, copier.WithTimeFormat(MyString(""), "2006/01/02"))
|
||||
fmt.Printf("默认格式: %s\n", str1)
|
||||
|
||||
// 示例5:字符串转时间
|
||||
timeStr := "2023-12-25 10:30:00"
|
||||
var parsedTime time.Time
|
||||
copier.Copy(&parsedTime, timeStr,
|
||||
copier.WithStringTimeFormat("2006-01-02 15:04:05"),
|
||||
copier.WithTimeFormat(MyString(""), "2006/01/02"),
|
||||
copier.WithDefaultTimeFormat(time.RFC3339))
|
||||
fmt.Printf("字符串转时间: %v\n", parsedTime)
|
||||
}
|
||||
|
||||
func TestTimeStruct(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
CreatedAt *time.Time
|
||||
}
|
||||
|
||||
src := map[string]any{
|
||||
"Name": "John",
|
||||
"CreatedAt": "2023-01-01T10:00:00Z",
|
||||
}
|
||||
|
||||
var user User
|
||||
if err := copier.Copy(&user, src, copier.WithStringTimeFormat(time.RFC3339)); err != nil {
|
||||
fmt.Printf("错误: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("用户: %+v\n", user)
|
||||
b, _ := json.Marshal(user)
|
||||
t.Log(string(b))
|
||||
}
|
||||
|
||||
// user.CreatedAt
|
||||
}
|
||||
|
||||
func TestFieldTag(t *testing.T) {
|
||||
type User struct {
|
||||
Name string
|
||||
CreatedAt time.Time `copier:"format=2006-01-02 15:04:05"`
|
||||
}
|
||||
|
||||
type User2 struct {
|
||||
Name string
|
||||
CreatedAt string
|
||||
}
|
||||
|
||||
var src = User{
|
||||
Name: "John",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
var dst User2
|
||||
if err := copier.Copy(&dst, src); err != nil {
|
||||
fmt.Printf("错误: %v\n", err)
|
||||
} else {
|
||||
fmt.Printf("用户: %+v\n", dst)
|
||||
}
|
||||
}
|
||||
30
optiongs.go
30
optiongs.go
@@ -19,6 +19,7 @@ type options struct {
|
||||
converters map[converterPair]convertFunc // 根据源和目标类型处理的类型转换器v
|
||||
convertByName map[string]convertFunc // 根据名称处理的类型转换器
|
||||
nameConverter func(string) string // 字段名转换函数
|
||||
timeFormats map[reflect.Type]string // 时间格式
|
||||
}
|
||||
|
||||
type option func(*options)
|
||||
@@ -156,6 +157,35 @@ func WithNameFn(fn func(string) string) option {
|
||||
}
|
||||
}
|
||||
|
||||
func WithTimeFormat(targetType any, format string) option {
|
||||
return func(o *options) {
|
||||
if o.timeFormats == nil {
|
||||
o.timeFormats = make(map[reflect.Type]string)
|
||||
}
|
||||
|
||||
t := reflect.TypeOf(targetType)
|
||||
if t.Kind() == reflect.Pointer {
|
||||
t = t.Elem()
|
||||
}
|
||||
|
||||
o.timeFormats[t] = format
|
||||
}
|
||||
}
|
||||
|
||||
func WithStringTimeFormat(format string) option {
|
||||
return WithTimeFormat("", format)
|
||||
}
|
||||
|
||||
func WithDefaultTimeFormat(format string) option {
|
||||
return func(o *options) {
|
||||
if o.timeFormats == nil {
|
||||
o.timeFormats = make(map[reflect.Type]string)
|
||||
}
|
||||
|
||||
o.timeFormats[reflect.TypeOf(nil)] = format
|
||||
}
|
||||
}
|
||||
|
||||
// WithTagName 添加标签名
|
||||
func WithTagName(tagName string) option {
|
||||
return func(o *options) {
|
||||
|
||||
Reference in New Issue
Block a user