-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif.go
45 lines (38 loc) · 1.11 KB
/
if.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
// 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
// ===========================================================================
// If represents an opional action.
//
// It wraps It (an Action - pun intended)
// and a boolean If switch
// and it facilitates conditional invocation via its Do() method.
//
// Intended use is for conditional logging, counting etc.
//
// The null value is useful: its Do() never does anything, it's a nop.
type If struct {
It
If bool
}
// Do applies It iff If is true and It is not nil,
// and makes If a Doer.
func (a If) Do() {
if a.If && a.It != nil {
a.It()
}
}
// ===========================================================================
// Iff makes iff the new If value
// when the returned Option is applied.
func (fn *If) Iff(iff bool) Option {
return func(any interface{}) Opt {
prev := (*fn).If
(*fn).If = iff
return func() Opt {
return (*fn).Iff(prev)(any)
}
}
}
// ===========================================================================