github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/expr/use/when.go (about) 1 package use 2 3 // When is if...else expression builder 4 type When[T any] struct { 5 condition bool 6 then T 7 } 8 9 // Else evaluates expression and returns result 10 func (w When[T]) Else(fals T) T { 11 if w.condition { 12 return w.then 13 } 14 return fals 15 } 16 17 // ElseZero evaluates expression and returns zero value as default 18 func (w When[T]) ElseZero() (out T) { 19 if w.condition { 20 return w.then 21 } 22 return out 23 } 24 25 // ElseErr returns the success result or error according to the condition 26 func (w When[T]) ElseErr(err error) (T, error) { 27 if w.condition { 28 return w.then, nil 29 } 30 var fals T 31 return fals, err 32 } 33 34 // ElseGetErr returns the success result or a result of the fals function according to the condition 35 func (w When[T]) ElseGetErr(fals func() (T, error)) (T, error) { 36 if w.condition { 37 return w.then, nil 38 } 39 return fals() 40 } 41 42 // ElseGet returns the tru or a return of the fals function according to the condition 43 func (w When[T]) ElseGet(fals func() T) T { 44 if w.condition { 45 return w.then 46 } 47 return fals() 48 } 49 50 // If creates new condition branch in the expression 51 func (w When[T]) If(condition bool, tru T) When[T] { 52 if w.condition { 53 return w 54 } 55 return newWhen(condition, tru) 56 } 57 58 // IfGet creates new condition branch for a getter function 59 func (w When[T]) IfGet(condition bool, tru func() T) When[T] { 60 if w.condition { 61 return w 62 } 63 return newWhen(condition, evaluate(condition, tru)) 64 } 65 66 // IfGetErr creates new condition branch for an error return getter function 67 func (w When[T]) IfGetErr(condition bool, tru func() (T, error)) WhenErr[T] { 68 if w.condition { 69 return newWhenErr(w.condition, w.then, nil) 70 } 71 return If_(condition, tru) 72 } 73 74 // Other creates new condition branch for a getter function. 75 // The condition function is called only if the current condition is false. 76 func (w When[T]) Other(condition func() bool, tru func() T) When[T] { 77 if w.condition { 78 return w 79 } 80 81 c := condition() 82 return newWhen(c, evaluate(c, tru)) 83 } 84 85 // OtherErr creates new condition branch for an error return getter function. 86 // The condition function is called only if the current condition is false. 87 func (w When[T]) OtherErr(condition func() bool, tru func() (T, error)) WhenErr[T] { 88 if w.condition { 89 return newWhenErr(w.condition, w.then, nil) 90 } 91 return If_(condition(), tru) 92 } 93 94 // Eval evaluates the expression and returns ok==false if there is no satisfied condition 95 func (w When[T]) Eval() (out T, ok bool) { 96 if w.condition { 97 return w.then, true 98 } 99 return out, false 100 } 101 102 // Or returns the current condition 'w' if true, or 'another' otherwise 103 func (w When[T]) Or(other When[T]) When[T] { 104 if w.condition { 105 return w 106 } 107 return other 108 } 109 110 func newWhen[T any](condition bool, then T) When[T] { 111 return When[T]{condition, then} 112 } 113 114 func evaluate[T any](condition bool, tru func() T) (out T) { 115 if condition { 116 out = tru() 117 } 118 return out 119 }