-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
52 lines (43 loc) · 1.29 KB
/
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
package factory
// Builder is a struct that implements builder pattern to create a new factory
type Builder struct {
proto interface{}
fGens []FieldGenFunc
}
// ForBuilder is an interface with a single method `For` to bind
// generator function to fields.
type ForBuilder interface {
For(field ...string) *Builder
}
type forBuilder struct {
g GeneratorFunc
b *Builder
}
func (f *forBuilder) For(fields ...string) *Builder {
return f.b.WithGen(f.g, fields...)
}
// NewBuilder allocates a new factory builder
func NewBuilder(proto interface{}) *Builder {
return &Builder{proto: proto, fGens: []FieldGenFunc{}}
}
// WithGen adds new gennerator to builder
func (b *Builder) WithGen(g GeneratorFunc, fields ...string) *Builder {
b.fGens = append(b.fGens, WithGen(g, fields...))
return b
}
// Use accepts generator function arguments and returns a ForBuilder interface
// with a single method `For` to bind generator to struct fiends
func (b *Builder) Use(i interface{}, args ...interface{}) ForBuilder {
return &forBuilder{
g: NewGenerator(i, args...),
b: b,
}
}
// And is synonim for Use
func (b *Builder) And(i interface{}, args ...interface{}) ForBuilder {
return b.Use(i, args...)
}
// Build create a new factory
func (b *Builder) Build() *Factory {
return NewFactory(b.proto, b.fGens...)
}