MapDirectSlice 批量映射 使用示例: dtos := mapper.MapDirectSlice[User, UserDTO](users)
(from []From)
| 132 | // MapDirectSlice 批量映射 |
| 133 | // 使用示例: dtos := mapper.MapDirectSlice[User, UserDTO](users) |
| 134 | func MapDirectSlice[From, To any](from []From) []To { |
| 135 | if from == nil { |
| 136 | return nil |
| 137 | } |
| 138 | |
| 139 | // 尝试预获取映射关系以优化批量操作 |
| 140 | fromType := reflect.TypeOf((*From)(nil)).Elem() |
| 141 | toType := reflect.TypeOf((*To)(nil)).Elem() |
| 142 | |
| 143 | _, hasCache := getFieldMappings(fromType, toType) |
| 144 | if !hasCache { |
| 145 | buildFieldMappings(fromType, toType) |
| 146 | } |
| 147 | |
| 148 | result := make([]To, len(from)) |
| 149 | for i, v := range from { |
| 150 | result[i] = MapDirect[From, To](v) |
| 151 | } |
| 152 | return result |
| 153 | } |
| 154 | |
| 155 | // MapDirectPtrSlice 指针切片映射 |
| 156 | // 使用示例: dtos := mapper.MapDirectPtrSlice[User, UserDTO](&users) |
nothing calls this directly
no test coverage detected
searching dependent graphs…