301 lines
5.6 KiB
Go
301 lines
5.6 KiB
Go
package copier
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCopyMap(t *testing.T) {
|
|
src := map[string]any{
|
|
"name": "Alice",
|
|
"age": 25,
|
|
"height": 165.5,
|
|
"is_student": true,
|
|
"birthday": "1998-05-15T06:00:00Z",
|
|
"tags": []any{"developer", "golang", "backend"},
|
|
"scores": []any{95, 88, 92},
|
|
"address": map[string]any{
|
|
"street": "123 Main St",
|
|
"city": "Beijing",
|
|
"country": "China",
|
|
},
|
|
"metadata": map[string]any{
|
|
"department": "Engineering",
|
|
"level": 3,
|
|
"projects": []any{"projectA", "projectB"},
|
|
},
|
|
"ptr_field": "pointer value",
|
|
}
|
|
|
|
dst := map[string]any{}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestMapAnyCopy(t *testing.T) {
|
|
src := map[string]any{
|
|
"department": "Engineering",
|
|
"level": 3,
|
|
}
|
|
|
|
dst := map[string]any{}
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(src, dst)
|
|
}
|
|
|
|
func TestIntMapCopy(t *testing.T) {
|
|
src := map[int]string{
|
|
1: "department",
|
|
3: "level",
|
|
}
|
|
|
|
dst := map[int]any{}
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
fmt.Println(src, dst)
|
|
}
|
|
|
|
func TestDiffStructMapCopy(t *testing.T) {
|
|
type person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
type user struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
src := map[string]*person{
|
|
"John": {
|
|
Name: "John",
|
|
Age: 30,
|
|
},
|
|
}
|
|
|
|
dst := map[string]*user{}
|
|
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fmt.Println(src, dst)
|
|
}
|
|
|
|
func TestStructToMapCopy(t *testing.T) {
|
|
type person struct {
|
|
Name string
|
|
Age int
|
|
Birthday time.Time
|
|
Address *Address
|
|
}
|
|
|
|
src := person{
|
|
Name: "John",
|
|
Age: 30,
|
|
Birthday: time.Now(),
|
|
Address: &Address{
|
|
City: "Beijing",
|
|
},
|
|
}
|
|
|
|
dst := map[string]any{}
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
fmt.Println(src, dst)
|
|
}
|
|
|
|
func TestAnonymousStructToMapCopy(t *testing.T) {
|
|
type Person struct {
|
|
Name string
|
|
Age int
|
|
}
|
|
|
|
type person2 struct {
|
|
*Person
|
|
Address *Address
|
|
}
|
|
|
|
// src := person2{
|
|
// person: &person{
|
|
// Name: "John",
|
|
// Age: 30,
|
|
// },
|
|
// Address: &Address{
|
|
// City: "Beijing",
|
|
// },
|
|
// }
|
|
|
|
// dst := map[string]any{}
|
|
// if err := Copy(&dst, src); err != nil {
|
|
// t.Fatal(err)
|
|
// }
|
|
|
|
t.Run("test to anonymous struct", func(t *testing.T) {
|
|
src := map[string]any{
|
|
"Name": "John",
|
|
"Age": 30,
|
|
"Address": map[string]any{
|
|
"City": "Beijing",
|
|
},
|
|
}
|
|
|
|
dst := person2{}
|
|
if err := Copy(&dst, src, WithCaseSensitive()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
b, _ := json.Marshal(dst)
|
|
t.Log(string(b))
|
|
})
|
|
|
|
}
|
|
|
|
func TestMapToStructCopy(t *testing.T) {
|
|
type person struct {
|
|
Name string
|
|
Age int
|
|
Address *Address
|
|
}
|
|
|
|
src := map[string]any{
|
|
"Name": "John",
|
|
"Age": 30,
|
|
"Address": map[string]any{
|
|
"City": "Beijing",
|
|
},
|
|
}
|
|
|
|
dst := person{}
|
|
if err := Copy(&dst, src, WithCaseSensitive()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
t.Log(dst)
|
|
}
|
|
|
|
func TestMixMapToStruct(t *testing.T) {
|
|
type Address struct {
|
|
Street string `json:"street"`
|
|
City string `json:"city"`
|
|
Country string `json:"country"`
|
|
}
|
|
|
|
type Person struct {
|
|
Name string `json:"name"`
|
|
Age int `json:"age"`
|
|
Height float64 `json:"height"`
|
|
IsStudent bool `json:"is_student" copier:"-"`
|
|
Birthday time.Time `json:"birthday"`
|
|
Tags []string `json:"tags"`
|
|
Scores []int `json:"scores"`
|
|
Address Address `json:"address"`
|
|
Metadata map[string]any `json:"metadata"`
|
|
PtrField *string `json:"ptr_field"`
|
|
}
|
|
|
|
src := map[string]any{
|
|
"name": "Alice",
|
|
"age": 25,
|
|
"height": 165.5,
|
|
"is_student": true,
|
|
"birthday": "1998-05-15T06:00:00Z",
|
|
"tags": []any{"developer", "golang", "backend"},
|
|
"scores": []any{95, 88, 92},
|
|
"address": map[string]any{
|
|
"street": "123 Main St",
|
|
"city": "Beijing",
|
|
"country": "China",
|
|
},
|
|
"metadata": map[string]any{
|
|
"department": "Engineering",
|
|
"level": 3,
|
|
"projects": []any{"projectA", "projectB"},
|
|
},
|
|
"ptr_field": "pointer value",
|
|
}
|
|
|
|
var dst Person
|
|
if err := Copy(&dst, src); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
b, _ := json.MarshalIndent(dst, "", " ")
|
|
|
|
t.Log(dst)
|
|
t.Log(string(b))
|
|
}
|
|
|
|
func BenchmarkMapCopy(b *testing.B) {
|
|
type Address struct {
|
|
Street string `json:"street"`
|
|
City string `json:"city"`
|
|
Country string `json:"country"`
|
|
}
|
|
|
|
type Person struct {
|
|
Name string `json:"name"`
|
|
Age int `json:"age"`
|
|
Height float64 `json:"height"`
|
|
IsStudent bool `json:"is_student"`
|
|
Birthday time.Time `json:"birthday"`
|
|
Tags []string `json:"tags"`
|
|
Scores []int `json:"scores"`
|
|
Address Address `json:"address"`
|
|
Metadata map[string]any `json:"metadata"`
|
|
PtrField *string `json:"ptr_field"`
|
|
}
|
|
|
|
src := map[string]any{
|
|
"name": "Alice",
|
|
"age": 25,
|
|
"height": 165.5,
|
|
"is_student": true,
|
|
"birthday": "1998-05-15T00:00:00Z",
|
|
"tags": []any{"developer", "golang", "backend"},
|
|
"scores": []any{95, 88, 92},
|
|
"address": map[string]any{
|
|
"street": "123 Main St",
|
|
"city": "Beijing",
|
|
"country": "China",
|
|
},
|
|
"metadata": map[string]any{
|
|
"department": "Engineering",
|
|
"level": 3,
|
|
"projects": []any{"projectA", "projectB"},
|
|
},
|
|
"ptr_field": "pointer value",
|
|
}
|
|
|
|
for b.Loop() {
|
|
var dst Person
|
|
if err := Copy(&dst, src); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|