-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopts.go
130 lines (106 loc) · 2.34 KB
/
opts.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package di
// Option represents single option type
type Option func(Options)
// Options represents a target for applying an Option
type Options interface {
Apply(Option)
}
// NamingOption supports setting a name
type NamingOption interface {
SetName(...string)
}
// ReturnOption supports setting a return target
type ReturnOption interface {
SetReturn(...any)
}
// FillingOption supports setting a fill flag
type FillingOption interface {
SetFill(bool)
}
// WithName returns a NamingOption
func WithName(names ...string) Option {
return func(o Options) {
if opt, ok := o.(NamingOption); ok {
opt.SetName(names...)
}
}
}
// WithReturn returns a ReturnOption
func WithReturn(returns ...any) Option {
return func(o Options) {
if opt, ok := o.(ReturnOption); ok {
opt.SetReturn(returns...)
}
}
}
// WithFill returns a FillingOption
func WithFill() Option {
return func(o Options) {
if opt, ok := o.(FillingOption); ok {
opt.SetFill(true)
}
}
}
// options for binding implementations into container
type bindOptions struct {
factory bool
fill bool
names []string
}
func newBindOptions(opts []Option) (out bindOptions) {
for _, o := range opts {
out.Apply(o)
}
return
}
// Apply implements Options interface
func (o *bindOptions) Apply(opt Option) {
opt(o)
}
// SetName implements NamingOption interface
func (o *bindOptions) SetName(names ...string) {
o.names = names
}
// SetFill implements FillingOption interface
func (o *bindOptions) SetFill(f bool) {
o.fill = f
}
// options for resolving abstractions
type resolveOptions struct {
name string
}
func newResolveOptions(opts []Option) (out resolveOptions) {
out.name = DefaultBindName
for _, o := range opts {
out.Apply(o)
}
return
}
// Apply implements Options interface
func (o *resolveOptions) Apply(opt Option) {
opt(o)
}
// SetName implements NamingOption interface
func (o *resolveOptions) SetName(names ...string) {
if len(names) > 0 {
o.name = names[0]
}
}
// options for resolving abstractions
type callOptions struct {
returns []any
}
func newCallOptions(opts []Option) (out callOptions) {
for _, o := range opts {
out.Apply(o)
}
return
}
// Apply implements Options interface
func (o *callOptions) Apply(opt Option) {
opt(o)
}
// SetReturn implements ReturnOption interface
func (o *callOptions) SetReturn(returns ...any) {
o.returns = returns
}