Skip to content

Commit b10c9cf

Browse files
author
Anthony Regeda
committed
added incr func for update
1 parent cbb8c6a commit b10c9cf

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

update.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,51 @@ type Changer struct {
1212
values Values
1313
}
1414

15+
// Modifier changes a value for update statement
16+
type Modifier func(v interface{}) interface{}
17+
18+
// IncrInt returns a modifier for int/int8/int16/int32/int64 increment
19+
func IncrInt(to interface{}) Modifier {
20+
return func(v interface{}) interface{} {
21+
if x, ok := v.(int); ok {
22+
return x + to.(int)
23+
}
24+
if x, ok := v.(int8); ok {
25+
return x + to.(int8)
26+
}
27+
if x, ok := v.(int16); ok {
28+
return x + to.(int16)
29+
}
30+
if x, ok := v.(int32); ok {
31+
return x + to.(int32)
32+
}
33+
if x, ok := v.(int64); ok {
34+
return x + to.(int64)
35+
}
36+
panic("y/update: unknown int type for increment.")
37+
}
38+
}
39+
40+
// IncrFloat returns a modifier for float32/float64 increment
41+
func IncrFloat(to interface{}) Modifier {
42+
return func(v interface{}) interface{} {
43+
if x, ok := v.(float32); ok {
44+
return x + to.(float32)
45+
}
46+
if x, ok := v.(float64); ok {
47+
return x + to.(float64)
48+
}
49+
panic("y/update: unknown float type for increment.")
50+
}
51+
}
52+
1553
func (c *Changer) modify() sq.Eq {
1654
modified := sq.Eq{}
1755
for name, val := range c.values {
1856
f := c.proxy.Field(name)
57+
if modifier, ok := val.(Modifier); ok {
58+
val = modifier(f.Interface())
59+
}
1960
if f.Interface() != val {
2061
modified[name] = val
2162
f.Set(reflect.ValueOf(val))

0 commit comments

Comments
 (0)