generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conditional.go
82 lines (74 loc) · 1.89 KB
/
conditional.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
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
78
79
80
81
82
package arrange
import "go.uber.org/fx"
// Conditional is a simple strategy for emitting options into
// an fx.App container
type Conditional struct {
}
// Then returns all the given options if this Conditional is not nil.
// If this Conditional is nil, it returns an empty fx.Options.
func (c *Conditional) Then(o ...fx.Option) fx.Option {
if c != nil {
return fx.Options(o...)
}
return fx.Options()
}
// If returns a non-nil Conditional if its sole argument is true.
// This allows one to build up conditional components:
//
// v := viper.New() // initialize
// fx.New(
// fx.Supply(v),
//
// // it's safe to provide this unconditionally as fx will not invoke
// // this constructor unless needed
// arrange.ProvideKey("server.main", ServerConfig{}),
//
// arrange.If(v.IsSet("server.main")).Then(
// fx.Invoke(
// func(cfg ServerConfig) error {
// // use the configuration to start the server
// },
// ),
// ),
//
// arrange.IfNot(v.IsSet("server.main")).Then(
// fx.Invoke(
// func() {
// log.Println("Main server not started")
// },
// ),
// ),
// )
//
// Note that conditional components do not have to use viper. Any function or series
// of boolean operators may be used:
//
// feature := flag.Bool("feature", false, "this is a feature flag")
// flag.Parse()
// fx.New(
// arrange.If(os.Getenv("feature") != "" || feature).Then(
// fx.Provide(
// func() ConditionalComponent {
// return ConditionalComponent{}
// },
// ),
// fx.Invoke(
// func(cc ConditionalComponent) error {
// // start whatever is needed for this conditionally enabled component
// },
// ),
// )
// )
func If(f bool) *Conditional {
if f {
return new(Conditional)
}
return nil
}
// IfNot is the boolean inverse of If
func IfNot(f bool) *Conditional {
if !f {
return new(Conditional)
}
return nil
}