|
| 1 | +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. |
| 2 | +// |
| 3 | +// This Source Code Form is subject to the terms of the MIT License. |
| 4 | +// If a copy of the MIT was not distributed with this file, |
| 5 | +// You can obtain one at https://github.com/gogf/gf. |
| 6 | + |
| 7 | +package gdb |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "reflect" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/gogf/gf/v2/container/gmap" |
| 15 | + "github.com/gogf/gf/v2/container/gtype" |
| 16 | +) |
| 17 | + |
| 18 | +var typeFieldCacheChecker = func(v *fieldCacheItem) bool { return v == nil } |
| 19 | + |
| 20 | +type typeFieldCacheManager struct { |
| 21 | + cache *gmap.KVMap[typeFieldCacheKey, *fieldCacheItem] |
| 22 | +} |
| 23 | + |
| 24 | +type typeFieldCacheKey struct { |
| 25 | + typ reflect.Type |
| 26 | + bindToAttrName string |
| 27 | + relationAttrName string |
| 28 | +} |
| 29 | + |
| 30 | +func newTypeFieldCacheManager() *typeFieldCacheManager { |
| 31 | + return &typeFieldCacheManager{ |
| 32 | + cache: gmap.NewKVMapWithChecker[typeFieldCacheKey, *fieldCacheItem](typeFieldCacheChecker, true), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +var fieldCacheInstance = newTypeFieldCacheManager() |
| 37 | + |
| 38 | +type fieldCacheItem struct { |
| 39 | + // Deterministic field index (can be safely cached) |
| 40 | + bindToAttrIndex int // Field index of bound attribute (e.g. UserDetail), -1 for embedded fields |
| 41 | + bindToAttrIndexPath []int // Full index path for embedded fields (e.g. []int{1, 2}) |
| 42 | + relationAttrIndex int // Field index of relation attribute (e.g. User, -1 means none) |
| 43 | + relationAttrIndexPath []int // Full index path for embedded relation attribute |
| 44 | + isPointerElem bool // Whether array element is pointer type |
| 45 | + bindToAttrKind reflect.Kind // Type of bound attribute |
| 46 | + |
| 47 | + // Field name mapping (supports case-insensitive lookup) |
| 48 | + fieldNameMap map[string]string // lowercase -> OriginalName |
| 49 | + fieldIndexMap map[string]int // FieldName -> Index |
| 50 | +} |
| 51 | + |
| 52 | +func (m *typeFieldCacheManager) getOrSet( |
| 53 | + arrayItemType reflect.Type, |
| 54 | + bindToAttrName string, |
| 55 | + relationAttrName string, |
| 56 | +) (*fieldCacheItem, error) { |
| 57 | + e := gtype.New(nil, true) |
| 58 | + cacheKey := typeFieldCacheKey{ |
| 59 | + typ: arrayItemType, |
| 60 | + bindToAttrName: bindToAttrName, |
| 61 | + relationAttrName: relationAttrName, |
| 62 | + } |
| 63 | + v := m.cache.GetOrSetFuncLock(cacheKey, func() *fieldCacheItem { |
| 64 | + item, err := m.buildCacheItem(arrayItemType, bindToAttrName, relationAttrName) |
| 65 | + if err != nil { |
| 66 | + e.Set(err) |
| 67 | + return nil |
| 68 | + } |
| 69 | + return item |
| 70 | + }) |
| 71 | + if e.Val() != nil { |
| 72 | + return nil, e.Val().(error) |
| 73 | + } |
| 74 | + return v, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (m *typeFieldCacheManager) buildCacheItem( |
| 78 | + arrayItemType reflect.Type, |
| 79 | + bindToAttrName string, |
| 80 | + relationAttrName string, |
| 81 | +) (*fieldCacheItem, error) { |
| 82 | + structType := arrayItemType |
| 83 | + isPointerElem := false |
| 84 | + if structType.Kind() == reflect.Pointer { |
| 85 | + structType = structType.Elem() |
| 86 | + isPointerElem = true |
| 87 | + } |
| 88 | + |
| 89 | + if structType.Kind() != reflect.Struct { |
| 90 | + return nil, fmt.Errorf("arrayItemType must be struct or pointer to struct, got: %s", arrayItemType.Kind()) |
| 91 | + } |
| 92 | + |
| 93 | + numField := structType.NumField() |
| 94 | + cache := &fieldCacheItem{ |
| 95 | + relationAttrIndex: -1, |
| 96 | + isPointerElem: isPointerElem, |
| 97 | + fieldNameMap: make(map[string]string, numField), |
| 98 | + fieldIndexMap: make(map[string]int, numField), |
| 99 | + } |
| 100 | + |
| 101 | + // Iterate all fields, build field mapping |
| 102 | + for i := 0; i < numField; i++ { |
| 103 | + field := structType.Field(i) |
| 104 | + fieldName := field.Name |
| 105 | + |
| 106 | + cache.fieldIndexMap[fieldName] = i |
| 107 | + cache.fieldNameMap[strings.ToLower(fieldName)] = fieldName |
| 108 | + } |
| 109 | + |
| 110 | + // Find bindToAttrName field index |
| 111 | + cache.bindToAttrIndex, cache.bindToAttrIndexPath = m.findFieldIndex(structType, cache.fieldIndexMap, cache.fieldNameMap, bindToAttrName) |
| 112 | + if cache.bindToAttrIndex < 0 && len(cache.bindToAttrIndexPath) == 0 { |
| 113 | + return nil, fmt.Errorf(`field "%s" not found in type %s`, bindToAttrName, arrayItemType.String()) |
| 114 | + } |
| 115 | + |
| 116 | + // Set bindToAttrKind based on the found field |
| 117 | + var bindToField reflect.StructField |
| 118 | + if cache.bindToAttrIndex >= 0 { |
| 119 | + bindToField = structType.Field(cache.bindToAttrIndex) |
| 120 | + } else { |
| 121 | + bindToField = structType.FieldByIndex(cache.bindToAttrIndexPath) |
| 122 | + } |
| 123 | + cache.bindToAttrKind = bindToField.Type.Kind() |
| 124 | + |
| 125 | + // Find relationAttrName field index (optional) |
| 126 | + if relationAttrName != "" { |
| 127 | + cache.relationAttrIndex, cache.relationAttrIndexPath = m.findFieldIndex(structType, cache.fieldIndexMap, cache.fieldNameMap, relationAttrName) |
| 128 | + // Note: if still not found, keep -1, indicating that arrayElemValue itself should be used |
| 129 | + } |
| 130 | + |
| 131 | + return cache, nil |
| 132 | +} |
| 133 | + |
| 134 | +// findFieldIndex finds the field index or index path for a given field name |
| 135 | +// Returns: index (>=0 for direct field, -1 for embedded field), indexPath (nil for direct field, actual path for embedded field) |
| 136 | +func (m *typeFieldCacheManager) findFieldIndex( |
| 137 | + structType reflect.Type, |
| 138 | + fieldIndexMap map[string]int, |
| 139 | + fieldNameMap map[string]string, |
| 140 | + fieldName string, |
| 141 | +) (int, []int) { |
| 142 | + // First, try direct lookup |
| 143 | + if idx, ok := fieldIndexMap[fieldName]; ok { |
| 144 | + return idx, nil |
| 145 | + } |
| 146 | + |
| 147 | + // Second, try case-insensitive lookup |
| 148 | + lowerFieldName := strings.ToLower(fieldName) |
| 149 | + if originalName, ok := fieldNameMap[lowerFieldName]; ok { |
| 150 | + return fieldIndexMap[originalName], nil |
| 151 | + } |
| 152 | + |
| 153 | + // Third, try using FieldByName for embedded fields |
| 154 | + field, ok := structType.FieldByName(fieldName) |
| 155 | + if !ok { |
| 156 | + return -1, nil // Not found |
| 157 | + } |
| 158 | + |
| 159 | + // For embedded fields, field.Index contains the full path |
| 160 | + if len(field.Index) == 1 { |
| 161 | + // Direct field (shouldn't normally happen here since we already checked fieldIndexMap) |
| 162 | + return field.Index[0], nil |
| 163 | + } else { |
| 164 | + // Embedded field - return the full index path |
| 165 | + return -1, field.Index |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// clear clears all cache (used for testing or hot updates) |
| 170 | +func (m *typeFieldCacheManager) clear() { |
| 171 | + m.cache.Clear() |
| 172 | +} |
| 173 | + |
| 174 | +// ClearFieldCache clears field cache (for external calls) |
| 175 | +// Used for testing or application hot update scenarios |
| 176 | +func ClearFieldCache() { |
| 177 | + fieldCacheInstance.clear() |
| 178 | +} |
0 commit comments