forked from gospackler/caddyshack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (95 loc) · 2.98 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package caddyshack
import (
"errors"
"github.com/bushwood/caddyshack/adapter"
"github.com/bushwood/caddyshack/collection"
"github.com/bushwood/caddyshack/model"
"github.com/bushwood/caddyshack/resource"
)
// Caddyshack is the core struct managing models and adapters
type Caddyshack struct {
Models map[string]model.Definition
Adapters map[string]adapter.Definition
Collections map[string]collection.Definition
}
// New creates a caddyshack object
func New() (Caddyshack, error) {
cs := Caddyshack{}
cs.Models = make(map[string]model.Definition)
cs.Adapters = make(map[string]adapter.Definition)
cs.Collections = make(map[string]collection.Definition)
return cs, nil
}
// LoadModels loads a map of models into the instance
func (cs *Caddyshack) LoadModels(models map[string]model.Definition) (Caddyshack, error) {
for _, v := range models {
_, err := cs.LoadModel(v)
if err != nil {
return *cs, err
}
}
return *cs, nil
}
// LoadModel loads a single model into the instance
func (cs *Caddyshack) LoadModel(model model.Definition) (Caddyshack, error) {
if model.Name == "" {
return *cs, errors.New("model .Name cannot be empty")
}
cs.Models[model.Name] = model
return *cs, nil
}
// LoadAdapters loads a map of adapters into the instance
func (cs *Caddyshack) LoadAdapters(adps map[string]adapter.Definition, rscs map[string]resource.Definition) (Caddyshack, error) {
for _, adp := range adps {
name := adp.GetName()
if name == "" {
return *cs, errors.New("adapter .Name cannot be empty")
}
_, err := cs.LoadAdapter(adp, rscs[name])
if err != nil {
return *cs, err
}
}
return *cs, nil
}
// LoadAdapter loads a single adapter into the instance
func (cs *Caddyshack) LoadAdapter(adp adapter.Definition, rsc resource.Definition) (Caddyshack, error) {
name := adp.GetName()
if name == "" {
return *cs, errors.New("adapter .Name cannot be empty")
}
cs.Adapters[name] = adp
cs.Adapters[name].SetConfig(rsc)
return *cs, nil
}
// Build generates collections based on the current state of the Caddyshack instance
func (cs *Caddyshack) Build() (Caddyshack, error) {
for _, m := range cs.Models {
a := cs.Adapters[m.Adapter]
if a == nil {
return *cs, errors.New("model [" + m.Name + "] attempted to use unknown adapter [" + m.Adapter + "]")
}
_, err := cs.BuildCollection(m, a)
if err != nil {
return *cs, err
}
}
return *cs, nil
}
// BuildCollection generates a single collection using the model and adapter provided
func (cs *Caddyshack) BuildCollection(m model.Definition, a adapter.Definition) (Caddyshack, error) {
coll, err := a.BuildCollection(m)
if err != nil {
return *cs, err
}
cs.Collections[m.Name] = coll
return *cs, nil
}
// Open a connection form the adapter connection pool to the specified collection
func (cs *Caddyshack) Open(collName string) (collection.Definition, error) {
c := cs.Collections[collName]
if c.GetName() == "" {
return c, errors.New("attempted to open unknown collection [" + collName + "]")
}
return c, nil
}