-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_builder.go
72 lines (62 loc) · 2.04 KB
/
option_builder.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
// Copyright 2022 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package di
type (
// BuilderOption is specified for NewBuilder option interface.
BuilderOption interface {
applyBuilderOption(*builder) error
}
builderOptionFunc func(*builder) error
)
// provideOptionFunc implements the BuilderOption interface.
var _ BuilderOption = (*builderOptionFunc)(nil)
func (fn builderOptionFunc) applyBuilderOption(builder *builder) error {
return fn(builder)
}
// BuilderOptions allow to organize logical groups of builder options.
//
// var builder = di.NewBuilder(
// di.BuilderOptions(
// di.Provide(NewBarController),
// di.Provide(NewBazController),
// ),
// di.BuilderOptions(
// di.Provide(NewMuxServer),
// di.Provide(NewServer),
// ),
// )
func BuilderOptions(options ...BuilderOption) BuilderOption {
return builderOptionFunc(func(b *builder) (err error) {
for _, o := range options {
if err = o.applyBuilderOption(b); err != nil {
return err
}
}
return nil
})
}
// Add is builder constructor option.
// This is a syntax sugar for builder constructor usage.
func Add(value Value, options ...AddOption) BuilderOption {
var option = caller(1)
return builderOptionFunc(func(b *builder) error {
return b.Add(value, append([]AddOption{option}, options...)...)
})
}
// Autowire is builder constructor option.
// This is a syntax sugar for builder constructor usage.
func Autowire(value Type, options ...ProvideOption) BuilderOption {
var option = caller(1)
return builderOptionFunc(func(b *builder) error {
return b.Autowire(value, append([]ProvideOption{option}, options...)...)
})
}
// Provide is builder constructor option.
// This is a syntax sugar for builder constructor usage.
func Provide(value Constructor, options ...ProvideOption) BuilderOption {
var option = caller(1)
return builderOptionFunc(func(b *builder) error {
return b.Provide(value, append([]ProvideOption{option}, options...)...)
})
}