-
Notifications
You must be signed in to change notification settings - Fork 1
/
exclusion_strategy.go
109 lines (101 loc) · 2.34 KB
/
exclusion_strategy.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
package sieve
import (
"bytes"
"fmt"
"reflect"
"strconv"
"time"
)
type ExclusionStrategy interface {
Name() string
Check(v ...reflect.Value) bool
}
type excludeEqualField struct {
fieldName string
}
func (excludeEqualField) Name() string {
return "equal_filed"
}
func (e excludeEqualField) Check(v ...reflect.Value) bool {
if len(v) < 2 {
return false
}
if !v[0].CanInterface() {
return false
}
kind := v[1].Type().Kind()
if kind == reflect.Map {
mapItemValue := v[1].MapIndex(reflect.ValueOf(e.fieldName))
if mapItemValue.IsValid() && mapItemValue.CanInterface() {
return reflect.DeepEqual(v[0].Interface(), mapItemValue.Interface())
}
} else if kind == reflect.Struct {
fieldValue := v[1].FieldByName(e.fieldName)
if fieldValue.IsValid() && fieldValue.CanInterface() {
return reflect.DeepEqual(v[0].Interface(), fieldValue.Interface())
}
}
return false
}
type excludeEqualValue struct {
value string
}
func (excludeEqualValue) Name() string {
return "equal_value"
}
func (e excludeEqualValue) Check(v ...reflect.Value) bool {
if len(v) < 1 {
return false
}
switch v[0].Type().Kind() {
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(e.value, 10, 64)
if err != nil {
return false
}
return i == v[0].Int()
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
u, err := strconv.ParseUint(e.value, 10, 64)
if err != nil {
return false
}
return u == v[0].Uint()
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(e.value, 64)
if err != nil {
return false
}
return f == v[0].Float()
case reflect.Bool:
b, err := strconv.ParseBool(e.value)
if err != nil {
return false
}
return b == v[0].Bool()
case reflect.String:
return v[0].String() == e.value
case reflect.Slice:
if v[0].Elem().Kind() == reflect.Uint8 {
return bytes.Equal(v[0].Bytes(), []byte(e.value))
}
case reflect.Struct:
{
if !v[0].CanInterface() {
return false
}
switch rv := v[0].Interface().(type) {
case time.Time:
rv.String()
if t, err := time.Parse(time.RFC3339, e.value); err == nil {
return rv.Equal(t)
}
if timeStamp, err := strconv.ParseInt(e.value, 10, 64); err == nil {
return rv.Equal(time.Unix(timeStamp, 0))
}
case fmt.Stringer:
return rv.String() == e.value
}
}
}
return false
}