github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/expr/use/when_err.go (about)

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