field.go
1.7 KB
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
package urlstruct
import (
"fmt"
"reflect"
"strings"
"github.com/codemodus/kace"
"github.com/go-pg/zerochecker"
"github.com/vmihailenco/tagparser"
)
type OpCode int
const (
OpEq OpCode = iota + 1
OpNotEq
OpLT
OpLTE
OpGT
OpGTE
OpIEq
OpMatch
)
type Field struct {
Type reflect.Type
Name string
Index []int
Tag *tagparser.Tag
Column string
Op OpCode
noDecode bool
required bool
noWhere bool
scanValue scannerFunc
isZeroValue zerochecker.Func
}
func (f *Field) init() {
_, f.required = f.Tag.Options["required"]
_, f.noDecode = f.Tag.Options["nodecode"]
_, f.noWhere = f.Tag.Options["nowhere"]
if f.required && f.noWhere {
err := fmt.Errorf("urlstruct: required and nowhere tags can't be set together")
panic(err)
}
const sep = "_"
f.Column, f.Op = splitColumnOperator(kace.Snake(f.Name), sep)
if f.Type.Kind() == reflect.Slice {
f.scanValue = sliceScanner(f.Type)
} else {
f.scanValue = scanner(f.Type)
}
f.isZeroValue = zerochecker.Checker(f.Type)
}
func (f *Field) Value(strct reflect.Value) reflect.Value {
return strct.FieldByIndex(f.Index)
}
func (f *Field) Omit(value reflect.Value) bool {
return !f.required && (f.noWhere || f.isZeroValue(value))
}
func splitColumnOperator(s, sep string) (string, OpCode) {
ind := strings.LastIndex(s, sep)
if ind == -1 {
return s, OpEq
}
col := s[:ind]
op := s[ind+len(sep):]
switch op {
case "eq", "":
return col, OpEq
case "neq", "exclude":
return col, OpNotEq
case "gt":
return col, OpGT
case "gte":
return col, OpGTE
case "lt":
return col, OpLT
case "lte":
return col, OpLTE
case "ieq":
return col, OpIEq
case "match":
return col, OpMatch
default:
return s, OpEq
}
}