GetIdentityFieldValuesMap get identity map from fields
(ctx context.Context, reflectValue reflect.Value, fields []*Field)
| 106 | |
| 107 | // GetIdentityFieldValuesMap get identity map from fields |
| 108 | func GetIdentityFieldValuesMap(ctx context.Context, reflectValue reflect.Value, fields []*Field) (map[string][]reflect.Value, [][]interface{}) { |
| 109 | var ( |
| 110 | results = [][]interface{}{} |
| 111 | dataResults = map[string][]reflect.Value{} |
| 112 | loaded = map[interface{}]bool{} |
| 113 | notZero, zero bool |
| 114 | ) |
| 115 | |
| 116 | if reflectValue.Kind() == reflect.Ptr || |
| 117 | reflectValue.Kind() == reflect.Interface { |
| 118 | reflectValue = reflectValue.Elem() |
| 119 | } |
| 120 | |
| 121 | switch reflectValue.Kind() { |
| 122 | case reflect.Map: |
| 123 | results = [][]interface{}{make([]interface{}, len(fields))} |
| 124 | for idx, field := range fields { |
| 125 | mapValue := reflectValue.MapIndex(reflect.ValueOf(field.DBName)) |
| 126 | if mapValue.IsZero() { |
| 127 | mapValue = reflectValue.MapIndex(reflect.ValueOf(field.Name)) |
| 128 | } |
| 129 | results[0][idx] = mapValue.Interface() |
| 130 | } |
| 131 | |
| 132 | dataResults[utils.ToStringKey(results[0]...)] = []reflect.Value{reflectValue} |
| 133 | case reflect.Struct: |
| 134 | results = [][]interface{}{make([]interface{}, len(fields))} |
| 135 | |
| 136 | for idx, field := range fields { |
| 137 | results[0][idx], zero = field.ValueOf(ctx, reflectValue) |
| 138 | notZero = notZero || !zero |
| 139 | } |
| 140 | |
| 141 | if !notZero { |
| 142 | return nil, nil |
| 143 | } |
| 144 | |
| 145 | dataResults[utils.ToStringKey(results[0]...)] = []reflect.Value{reflectValue} |
| 146 | case reflect.Slice, reflect.Array: |
| 147 | for i := 0; i < reflectValue.Len(); i++ { |
| 148 | elem := reflectValue.Index(i) |
| 149 | elemKey := elem.Interface() |
| 150 | if elem.Kind() != reflect.Ptr && elem.CanAddr() { |
| 151 | elemKey = elem.Addr().Interface() |
| 152 | } |
| 153 | |
| 154 | if _, ok := loaded[elemKey]; ok { |
| 155 | continue |
| 156 | } |
| 157 | loaded[elemKey] = true |
| 158 | |
| 159 | fieldValues := make([]interface{}, len(fields)) |
| 160 | notZero = false |
| 161 | for idx, field := range fields { |
| 162 | fieldValues[idx], zero = field.ValueOf(ctx, elem) |
| 163 | notZero = notZero || !zero |
| 164 | } |
| 165 |
no test coverage detected