Skip to content

Commit f655e1a

Browse files
authored
feat: assign or attr map/struct (#894)
* feat: assign or attr map/struct * feat: impl BeCond
1 parent 25d257b commit f655e1a

File tree

3 files changed

+129
-2
lines changed

3 files changed

+129
-2
lines changed

do.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,9 @@ func (d *DO) Assign(attrs ...field.AssignExpr) Dao {
392392
func (d *DO) attrsValue(attrs []field.AssignExpr) []interface{} {
393393
values := make([]interface{}, 0, len(attrs))
394394
for _, attr := range attrs {
395-
if expr, ok := attr.AssignExpr().(clause.Eq); ok {
395+
if expr, ok := attr.AssignExpr().(field.IValues); ok {
396+
values = append(values, expr.Values())
397+
} else if expr, ok := attr.AssignExpr().(clause.Eq); ok {
396398
values = append(values, expr)
397399
}
398400
}

field/assign_attr.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
}

field/expr.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Expr interface {
2424
Build(clause.Builder)
2525

2626
As(alias string) Expr
27-
ColumnName() sql
27+
IColumnName
2828
BuildColumn(*gorm.Statement, ...BuildOpt) sql
2929
BuildWithArgs(*gorm.Statement) (query sql, args []interface{})
3030
RawExpr() expression
@@ -52,6 +52,10 @@ type OrderExpr interface {
5252

5353
type expression interface{}
5454

55+
type IColumnName interface {
56+
ColumnName() sql
57+
}
58+
5559
type sql string
5660

5761
func (e sql) String() string { return string(e) }

0 commit comments

Comments
 (0)