-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_scope.go
136 lines (122 loc) · 3.62 KB
/
field_scope.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package firevault
import "reflect"
// fieldScope contains a single field's
// information to help validate it.
// It complies with the FieldScope interface.
type fieldScope struct {
strct reflect.Value
field string
structField string
displayField string
path string
structPath string
value reflect.Value
kind reflect.Kind
typ reflect.Type
rule string
param string
dive bool
rules []string
}
// A Firevault FieldScope interface gives access
// to all information needed to validate a field.
type FieldScope interface {
// Struct returns the reflected top level struct.
Struct() reflect.Value
// Field returns the field's name, with the tag
// name taking precedence over the field's
// struct name.
Field() string
// StructField returns the field's actual name
// from the struct.
StructField() string
// DisplayField returns the field's struct name
// in a human-readable form. It splits camel,
// pascal, and snake case names into
// space-separated words, including separating
// adjacent letters and numbers
// (e.g. "FirstName" -> "First Name").
DisplayField() string
// Path returns the field's dot-separated path,
// with the tag names taking precedence over the
// fields' actual names (e.g. "names.first").
Path() string
// StructPath returns the field's actual
// dot-separated path from the stuct
// (e.g. "Names.First").
StructPath() string
// Value returns the current field's reflected
// value to be validated.
Value() reflect.Value
// Kind returns the Value's reflect Kind
// (eg. time.Time's kind is a struct).
Kind() reflect.Kind
// Type returns the Value's reflect Type
// (eg. time.Time's type is time.Time).
Type() reflect.Type
// Rule returns the current validation's rule name.
Rule() string
// Param returns the param value, in string form
// for comparison.
Param() string
}
// Struct returns the reflected top level struct.
func (fs *fieldScope) Struct() reflect.Value {
return fs.strct
}
// Field returns the field's name, with the tag
// name taking precedence over the field's
// actual name.
func (fs *fieldScope) Field() string {
return fs.field
}
// StructField returns the field's actual name
// from the struct.
func (fs *fieldScope) StructField() string {
return fs.structField
}
// DisplayField returns the field's struct name
// in a human-readable form. It splits camel,
// pascal, and snake case names into
// space-separated words, including separating
// adjacent letters and numbers
// (e.g. "FirstName" -> "First Name").
func (fs *fieldScope) DisplayField() string {
return fs.displayField
}
// Path returns the field's dot-separated path,
// with the tag names taking precedence over the
// fields' actual names (e.g. "names.first").
func (fs *fieldScope) Path() string {
return fs.path
}
// StructPath returns the field's actual
// dot-separated path from the stuct
// (e.g. "Names.First").
func (fs *fieldScope) StructPath() string {
return fs.structPath
}
// Value returns the current field's reflected
// value to be validated.
func (fs *fieldScope) Value() reflect.Value {
return fs.value
}
// Kind returns the Value's reflect Kind
// (eg. time.Time's kind is a struct).
func (fs *fieldScope) Kind() reflect.Kind {
return fs.kind
}
// Type returns the Value's reflect Type
// (eg. time.Time's type is time.Time).
func (fs *fieldScope) Type() reflect.Type {
return fs.typ
}
// Rule returns the current validation's rule name.
func (fs *fieldScope) Rule() string {
return fs.rule
}
// Param returns the param value, in string form
// for comparison.
func (fs *fieldScope) Param() string {
return fs.param
}