-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathformation.go
174 lines (136 loc) · 2.73 KB
/
formation.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"html/template"
"io/ioutil"
"os"
"sort"
"strings"
yaml "gopkg.in/yaml.v2"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
type Table struct {
Name string
DependsOn string
HashKey string
RangeKey string
Ttl string
Indexes []Index `yaml:"-"`
RawKey string `yaml:"key"`
RawIndexes map[string]string `yaml:"indexes"`
}
type Index struct {
Name string
HashKey string
RangeKey string
}
type Tables map[string]Table
func run() error {
data, err := ioutil.ReadFile("tables.yml")
if err != nil {
return err
}
var tm map[string]Table
if err := yaml.Unmarshal(data, &tm); err != nil {
return err
}
ts := []Table{}
for k, v := range tm {
v.Name = k
p := strings.Split(v.RawKey, ",")
v.HashKey = strings.TrimSpace(p[0])
if len(p) > 1 {
v.RangeKey = strings.TrimSpace(p[1])
}
v.Indexes = []Index{}
for k, vi := range v.RawIndexes {
i := Index{Name: k}
p := strings.Split(vi, ",")
i.HashKey = strings.TrimSpace(p[0])
if len(p) > 1 {
i.RangeKey = strings.TrimSpace(p[1])
}
v.Indexes = append(v.Indexes, i)
}
sort.Slice(v.Indexes, func(i, j int) bool { return v.Indexes[i].Name < v.Indexes[j].Name })
ts = append(ts, v)
}
sort.Slice(ts, func(i, j int) bool { return ts[i].Name < ts[j].Name })
for i := range ts {
if i > 0 {
ts[i].DependsOn = ts[i-1].Name
}
}
t, err := template.New("formation").Funcs(helpers()).ParseFiles("formation.json.tmpl")
if err != nil {
return err
}
params := map[string]interface{}{
"Tables": ts,
}
// fmt.Printf("multiplier = %+v\n", multiplier)
// fmt.Printf("t = %+v\n", t)
// fmt.Printf("params = %+v\n", params)
if err := t.Execute(os.Stdout, params); err != nil {
return err
}
return nil
}
func (t *Table) Attributes() []string {
ah := map[string]bool{}
ah[t.HashKey] = true
ah[t.RangeKey] = true
for _, i := range t.Indexes {
ah[i.HashKey] = true
ah[i.RangeKey] = true
}
as := []string{}
for k := range ah {
if k != "" {
as = append(as, k)
}
}
sort.Strings(as)
return as
}
func helpers() map[string]interface{} {
return map[string]interface{}{
"type": func(s string) string {
switch s {
case "github-id":
return "N"
default:
return "S"
}
},
"times": func(i, j int) int {
return i * j
},
"upper": func(name string) string {
if name == "" {
return ""
}
us := strings.ToUpper(name[0:1]) + name[1:]
for {
i := strings.Index(us, "-")
if i == -1 {
break
}
s := us[0:i]
if len(us) > i+1 {
s += strings.ToUpper(us[i+1 : i+2])
}
if len(us) > i+2 {
s += us[i+2:]
}
us = s
}
return us
},
}
}