-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathchecker.go
41 lines (35 loc) · 864 Bytes
/
checker.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
package checker
// Checker is the representation of
// validation object
type Checker interface {
Add(rule Rule, prompt string)
Check(param interface{}) (bool, string, string)
}
type ruleChecker struct {
rules []Rule
prompts []string
}
func (c *ruleChecker) Add(rule Rule, prompt string) {
c.rules = append(c.rules, rule)
c.prompts = append(c.prompts, prompt)
}
func (c ruleChecker) Check(param interface{}) (bool, string, string) {
cache := make(map[string]valueKindPair)
for i := 0; i < len(c.rules); i++ {
c.rules[i].setCache(cache)
isValid, msg := c.rules[i].Check(param)
if !isValid {
prompt := c.rules[i].getPrompt()
if prompt == "" {
prompt = c.prompts[i]
}
return false, prompt, msg
}
}
return true, "", ""
}
// NewChecker returns the
// Checker implementation
func NewChecker() Checker {
return &ruleChecker{}
}