forked from goadesign/gorma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storagegroup.go
104 lines (91 loc) · 2.76 KB
/
storagegroup.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
package gorma
import (
"fmt"
"sort"
"github.com/goadesign/goa/design"
"github.com/goadesign/goa/dslengine"
)
// NewStorageGroupDefinition returns an initialized
// StorageGroupDefinition.
func NewStorageGroupDefinition() *StorageGroupDefinition {
m := &StorageGroupDefinition{
RelationalStores: make(map[string]*RelationalStoreDefinition),
}
return m
}
// IterateStores runs an iterator function once per Relational Store in the
// StorageGroup's Store list.
func (sd *StorageGroupDefinition) IterateStores(it StoreIterator) error {
if sd == nil {
return nil
}
if sd.RelationalStores != nil {
names := make([]string, len(sd.RelationalStores))
i := 0
for n := range sd.RelationalStores {
names[i] = n
i++
}
sort.Strings(names)
for _, n := range names {
if err := it(sd.RelationalStores[n]); err != nil {
return err
}
}
}
return nil
}
// Context returns the generic definition name used in error messages.
func (sd StorageGroupDefinition) Context() string {
if sd.Name != "" {
return fmt.Sprintf("StorageGroup %#v", sd.Name)
}
return "unnamed Storage Group"
}
// DSL returns this object's DSL.
func (sd StorageGroupDefinition) DSL() func() {
return sd.DefinitionDSL
}
// Children returns a slice of this objects children.
func (sd StorageGroupDefinition) Children() []dslengine.Definition {
var stores []dslengine.Definition
for _, s := range sd.RelationalStores {
stores = append(stores, s)
}
return stores
}
// DSLName is displayed to the user when the DSL executes.
func (sd *StorageGroupDefinition) DSLName() string {
return "Gorma storage group"
}
// DependsOn return the DSL roots the Gorma DSL root depends on, that's the goa API DSL.
func (sd *StorageGroupDefinition) DependsOn() []dslengine.Root {
return []dslengine.Root{design.Design, design.GeneratedMediaTypes}
}
// IterateSets goes over all the definition sets of the StorageGroup: the
// StorageGroup definition itself, each store definition, models and fields.
func (sd *StorageGroupDefinition) IterateSets(iterator dslengine.SetIterator) {
// First run the top level StorageGroup
iterator([]dslengine.Definition{sd})
sd.IterateStores(func(store *RelationalStoreDefinition) error {
iterator([]dslengine.Definition{store})
store.IterateModels(func(model *RelationalModelDefinition) error {
iterator([]dslengine.Definition{model})
model.IterateFields(func(field *RelationalFieldDefinition) error {
iterator([]dslengine.Definition{field})
return nil
})
model.IterateBuildSources(func(bs *BuildSource) error {
iterator([]dslengine.Definition{bs})
return nil
})
return nil
})
return nil
})
}
// Reset resets the storage group to pre DSL execution state.
func (sd *StorageGroupDefinition) Reset() {
n := NewStorageGroupDefinition()
*sd = *n
}