-
Notifications
You must be signed in to change notification settings - Fork 0
/
columnfilter.go
89 lines (74 loc) · 2.88 KB
/
columnfilter.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
package sqldb
import (
"reflect"
)
type ColumnFilter interface {
IgnoreColumn(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool
}
type ColumnFilterFunc func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool
func (f ColumnFilterFunc) IgnoreColumn(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return f(name, flags, fieldType, fieldValue)
}
func IgnoreColumns(names ...string) ColumnFilter {
return ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
for _, ignore := range names {
if name == ignore {
return true
}
}
return false
})
}
func OnlyColumns(names ...string) ColumnFilter {
return ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
for _, include := range names {
if name == include {
return false
}
}
return true
})
}
func IgnoreStructFields(names ...string) ColumnFilter {
return ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
for _, ignore := range names {
if fieldType.Name == ignore {
return true
}
}
return false
})
}
func OnlyStructFields(names ...string) ColumnFilter {
return ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
for _, include := range names {
if fieldType.Name == include {
return false
}
}
return true
})
}
func IgnoreFlags(ignore FieldFlag) ColumnFilter {
return ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return flags&ignore != 0
})
}
var IgnoreDefault ColumnFilter = ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return flags.Default()
})
var IgnorePrimaryKey ColumnFilter = ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return flags.PrimaryKey()
})
var IgnoreReadOnly ColumnFilter = ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return flags.ReadOnly()
})
var IgnoreNull ColumnFilter = ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return IsNull(fieldValue)
})
var IgnoreNullOrZero ColumnFilter = ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return IsNullOrZero(fieldValue)
})
var IgnoreNullOrZeroDefault ColumnFilter = ColumnFilterFunc(func(name string, flags FieldFlag, fieldType reflect.StructField, fieldValue reflect.Value) bool {
return flags.Default() && IsNullOrZero(fieldValue)
})