-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathverifier.go
214 lines (193 loc) · 6.29 KB
/
verifier.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package verifier
import (
"errors"
"fmt"
"io"
"os"
"runtime"
"sync/atomic"
)
// New creates verification instance (recommended).
// It tracks verification state.
// If you forget to check internal error, using `GetError` or `PanicOnError` methods,
// it will write error message to UnhandledVerificationsWriter (default: os.Stdout).
// This mechanism will help you track down possible unhandled verifications.
// If you don't wan't to track anything, create zero verifier `Verify{}`.
func New() *Verify {
v := &Verify{
creationStack: captureCreationStack(),
errFactory: fmt.Errorf,
}
runtime.SetFinalizer(v, printWarningOnUncheckedVerification)
return v
}
// WithErrFactory sets error construction function (default: fmt.Errorf).
// Use it to set custom error type of error, returned by Verify.GetError().
func (v *Verify) WithErrFactory(factory func(string, ...interface{}) error) *Verify {
v.errFactory = factory
return v
}
// Offensive creates verification instance (not-recommended).
// It tracks verification state and stops application process when founds unchecked verification.
// If you forget to check internal error, using `GetError` or `PanicOnError` methods,
// it will write error message to UnhandledVerificationsWriter (default: os.Stdout) and WILL STOP YOUR PROCESS.
// Created for people who adopt offensive programming(https://en.wikipedia.org/wiki/Offensive_programming).
// This mechanism will help you track down possible unhandled verifications.
// USE IT WISELY.
func Offensive() *Verify {
v := &Verify{
creationStack: captureCreationStack(),
}
runtime.SetFinalizer(v, failProcessOnUncheckedVerification)
return v
}
// Verify represents verification instance.
// All checks can be performed on it using `That` or `Predicate` functions.
// After one failed check all others won't count and predicates won't be evaluated.
// Use Verify.GetError function to check if there where any during verification process.
type Verify struct {
creationStack []uintptr
err error
errFactory func(string, ...interface{}) error
checked bool
}
// WithError verifies condition passed as first argument.
// If `positiveCondition == true`, verification will proceed for other checks.
// If `positiveCondition == false`, internal state will be filled with error specified as second argument.
// After the first failed verification all others won't count and predicates won't be evaluated.
func (v *Verify) WithError(positiveCondition bool, err error) *Verify {
vObj := v
if v == nil {
vObj = &Verify{}
}
vObj.checked = false
if vObj.err != nil {
return vObj
}
if positiveCondition {
return vObj
}
vObj.err = err
return vObj
}
// That verifies condition passed as first argument.
// If `positiveCondition == true`, verification will proceed for other checks.
// If `positiveCondition == false`, internal state will be filled with error,
// using message argument as format in error factory func(message, args...) (default: fmt.Errorf).
// After the first failed verification all others won't count and predicates won't be evaluated.
func (v *Verify) That(positiveCondition bool, message string, args ...interface{}) *Verify {
vObj := v
if v == nil {
vObj = &Verify{}
}
vObj.checked = false
if vObj.err != nil {
return vObj
}
if positiveCondition {
return vObj
}
vObj.err = vObj.errorf(message, args...)
return vObj
}
// That evaluates predicate passed as first argument.
// If `predicate() == true`, verification will proceed for other checks.
// If `predicate() == false`, internal state will be filled with error,
// using message argument as format in error factory func(message, args...) (default: fmt.Errorf).
// After the first failed verification all others won't count and predicates won't be evaluated.
func (v *Verify) Predicate(predicate func() bool, message string, args ...interface{}) *Verify {
vObj := v
if v == nil {
vObj = &Verify{}
}
vObj.checked = false
if vObj.err != nil {
return vObj
}
if predicate() {
return vObj
}
vObj.err = vObj.errorf(message, args...)
return vObj
}
// GetError extracts error from internal state to check if there where any during verification process.
func (v *Verify) GetError() error {
if v == nil {
return errors.New("verifier instance is nil")
}
v.checked = true
return v.err
}
// PanicOnError panics if there is an error in internal state.
// Created for people who adopt offensive programming(https://en.wikipedia.org/wiki/Offensive_programming).
func (v *Verify) PanicOnError() {
if v == nil {
panic("verifier instance is nil")
}
v.checked = true
if v.err != nil {
panic("verification failure: " + v.err.Error())
}
}
// String represents verification and it's status as string type.
func (v *Verify) String() string {
if v == nil {
return "nil"
}
if v.err == nil {
return "verification success"
}
return "verification failure: " + v.err.Error()
}
func (v *Verify) errorf(message string, args ...interface{}) error {
if v.errFactory == nil {
v.errFactory = fmt.Errorf
}
return v.errFactory(message, args...)
}
func (v *Verify) printCreationStack(writer io.Writer) {
frames := runtime.CallersFrames(v.creationStack)
for {
frame, more := frames.Next()
fmt.Fprintf(writer, "%s\n\t%s:%d\n", frame.Function, frame.File, frame.Line)
if !more {
break
}
}
}
func failProcessOnUncheckedVerification(v *Verify) {
if v.checked {
return
}
printWarningOnUncheckedVerification(v)
os.Exit(1)
}
func captureCreationStack() []uintptr {
var rawStack [32]uintptr
numberOfFrames := runtime.Callers(3, rawStack[:])
return rawStack[:numberOfFrames]
}
type writerWrapper struct {
value io.Writer
}
var verificationsWriter atomic.Value
// SetUnhandledVerificationsWriter gives you ability to override UnhandledVerificationsWriter (default: os.Stdout).
func SetUnhandledVerificationsWriter(w io.Writer) {
verificationsWriter.Store(writerWrapper{w})
}
func init() {
SetUnhandledVerificationsWriter(os.Stdout)
}
func printWarningOnUncheckedVerification(v *Verify) {
if v.checked {
return
}
rawWriter := verificationsWriter.Load()
if rawWriter == nil || rawWriter.(writerWrapper).value == nil {
rawWriter = writerWrapper{os.Stdout}
}
writer := rawWriter.(writerWrapper).value
fmt.Fprintf(writer, "[ERROR] found unhandled verification: %s\n", v)
fmt.Fprint(writer, "verification was created here:\n")
v.printCreationStack(writer)
}