Skip to content

Commit db655c8

Browse files
author
OpenClaw
committed
feat: add generic mapper examples
1 parent 77cbfe0 commit db655c8

2 files changed

Lines changed: 231 additions & 0 deletions

File tree

example/generic/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Mapper 泛型示例
2+
3+
本目录展示如何使用 mapper 的泛型功能。
4+
5+
## 泛型 API
6+
7+
### 1. Map[From, To] - 同构类型映射
8+
9+
```go
10+
// 同类型之间复制
11+
user1 := &User{ID: 1, Name: "张三"}
12+
userCopy := &User{}
13+
mapper.Map(user1, userCopy)
14+
```
15+
16+
### 2. MapTo[To] - 异构类型映射
17+
18+
```go
19+
// 不同类型之间映射
20+
user := &User{ID: 1, Name: "张三"}
21+
userDTO := &UserDTO{}
22+
mapper.MapTo(user, userDTO)
23+
```
24+
25+
### 3. MapSliceGeneric - Slice 批量映射
26+
27+
```go
28+
// 批量映射
29+
users := []User{
30+
{ID: 1, Name: "用户1"},
31+
{ID: 2, Name: "用户2"},
32+
}
33+
var userDTOs []UserDTO
34+
mapper.MapSliceGeneric(users, &userDTOs)
35+
```
36+
37+
### 4. MapToSliceGeneric - Map 转 Slice
38+
39+
```go
40+
// Map 数据转换为 Slice
41+
userMap := map[string]any{
42+
"user1": map[string]any{"id": 1, "name": "张三"},
43+
"user2": map[string]any{"id": 2, "name": "李四"},
44+
}
45+
var userDTOs []UserDTO
46+
mapper.MapToSliceGeneric(userMap, &userDTOs)
47+
```
48+
49+
### 5. MapperGeneric 实例
50+
51+
```go
52+
// 创建泛型 Mapper 实例
53+
mp := mapper.NewMapperGeneric()
54+
mp.MapTo(target, source)
55+
```
56+
57+
## 运行示例
58+
59+
```bash
60+
cd example/generic
61+
go run main.go
62+
```
63+
64+
## 输出示例
65+
66+
```
67+
=== Mapper 泛型示例 ===
68+
69+
1. 同构类型映射 (Map[From, To])
70+
原对象: &{1 张三 zhangsan@example.com 28 1704067200}
71+
复制后: &{1 张三 zhangsan@example.com 28 1704067200}
72+
73+
2. 异构类型映射 (MapTo[To])
74+
User: &{2 李四 lisi@example.com 32 1704153600}
75+
UserDTO: &{2 李四 lisi@example.com 32 1704153600}
76+
77+
3. Slice 批量映射 (MapSliceGeneric)
78+
源数量: 3, 目标数量: 3
79+
[0] {1 用户1 user1@example.com 20 1704067200}
80+
[1] {2 用户2 user2@example.com 25 1704153600}
81+
[2] {3 用户3 user3@example.com 30 1704240000}
82+
83+
4. Map 转 Slice (MapToSliceGeneric)
84+
源数量: 2, 目标数量: 2
85+
[0] {1 王五 wangwu@example.com 35 1704067200}
86+
[1] {2 赵六 zhaoliu@example.com 40 1704153600}
87+
88+
5. 使用 MapperGeneric 实例
89+
User: &{5 孙七 sunqi@example.com 45 1704326400}
90+
UserVO: &{5 孙七 sunqi@example.com 45}
91+
92+
=== 示例完成 ===
93+
```
94+
95+
## 优势
96+
97+
- **类型安全**: 编译时类型检查
98+
- **代码简洁**: 无需类型断言
99+
- **性能更好**: 减少运行时反射开销
100+
- **完全兼容**: 与现有 API 共存

example/generic/main.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/devfeel/mapper"
6+
)
7+
8+
// 定义测试用的结构体
9+
type (
10+
// 源结构体 - 用户信息
11+
User struct {
12+
ID int64 `mapper:"id"`
13+
Name string `mapper:"name"`
14+
Email string `mapper:"email"`
15+
Age int `mapper:"age"`
16+
CreatedAt int64 `mapper:"created_at"`
17+
}
18+
19+
// 目标结构体 - 用户DTO
20+
UserDTO struct {
21+
ID int64 `json:"id"`
22+
Name string `json:"name"`
23+
Email string `json:"email"`
24+
Age int `json:"age"`
25+
CreatedAt int64 `json:"created_at"`
26+
}
27+
28+
// 目标结构体 - 用户VO (不同字段)
29+
UserVO struct {
30+
UserID int64 `json:"user_id"`
31+
UserName string `json:"user_name"`
32+
Email string `json:"email"`
33+
Age int `json:"age"`
34+
}
35+
)
36+
37+
func main() {
38+
fmt.Println("=== Mapper 泛型示例 ===")
39+
fmt.Println()
40+
41+
// 示例1: 使用泛型 Map 进行同构类型映射
42+
fmt.Println("1. 同构类型映射 (Map[From, To])")
43+
user1 := &User{
44+
ID: 1,
45+
Name: "张三",
46+
Email: "zhangsan@example.com",
47+
Age: 28,
48+
CreatedAt: 1704067200,
49+
}
50+
userCopy := &User{}
51+
err := mapper.Map(user1, userCopy)
52+
if err != nil {
53+
fmt.Printf(" 映射失败: %v\n", err)
54+
} else {
55+
fmt.Printf(" 原对象: %+v\n", user1)
56+
fmt.Printf(" 复制后: %+v\n", userCopy)
57+
}
58+
fmt.Println()
59+
60+
// 示例2: 使用泛型 MapTo 进行异构类型映射
61+
fmt.Println("2. 异构类型映射 (MapTo[To])")
62+
user2 := &User{
63+
ID: 2,
64+
Name: "李四",
65+
Email: "lisi@example.com",
66+
Age: 32,
67+
CreatedAt: 1704153600,
68+
}
69+
userDTO := &UserDTO{}
70+
err = mapper.MapTo(user2, userDTO)
71+
if err != nil {
72+
fmt.Printf(" 映射失败: %v\n", err)
73+
} else {
74+
fmt.Printf(" User: %+v\n", user2)
75+
fmt.Printf(" UserDTO: %+v\n", userDTO)
76+
}
77+
fmt.Println()
78+
79+
// 示例3: 使用泛型 MapSliceGeneric 进行 Slice 映射
80+
fmt.Println("3. Slice 批量映射 (MapSliceGeneric)")
81+
users := []User{
82+
{ID: 1, Name: "用户1", Email: "user1@example.com", Age: 20, CreatedAt: 1704067200},
83+
{ID: 2, Name: "用户2", Email: "user2@example.com", Age: 25, CreatedAt: 1704153600},
84+
{ID: 3, Name: "用户3", Email: "user3@example.com", Age: 30, CreatedAt: 1704240000},
85+
}
86+
var userDTOs []UserDTO
87+
err = mapper.MapSliceGeneric(users, &userDTOs)
88+
if err != nil {
89+
fmt.Printf(" 映射失败: %v\n", err)
90+
} else {
91+
fmt.Printf(" 源数量: %d, 目标数量: %d\n", len(users), len(userDTOs))
92+
for i, u := range userDTOs {
93+
fmt.Printf(" [%d] %+v\n", i, u)
94+
}
95+
}
96+
fmt.Println()
97+
98+
// 示例4: 使用泛型 MapToSliceGeneric 进行 Map 转 Slice
99+
fmt.Println("4. Map 转 Slice (MapToSliceGeneric)")
100+
userMap := map[string]any{
101+
"user1": map[string]any{"id": int64(1), "name": "王五", "email": "wangwu@example.com", "age": int(35), "created_at": int64(1704067200)},
102+
"user2": map[string]any{"id": int64(2), "name": "赵六", "email": "zhaoliu@example.com", "age": int(40), "created_at": int64(1704153600)},
103+
}
104+
var userDTOsFromMap []UserDTO
105+
err = mapper.MapToSliceGeneric(userMap, &userDTOsFromMap)
106+
if err != nil {
107+
fmt.Printf(" 映射失败: %v\n", err)
108+
} else {
109+
fmt.Printf(" 源数量: %d, 目标数量: %d\n", len(userMap), len(userDTOsFromMap))
110+
for i, u := range userDTOsFromMap {
111+
fmt.Printf(" [%d] %+v\n", i, u)
112+
}
113+
}
114+
fmt.Println()
115+
116+
// 示例5: 使用 MapperGeneric 实例
117+
fmt.Println("5. 使用 MapperGeneric 实例")
118+
mp := mapper.NewMapperGeneric()
119+
user5 := &User{ID: 5, Name: "孙七", Email: "sunqi@example.com", Age: 45, CreatedAt: 1704326400}
120+
userVO := &UserVO{}
121+
err = mp.MapTo(userVO, user5)
122+
if err != nil {
123+
fmt.Printf(" 映射失败: %v\n", err)
124+
} else {
125+
fmt.Printf(" User: %+v\n", user5)
126+
fmt.Printf(" UserVO: %+v\n", userVO)
127+
}
128+
fmt.Println()
129+
130+
fmt.Println("=== 示例完成 ===")
131+
}

0 commit comments

Comments
 (0)