-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_int.go
115 lines (111 loc) · 2.78 KB
/
spec_int.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
package vd
import (
xconv "github.com/goclub/conv"
"github.com/hoisie/mustache"
)
type IntSpec struct {
Name string
Path string
// AllowZero bool // 暂时取消 AllowZero,目的是降低使用者学习成本,观察一段时间后再决定是否完全去掉 (2020年08月07日 by @nimoc)
Min OptionInt
MinMessage string
Max OptionInt
MaxMessage string
Pattern []string
BanPattern []string
PatternMessage string
}
type intSpecRender struct {
Value interface{}
IntSpec
}
func (spec IntSpec) render (message string, value interface{}) string {
context := intSpecRender{
Value: value,
IntSpec: spec,
}
return mustache.Render(message, context)
}
func (r *Rule) Uint(v uint, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Uint8(v uint8, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Uint16(v uint16, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Uint32(v uint32, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Uint64(v uint64, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Int8(v int8, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Int16(v int16, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Int32(v int32, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Int64(v int64, spec IntSpec) {
r.Int(int(v), spec)
}
func (r *Rule) Int(v int, spec IntSpec) {
if r.Fail {return}
// if v == 0 && !spec.AllowZero {
// r.Break(r.Format.IntNotAllowEmpty(spec.Name))
// return
// }
if spec.CheckMin(v, r) { return }
if spec.CheckMax(v ,r) { return }
if spec.CheckPattern(v, r) {return}
if spec.CheckBanPattern(v, r) {return}
return
}
func (spec IntSpec) CheckMin(v int, r *Rule) (fail bool) {
if !spec.Min.Valid() {
return
}
min := spec.Min.Unwrap()
pass := v >= min
if !pass {
message := r.CreateMessage(spec.MinMessage, func() string {
return r.Format.IntMin(spec.Name, spec.Path, v, min)
})
r.Break(spec.render(message, v), spec.Path)
}
return
}
func (spec IntSpec) CheckMax(v int, r *Rule) (fail bool) {
if !spec.Max.Valid() {
return
}
max := spec.Max.Unwrap()
pass := v <= max
if !pass {
message := r.CreateMessage(spec.MaxMessage, func() string {
return r.Format.IntMax(spec.Name, spec.Path, v, max)
})
r.Break(spec.render(message, v), spec.Path)
}
return
}
func (spec IntSpec) CheckPattern(v int, r *Rule) (fail bool) {
return checkPattern(patternData{
Pattern: spec.Pattern,
PatternMessage: spec.PatternMessage,
Name: spec.Name,
Path: spec.Path,
}, spec.render, xconv.IntString(v), r, spec.Path)
}
func (spec IntSpec) CheckBanPattern(v int, r *Rule) (fail bool) {
return checkBanPattern(banPatternData{
BanPattern: spec.BanPattern,
PatternMessage: spec.PatternMessage,
Name: spec.Name,
Path: spec.Path,
}, spec.render, xconv.IntString(v), r, spec.Path)
}