/
properties.go
115 lines (96 loc) · 2.9 KB
/
properties.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package dalgo2datastore
import (
"cloud.google.com/go/datastore"
"errors"
"fmt"
"time"
)
// IsOkToRemove checks if it's OK to remove property value
type IsOkToRemove func(p datastore.Property) bool // TODO: Open source + article?
// IsObsolete used for obsolete properties
func IsObsolete(_ datastore.Property) bool {
return true
}
// IsDuplicate indicates property is duplicate
func IsDuplicate(_ datastore.Property) bool {
return true
}
// IsFalse returns true if propertry value is false
func IsFalse(p datastore.Property) bool {
return p.Value == nil || !p.Value.(bool)
}
// IsZeroInt returns true if property value is 0
func IsZeroInt(p datastore.Property) bool {
return p.Value == nil || p.Value.(int64) == 0
}
// IsZeroBool returns true if property value false
func IsZeroBool(p datastore.Property) bool {
return p.Value == nil || !p.Value.(bool)
}
// IsZeroFloat returns true if property value 0.0
func IsZeroFloat(p datastore.Property) bool {
return p.Value == nil || p.Value.(float64) == 0
}
// IsZeroTime returns true of property value iz zero time
func IsZeroTime(p datastore.Property) bool {
return p.Value == nil || p.Value.(time.Time).IsZero()
}
// IsEmptyString returns true if property value iz empty string
func IsEmptyString(p datastore.Property) bool {
return p.Value == nil || p.Value.(string) == "" // TODO: Do we need to check for nil?
}
// IsEmptyJSON returns true if property value is empty string or empty array or empty object
func IsEmptyJSON(p datastore.Property) bool {
if p.Value == nil {
return true
}
v := p.Value.(string)
return v == "" || v == "{}" || v == "[]"
}
// IsEmptyByteArray returns true of property value is nil or empty byte array
func IsEmptyByteArray(p datastore.Property) bool {
if p.Value == nil {
return true
}
v := p.Value.([]uint8)
return len(v) == 0
}
// IsEmptyStringOrSpecificValue returns true if property value is empty string or has specific value
func IsEmptyStringOrSpecificValue(v string) func(p datastore.Property) bool {
return func(p datastore.Property) bool {
if p.Value == nil {
return true
}
s := p.Value.(string)
return s == "" || s == v
}
}
// CleanProperties removes properties in place and returns filtered slice
func CleanProperties(properties []datastore.Property, filters map[string]IsOkToRemove) (filtered []datastore.Property, err error) {
var (
i int
p datastore.Property
)
if properties == nil {
return properties, errors.New("properties == nil")
}
if filters == nil {
return properties, errors.New("filters == nil")
}
if len(filters) == 0 {
return properties, errors.New("len(filters) == 0")
}
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("failed to process property %v=%T(%v), recovered: %v", p.Name, p.Value, p.Value, r)
}
filtered = properties[:i]
}()
for _, p = range properties {
if filter, ok := filters[p.Name]; !ok || !filter(p) {
properties[i] = p
i++
}
}
return
}