forked from deta/deta-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_util.go
91 lines (81 loc) · 1.84 KB
/
base_util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package deta
import "reflect"
// append util
type appendUtil struct {
value interface{}
}
// prepend util
type prependUtil struct {
value interface{}
}
// increment util
type incrementUtil struct {
value interface{}
}
type trimUtil struct{}
// utility struct
type util struct{}
// Append utility, can be used in Updates to append items to a list
//
// value can be single or a list of items
func (u *util) Append(value interface{}) *appendUtil {
switch reflect.ValueOf(value).Kind() {
case reflect.Array, reflect.Slice:
return &appendUtil{
value: value,
}
case reflect.Ptr:
switch reflect.Indirect(reflect.ValueOf(value)).Kind() {
case reflect.Array, reflect.Slice:
return &appendUtil{
value: value,
}
default:
return &appendUtil{
value: []interface{}{value},
}
}
default:
return &appendUtil{
value: []interface{}{value},
}
}
}
// Prepend utility, can be used in Updates to prepend items to a list
//
// value can be single or a list of items
func (u *util) Prepend(value interface{}) *prependUtil {
switch reflect.ValueOf(value).Kind() {
case reflect.Array, reflect.Slice:
return &prependUtil{
value: value,
}
case reflect.Ptr:
switch reflect.Indirect(reflect.ValueOf(value)).Kind() {
case reflect.Array, reflect.Slice:
return &prependUtil{
value: value,
}
default:
return &prependUtil{
value: []interface{}{value},
}
}
default:
return &prependUtil{
value: []interface{}{value},
}
}
}
// Increment utility, can be used in Updates to increment/decrement a numerical value
//
// if value is negative, the value is subtracted in the update operation
func (u *util) Increment(value interface{}) *incrementUtil {
return &incrementUtil{
value: value,
}
}
// Trim utility, can be used in Updates to trim a field from an item
func (u *util) Trim() *trimUtil {
return &trimUtil{}
}