forked from shikanon/backTrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registStrategy.go
49 lines (45 loc) · 1.16 KB
/
registStrategy.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
package backTrace
import (
"errors"
"fmt"
"reflect"
"sync"
)
type StrategyRegister struct {
Value sync.Map
Names []string
}
func (r *StrategyRegister) Regist(s interface{}) {
var name string
typeStrategy := reflect.TypeOf(s)
valueStrategy := reflect.ValueOf(s)
name = typeStrategy.Name()
for i := 0; i < typeStrategy.NumField(); i++ {
field := typeStrategy.Field(i)
fieldValue := reflect.Indirect(valueStrategy).FieldByName(field.Name)
name = name + "/" + fmt.Sprintf("%s:%v", string(field.Name[0]), fieldValue)
}
r.Names = append(r.Names, name)
fmt.Println(name)
r.Value.Store(name, s)
}
func (r *StrategyRegister) Load(name string) (strategy Strategy, err error) {
value, ok := r.Value.Load(name)
if !ok {
return nil, errors.New("Loaded failed! " + name + " is not in StrategyRegister.")
}
switch strag := value.(type) {
case BreakOutStrategyBuy:
strategy = &strag
case BreakOutStrategySell:
strategy = &strag
case KDJtStrategyBuy:
strategy = &strag
case KDJtStrategySell:
strategy = &strag
}
if strategy == nil {
return nil, errors.New("Error type in StrategyRegister! is not strategy, so can't find it!")
}
return strategy, nil
}