-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
41 lines (35 loc) · 1017 Bytes
/
state.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 bynom
import "context"
// ChangeState invokes all modification handlers fns passing them the value v.
// The function does no affect the plate read position.
// If any signal handler from fns returns non-nil error the function fails with that error.
func ChangeState(v int64, fns ...func(int64) error) Nom {
const funcName = "ChangeState"
return func(context.Context, Plate) (err error) {
for i, mod := range fns {
if err = mod(v); err != nil {
return WrapBreadcrumb(err, funcName, i)
}
}
return
}
}
// RequireState runs state tests fns for against the value v. If at least one test fails the function will fail.
// The function does no affect the plate read position.
func RequireState(v int64, fns ...func(int64) bool) Nom {
const funcName = "RequireState"
return func(context.Context, Plate) error {
for i, test := range fns {
if !test(v) {
return WrapBreadcrumb(
ErrStateTestFailed{
Assert: v,
},
funcName,
i,
)
}
}
return nil
}
}