forked from kat-co/vala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc.go
159 lines (135 loc) · 4.24 KB
/
doc.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
A simple, extensible, library to make argument validation in Go
palatable.
Instead of this:
func BoringValidation(a, b, c, d, e, f, g MyType) {
if (a == nil)
panic("a is nil")
if (b == nil)
panic("b is nil")
if (c == nil)
panic("c is nil")
if (d == nil)
panic("d is nil")
if (e == nil)
panic("e is nil")
if (f == nil)
panic("f is nil")
if (g == nil)
panic("g is nil")
}
Do this:
func ClearValidation(a, b, c, d, e, f, g MyType) {
Begin().Validate(
NotNil(a, "a"),
NotNil(b, "b"),
NotNil(c, "c"),
NotNil(d, "d"),
NotNil(e, "e"),
NotNil(f, "f"),
NotNil(g, "g"),
).CheckAndPanic() // All values will get checked before an error is thrown!
}
Instead of this:
func BoringValidation(a, b, c, d, e, f, g MyType) error {
if (a == nil)
return fmt.Errorf("a is nil")
if (b == nil)
return fmt.Errorf("b is nil")
if (c == nil)
return fmt.Errorf("c is nil")
if (d == nil)
return fmt.Errorf("d is nil")
if (e == nil)
return fmt.Errorf("e is nil")
if (f == nil)
return fmt.Errorf("f is nil")
if (g == nil)
return fmt.Errorf("g is nil")
}
Do this:
func ClearValidation(a, b, c, d, e, f, g MyType) (err error) {
defer func() { recover() }
Begin().Validate(
NotNil(a, "a"),
NotNil(b, "b"),
NotNil(c, "c"),
NotNil(d, "d"),
NotNil(e, "e"),
NotNil(f, "f"),
NotNil(g, "g"),
).CheckSetErrorAndPanic(&err) // Return error will get set, and the function will return.
// ...
VeryExpensiveFunction(c, d)
}
Tier your validation:
func ClearValidation(a, b, c MyType) (err error) {
err = Begin().Validate(
NotNil(a, "a"),
NotNil(b, "b"),
NotNil(c, "c"),
).CheckAndPanic().Validate( // Panic will occur here if a, b, or c are nil.
Rng(len(a.Items), 50, 50, "a.Items"),
Gt(b.UserCount, 0, "b.UserCount"),
Eq(c.Name, "Vala", "c.name"),
Not(Eq(c.FriendlyName, "Foo", "c.FriendlyName"), "!Eq"),
).Check()
if err != nil {
return err
}
// ...
VeryExpensiveFunction(c, d)
}
The nameOrErr parameter to the default checker functions allows you to
either specify the parameters name or to provide a custom error to be
used instead of the default error values:
a, b := nil, nil
err := Begin().Validate(
NotNil(a, ErrANotNil),
NotNil(b, "b"),
).Check()
err will have a value of:
Validation{
Errors: []*CheckerError{
&CheckerError{Name: "", Err: ErrANotNil},
&CheckerError{Name: "b", Err: ErrNotNil},
}
}
Extend with your own validators for readability. Note that an error
should always be returned so that the Not function can return a message
if it passes. Unlike idiomatic Go, use the boolean to check for success.
func ReportFitsRepository(report *Report, repository *Repository) Checker {
return func() *CheckerError {
if repository.Type != report.Type {
return fmt.Errorf("A %s report does not belong in a %s repository.", report.Type, repository.Type)
}
return nil
}
}
func AuthorCanUpload(authorName string, repository *Repository) Checker {
return func() *CheckerError {
if !repository.AuthorCanUpload(authorName) {
return fmt.Errorf("%s does not have access to this repository.", authorName)
}
return nil
}
}
func AuthorIsCollaborator(authorName string, report *Report) Checker {
return func() *CheckerError {
for _, collaboratorName := range report.Collaborators() {
if collaboratorName == authorName {
return nil
}
}
return fmt.Errorf("The given author was not one of the collaborators for this report.")
}
}
func HandleReport(authorName string, report *Report, repository *Repository) {
Begin().Validate(
AuthorIsCollaborator(authorName, report),
AuthorCanUpload(authorName, repository),
ReportFitsRepository(report, repository),
).CheckAndPanic()
}
*/
package vala