Repository navigation
Expand file tree
/
Copy pathcondition.go
More file actions
77 lines (66 loc) · 1.84 KB
/
Copy pathcondition.go
File metadata and controls
77 lines (66 loc) · 1.84 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sugar
// Ternary returns ifOutput if condition is true, otherwise returns elseOutput.
func Ternary[T any](condition bool, ifOutput T, elseOutput T) T {
if condition {
return ifOutput
}
return elseOutput
}
// TernaryF returns the result of ifCallback if condition is true, otherwise returns the result of elseCallback.
func TernaryF[T any](condition bool, ifCallback func() T, elseCallback func() T) T {
if condition {
return ifCallback()
}
return elseCallback()
}
// IfElse represents a conditional value container.
type IfElse[T any] struct {
Result T
Ok bool
}
// IfValue creates a new IfElse based on a condition and value.
func IfValue[T any](condition bool, result T) *IfElse[T] {
if condition {
return &IfElse[T]{result, true}
}
var t T
return &IfElse[T]{t, false}
}
// IfFunc creates a new IfElse based on a condition and callback.
func IfFunc[T any](condition bool, resultFn func() T) *IfElse[T] {
if condition {
return &IfElse[T]{resultFn(), true}
}
var t T
return &IfElse[T]{t, false}
}
// ElseIf adds another condition to check if the previous condition was false.
func (i *IfElse[T]) ElseIf(condition bool, result T) *IfElse[T] {
if condition && !i.Ok {
i.Result = result
i.Ok = true
}
return i
}
// ElseIfFunc adds another condition with a callback to check if the previous condition was false.
func (i *IfElse[T]) ElseIfFunc(condition bool, resultFn func() T) *IfElse[T] {
if condition && !i.Ok {
i.Result = resultFn()
i.Ok = true
}
return i
}
// Else returns the result if any condition was true, otherwise returns the fallback.
func (i *IfElse[T]) Else(result T) T {
if i.Ok {
return i.Result
}
return result
}
// ElseFunc returns the result if any condition was true, otherwise calls the callback.
func (i *IfElse[T]) ElseFunc(resultFn func() T) T {
if i.Ok {
return i.Result
}
return resultFn()
}