MCPcopy Index your code
hub / github.com/devfeel/mapper / buildFieldMappings

Function buildFieldMappings

mapper_func.go:36–68  ·  view source on GitHub ↗

buildFieldMappings 构建字段映射关系

(fromType, toType reflect.Type)

Source from the content-addressed store, hash-verified

34
35// buildFieldMappings 构建字段映射关系
36func 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

Callers 3

MapDirectFunction · 0.85
MapDirectSliceFunction · 0.85
SafeMapDirectFunction · 0.85

Calls 2

cacheKeyFunction · 0.85
GetMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…