-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathok.go
111 lines (98 loc) · 2.48 KB
/
ok.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
// Copyright 2016 Andreas Pannewitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package do
// ===========================================================================
// Ok represents some condition
// which is true by default.
//
// The null value is useful: its Do() never returns false.
type Ok func() bool
// Do applies Ok iff Ok is not nil
// and returns its value - usually true,
func (ok *Ok) Do() bool {
if *ok != nil {
return (*ok)()
}
return true
}
// Not returns the negation - usually false.
func (ok *Ok) Not() bool {
if *ok != nil {
return !(*ok)()
}
return false
}
// ===========================================================================
// OkJoin returns a closure around given fs.
//
// Iff there are no fs, nil is returned, and
// iff there is only one fs, this single fs is returned.
//
// Evaluate the returned function
// by invoking its Do() or
// by invoking it directly, iff not nil.
//
// Note: Order matters - evaluation terminates on first exceptional (non-default) result.
func OkJoin(fs ...Ok) Ok {
switch len(fs) {
case 0:
return nil
case 1:
return fs[0]
default:
return func() bool {
for _, f := range fs {
if !(&f).Do() {
return false
}
}
return true
}
}
}
// ===========================================================================
// Set sets all oks as the new ok
// when the returned Option is applied.
func (ok *Ok) Set(oks ...Ok) Option {
return func(any interface{}) Opt {
prev := *ok
*ok = OkJoin(oks...)
return func() Opt {
return (*ok).Set(prev)(any)
}
}
}
// Add appends all oks to the existing ok
// when the returned Option is applied.
func (ok *Ok) Add(oks ...Ok) Option {
if ok == nil || *ok == nil {
return (*ok).Set(oks...)
}
return func(any interface{}) Opt {
prev := *ok
*ok = OkJoin(append([]Ok{prev}, oks...)...)
return func() Opt {
return (*ok).Set(prev)(any)
}
}
}
// ===========================================================================
// OkIt returns an Ok function
// which Do()es the Join of the given its
// and returns the default, namely: `true`,
// upon evaluation.
//
// Evaluate the returned function
// by invoking it's Do() or
// by invoking it directly, iff not nil.
//
// OkIt is a convenient wrapper.
func OkIt(its ...It) Ok {
return func() bool {
it := ItJoin(its...)
(&it).Do()
return true
}
}
// ===========================================================================