-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
37 lines (32 loc) · 1.11 KB
/
api.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
// Package use provides conditional expression builders
package use
// If builds use.If(condition, tru).Else(fals) expression builder.
// Looks like val := use.If(condition, valOnTrue).Else(defaltVal) tha can be rewrited by:
//
// var val type
// if condtion {
// val = valOnTrue
// } else {
// val = defaltVal
// }
func If[T any](condition bool, tru T) When[T] {
return newWhen(condition, tru)
}
// IfGet is like If but aimed to use an getter function
func IfGet[T any](condition bool, then func() T) When[T] {
return If(condition, evaluate(condition, then))
}
// IfGetErr is like If but aimed to use an error return function
func IfGetErr[T any](condition bool, tru func() (T, error)) WhenErr[T] {
then, err := evaluateErr(condition, tru)
return newWhenErr(condition, then, err)
}
// If_ is alias of IfErr
func If_[T any](condition bool, tru func() (T, error)) WhenErr[T] {
return IfGetErr(condition, tru)
}
// IfOK like If but with reverse arguments order.
// It is useful for wrapping calls of methods that return a value and a bool validity indicator.
func IfOK[T any](val T, ok bool) When[T] {
return newWhen(ok, val)
}