|
| 1 | +package field |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "gorm.io/gorm" |
| 8 | + "gorm.io/gorm/utils/tests" |
| 9 | +) |
| 10 | + |
| 11 | +var testDB, _ = gorm.Open(tests.DummyDialector{}, nil) |
| 12 | + |
| 13 | +type IValues interface { |
| 14 | + Values() interface{} |
| 15 | +} |
| 16 | + |
| 17 | +type attrs struct { |
| 18 | + expr |
| 19 | + value interface{} |
| 20 | + db *gorm.DB |
| 21 | + selectFields []IColumnName |
| 22 | + omitFields []IColumnName |
| 23 | +} |
| 24 | + |
| 25 | +func (att *attrs) AssignExpr() expression { |
| 26 | + return att |
| 27 | +} |
| 28 | + |
| 29 | +func (att *attrs) BeCond() interface{} { |
| 30 | + return att.db.Statement.BuildCondition(att.Values()) |
| 31 | +} |
| 32 | + |
| 33 | +func (att *attrs) Values() interface{} { |
| 34 | + if att == nil || att.value == nil { |
| 35 | + return nil |
| 36 | + } |
| 37 | + if len(att.selectFields) == 0 && len(att.omitFields) == 0 { |
| 38 | + return att.value |
| 39 | + } |
| 40 | + values := make(map[string]interface{}) |
| 41 | + if value, ok := att.value.(map[string]interface{}); ok { |
| 42 | + values = value |
| 43 | + } else if value, ok := att.value.(*map[string]interface{}); ok { |
| 44 | + values = *value |
| 45 | + } else { |
| 46 | + reflectValue := reflect.Indirect(reflect.ValueOf(att.value)) |
| 47 | + for reflectValue.Kind() == reflect.Ptr || reflectValue.Kind() == reflect.Interface { |
| 48 | + reflectValue = reflect.Indirect(reflectValue) |
| 49 | + } |
| 50 | + switch reflectValue.Kind() { |
| 51 | + case reflect.Struct: |
| 52 | + if err := att.db.Statement.Parse(att.value); err == nil { |
| 53 | + ignoreZero := len(att.selectFields) == 0 |
| 54 | + for _, f := range att.db.Statement.Schema.Fields { |
| 55 | + if f.Readable { |
| 56 | + if v, isZero := f.ValueOf(att.db.Statement.Context, reflectValue); !isZero || !ignoreZero { |
| 57 | + values[f.DBName] = v |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + if len(att.selectFields) > 0 { |
| 65 | + fm, all := toFieldMap(att.selectFields) |
| 66 | + if all { |
| 67 | + return values |
| 68 | + } |
| 69 | + tvs := make(map[string]interface{}, len(fm)) |
| 70 | + for fn, vl := range values { |
| 71 | + if fm[fn] { |
| 72 | + tvs[fn] = vl |
| 73 | + } |
| 74 | + } |
| 75 | + return tvs |
| 76 | + } |
| 77 | + fm, all := toFieldMap(att.omitFields) |
| 78 | + if all { |
| 79 | + return map[string]interface{}{} |
| 80 | + } |
| 81 | + for fn, _ := range fm { |
| 82 | + delete(values, fn) |
| 83 | + } |
| 84 | + return values |
| 85 | +} |
| 86 | + |
| 87 | +func toFieldMap(fields []IColumnName) (fieldsMap map[string]bool, all bool) { |
| 88 | + fieldsMap = make(map[string]bool, len(fields)) |
| 89 | + for _, f := range fields { |
| 90 | + if strings.HasSuffix(string(f.ColumnName()), "*") { |
| 91 | + all = true |
| 92 | + return |
| 93 | + } |
| 94 | + fieldsMap[string(f.ColumnName())] = true |
| 95 | + } |
| 96 | + return |
| 97 | +} |
| 98 | + |
| 99 | +func (att *attrs) Select(fields ...IColumnName) *attrs { |
| 100 | + if att == nil || att.db == nil { |
| 101 | + return att |
| 102 | + } |
| 103 | + att.selectFields = fields |
| 104 | + return att |
| 105 | +} |
| 106 | + |
| 107 | +func (att *attrs) Omit(fields ...IColumnName) *attrs { |
| 108 | + if att == nil || att.db == nil { |
| 109 | + return att |
| 110 | + } |
| 111 | + att.omitFields = fields |
| 112 | + return att |
| 113 | +} |
| 114 | + |
| 115 | +func Attrs(attr interface{}) *attrs { |
| 116 | + res := &attrs{db: testDB.Debug()} |
| 117 | + if attr != nil { |
| 118 | + res.value = attr |
| 119 | + } |
| 120 | + return res |
| 121 | +} |
0 commit comments