-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
73 lines (62 loc) · 1.78 KB
/
controller.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
package mooncake
import (
"fmt"
"log"
"reflect"
)
var (
ErrInvalidNumberOfReturns = func(key string, expected, got int) error {
return fmt.Errorf("invalid number of returns for %s. expected=%v got=%v", key, expected, got)
}
ErrInvalidTypeOfReturn = func(key string, expected, got interface{}) error {
return fmt.Errorf("invalid type of return for %s. expected=%v got=%v", key, expected, got)
}
)
type AgentController struct {
key string
returnValues []ReturnDetail
lifeTime MooncakeLifetime
lifeTimeCount int
}
type ReturnDetail struct {
Value interface{}
DType reflect.Type
}
func NewAgentController(key string, imp reflect.Type) AgentController {
newAgentController := new(AgentController)
newAgentController.key = key
newAgentController.lifeTime = LT_ONE_CALL
newAgentController.lifeTimeCount = 1
for x := 0; x < imp.NumOut(); x++ {
newAgentController.returnValues = append(newAgentController.returnValues, ReturnDetail{
Value: nil,
DType: imp.Out(x),
})
}
return *newAgentController
}
func (ag *AgentController) SetReturn(args ...interface{}) *AgentController {
if len(ag.returnValues) != len(args) {
log.Fatalln(ErrInvalidNumberOfReturns(ag.key,
len(ag.returnValues), len(args)).Error())
}
for idx, arg := range args {
if reflect.TypeOf(arg) != ag.returnValues[idx].DType {
if ag.returnValues[idx].DType != reflect.TypeOf((*error)(nil)).Elem() {
log.Fatalln(ErrInvalidTypeOfReturn(ag.key,
ag.returnValues[idx].DType, reflect.TypeOf(arg)).Error())
}
}
ag.returnValues[idx].Value = arg
}
return ag
}
func (ag *AgentController) AnyTime() *AgentController {
ag.lifeTime = LT_ANY_TIME
return ag
}
func (ag *AgentController) Repeat(count int) *AgentController {
ag.lifeTime = LT_REPEAT
ag.lifeTimeCount = count
return ag
}