/
factory.go
61 lines (54 loc) · 1.51 KB
/
factory.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
package backend
import (
"fmt"
"gopkg.in/yaml.v2"
)
func InstanceFactory(typeStr string, opts map[string]interface{}) IAbstractBackend {
switch typeStr {
case MemoryType.String():
return NewMemoryBackend()
case RedisType.String():
return NewRedisBackend(ExtractRedisOpts(opts))
default:
panic(fmt.Sprintf("backend %s not supported", typeStr))
}
}
const RedisOptsKey = "redis"
func ExtractRedisOpts(opts map[string]interface{}) *RedisOpts {
details := make(map[Namespace]RedisConnectionDetails)
namespacesIf, ok := opts[RedisOptsKey]
if !ok {
panic("no redis opts")
}
namespaces := namespacesIf.([]interface{}) // allow to panic, converting should succeed, panic otherwise
for idx, namespace := range namespaces {
// namespace is of type map[interface{}]interface{}, convert back to yaml before unmarshalling again to
// the correct RedisConnectionDetails type
yamlBytes, err := yaml.Marshal(namespace)
if err != nil {
panic(err)
}
var connectionDetails RedisConnectionDetails
err = yaml.Unmarshal(yamlBytes, &connectionDetails)
if err != nil {
panic(err)
}
details[Namespace(idx)] = connectionDetails
}
if len(details) == 0 {
panic("no namespaces found")
}
return &RedisOpts{
details,
}
}
func StrategyInstanceFactory(typeStr string, opts map[string]interface{}) AbstractStrategy {
switch typeStr {
case SimpleStrategyType.String():
fallthrough
case "": // empty
return NewSimpleStrategy()
default:
panic(fmt.Sprintf("backend strategy %s not supported", typeStr))
}
}