/
engine.go
53 lines (41 loc) · 1 KB
/
engine.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
package tester
import (
"github.com/pkg/errors"
"go.uber.org/fx"
)
type EngineConstructor func() Engine
type EngineDescriptor struct {
Type string
Constructor EngineConstructor
}
type EngineFactoryIn struct {
fx.In
Descriptors []EngineDescriptor `group:"engine_descriptor"`
}
type EngineFactory interface {
Create(name string) (Engine, error)
}
type engineFactory struct {
constructors map[string]EngineConstructor
}
func NewFxEngineFactory(in EngineFactoryIn) EngineFactory {
f := &engineFactory{
constructors: make(map[string]EngineConstructor),
}
for _, descriptor := range in.Descriptors {
f.constructors[descriptor.Type] = descriptor.Constructor
}
return f
}
func (f *engineFactory) Create(name string) (Engine, error) {
constructor, ok := f.constructors[name]
if !ok {
return nil, errors.Errorf("assertion %s is not found", name)
}
return constructor(), nil
}
type Engine interface {
Configure(data interface{}) error
GetTestCase2List() TestCase2List
CreateRunner() Runner
}