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
|
package decor
// OnCondition applies decorator only if a condition is true.
//
// `decorator` Decorator
//
// `cond` bool
func OnCondition(decorator Decorator, cond bool) Decorator {
return Conditional(cond, decorator, nil)
}
// OnPredicate applies decorator only if a predicate evaluates to true.
//
// `decorator` Decorator
//
// `predicate` func() bool
func OnPredicate(decorator Decorator, predicate func() bool) Decorator {
return Predicative(predicate, decorator, nil)
}
// Conditional returns decorator `a` if condition is true, otherwise
// decorator `b`.
//
// `cond` bool
//
// `a` Decorator
//
// `b` Decorator
func Conditional(cond bool, a, b Decorator) Decorator {
if cond {
return a
} else {
return b
}
}
// Predicative returns decorator `a` if predicate evaluates to true,
// otherwise decorator `b`.
//
// `predicate` func() bool
//
// `a` Decorator
//
// `b` Decorator
func Predicative(predicate func() bool, a, b Decorator) Decorator {
if predicate() {
return a
} else {
return b
}
}
|