use

package
v0.0.14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2024 License: MIT Imports: 0 Imported by: 3

Documentation

Overview

Package use provides conditional expression builders

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type When

type When[T any] struct {
	// contains filtered or unexported fields
}

When is if...else expression builder

func If

func If[T any](condition bool, tru T) When[T]

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 IfGet

func IfGet[T any](condition bool, then func() T) When[T]

IfGet is like If but aimed to use an getter function

func IfOK added in v0.0.13

func IfOK[T any](val T, ok bool) When[T]

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 (When[T]) Else

func (w When[T]) Else(fals T) T

Else evaluates expression and returns result

func (When[T]) ElseErr

func (w When[T]) ElseErr(err error) (T, error)

ElseErr returns the success result or error according to the condition

func (When[T]) ElseGet

func (w When[T]) ElseGet(fals func() T) T

ElseGet returns the tru or a return of the fals function according to the condition

func (When[T]) ElseGetErr

func (w When[T]) ElseGetErr(fals func() (T, error)) (T, error)

ElseGetErr returns the success result or a result of the fals function according to the condition

func (When[T]) ElseZero

func (w When[T]) ElseZero() (out T)

ElseZero evaluates expression and returns zero value as default

func (When[T]) Eval

func (w When[T]) Eval() (out T, ok bool)

Eval evaluates the expression and returns ok==false if there is no satisfied condition

func (When[T]) If

func (w When[T]) If(condition bool, tru T) When[T]

If creates new condition branch in the expression

func (When[T]) IfGet

func (w When[T]) IfGet(condition bool, tru func() T) When[T]

IfGet creates new condition branch for a getter function

func (When[T]) IfGetErr

func (w When[T]) IfGetErr(condition bool, tru func() (T, error)) WhenErr[T]

IfGetErr creates new condition branch for an error return getter function

func (When[T]) Or added in v0.0.10

func (w When[T]) Or(other When[T]) When[T]

Or returns the current condition 'w' if true, or 'another' otherwise

func (When[T]) Other

func (w When[T]) Other(condition func() bool, tru func() T) When[T]

Other creates new condition branch for a getter function. The condition function is called only if the current condition is false.

func (When[T]) OtherErr

func (w When[T]) OtherErr(condition func() bool, tru func() (T, error)) WhenErr[T]

OtherErr creates new condition branch for an error return getter function. The condition function is called only if the current condition is false.

type WhenErr

type WhenErr[T any] struct {
	// contains filtered or unexported fields
}

WhenErr if..else expression builder

func IfGetErr

func IfGetErr[T any](condition bool, tru func() (T, error)) WhenErr[T]

IfGetErr is like If but aimed to use an error return function

func If_

func If_[T any](condition bool, tru func() (T, error)) WhenErr[T]

If_ is alias of IfErr

func (WhenErr[T]) Else

func (w WhenErr[T]) Else(fals T) (T, error)

Else evaluates expression and returns result

func (WhenErr[T]) ElseErr

func (w WhenErr[T]) ElseErr(err error) (T, error)

ElseErr returns the success result or error according to the condition

func (WhenErr[T]) ElseGet

func (w WhenErr[T]) ElseGet(fals func() T) (T, error)

ElseGet returns the tru or a return of the fals function according to the condition

func (WhenErr[T]) ElseGetErr

func (w WhenErr[T]) ElseGetErr(fals func() (T, error)) (T, error)

ElseGetErr returns the success result or a result of the fals function according to the condition

func (WhenErr[T]) ElseZero added in v0.0.9

func (w WhenErr[T]) ElseZero() (out T, err error)

ElseZero evaluates expression and returns zero value as default

func (WhenErr[T]) Eval added in v0.0.9

func (w WhenErr[T]) Eval() (out T, ok bool, err error)

Eval evaluates the expression and returns ok==false if there is no satisfied condition

func (WhenErr[T]) If

func (w WhenErr[T]) If(condition bool, tru T) WhenErr[T]

If creates new condition branch in the expression

func (WhenErr[T]) IfGet

func (w WhenErr[T]) IfGet(condition bool, tru func() T) WhenErr[T]

IfGet creates new condition branch for a getter function

func (WhenErr[T]) IfGetErr

func (w WhenErr[T]) IfGetErr(condition bool, tru func() (T, error)) WhenErr[T]

IfGetErr creates new condition branch for an error return getter function

func (WhenErr[T]) Other

func (w WhenErr[T]) Other(condition func() bool, tru func() T) WhenErr[T]

Other creates new condition branch for a getter function. The condition function is called only if the current condition is false.

func (WhenErr[T]) OtherErr

func (w WhenErr[T]) OtherErr(condition func() bool, tru func() (T, error)) WhenErr[T]

OtherErr creates new condition branch for an error return getter function. The condition function is called only if the current condition is false.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL