buildFieldMappings 构建字段映射关系
(fromType, toType reflect.Type)
| 34 | |
| 35 | // buildFieldMappings 构建字段映射关系 |
| 36 | func buildFieldMappings(fromType, toType reflect.Type) []fieldMapping { |
| 37 | mappings := []fieldMapping{} |
| 38 | |
| 39 | for i := 0; i < fromType.NumField(); i++ { |
| 40 | fromField := fromType.Field(i) |
| 41 | |
| 42 | // 尝试通过 mapper tag 或字段名找到对应字段 |
| 43 | fieldName := fromField.Name |
| 44 | toField, found := toType.FieldByName(fieldName) |
| 45 | if !found { |
| 46 | // 尝试通过 mapper tag 查找 |
| 47 | if tag := fromField.Tag.Get("mapper"); tag != "" { |
| 48 | if f, ok := toType.FieldByName(tag); ok { |
| 49 | toField = f |
| 50 | found = true |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if found && fromField.Type == toField.Type { |
| 56 | mappings = append(mappings, fieldMapping{ |
| 57 | fromIndex: []int{i}, |
| 58 | toIndex: toField.Index[0], |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // 存入缓存 |
| 64 | key := cacheKey(fromType, toType) |
| 65 | fieldMappingCache.Store(key, mappings) |
| 66 | |
| 67 | return mappings |
| 68 | } |
| 69 | |
| 70 | // ============ 函数式泛型 Mapper (优化版) ============ |
| 71 |
no test coverage detected
searching dependent graphs…