forked from leeor/hammock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesigndoc.go
353 lines (238 loc) · 7.85 KB
/
designdoc.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package hammock
import (
"fmt"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"github.com/mikebell-org/go-couchdb"
)
type viewMapReduceFunctions struct {
MapFunc string `json:"map,omitempty"`
ReduceFunc string `json:"reduce,omitempty"`
}
type designDocViews map[string]*viewMapReduceFunctions
type designDocFunctions map[string]string
type designDocument struct {
couchdb.BasicDocumentWithMtime
Name string `json:"-"`
Language string `json:"language,omitempty"`
Views designDocViews `json:"views,omitempty"`
Shows designDocFunctions `json:"shows,omitempty"`
Lists designDocFunctions `json:"lists,omitempty"`
Updates designDocFunctions `json:"updates,omitempty"`
Filters designDocFunctions `json:"filters,omitempty"`
Validate string `json:"validate_doc_update,omitempty"`
}
func readFileContents(path string, contents *string) error {
if file, err := os.Open(path); err != nil {
return err
} else {
file_size, err := file.Seek(0, os.SEEK_END)
if err != nil {
return err
}
buf := make([]byte, file_size)
if _, err := file.ReadAt(buf, 0); err != nil {
return err
}
*contents = string(buf)
}
return nil
}
func newDesignDocument(name string) *designDocument {
return &designDocument {
Name: name,
Language: "javascript",
Views: designDocViews{},
Shows: designDocFunctions{},
Lists: designDocFunctions{},
Updates: designDocFunctions{},
Filters: designDocFunctions{},
Validate: "",
}
}
// http://play.golang.org/p/0lb3Hg8nT1
func keys(m interface{}) interface{} {
mval := reflect.ValueOf(m)
key_type := reflect.TypeOf(m).Key()
keys := reflect.MakeSlice(reflect.SliceOf(key_type), 0, mval.Len())
for _, key := range mval.MapKeys() {
keys = reflect.Append(keys, key)
}
return keys.Interface()
}
func (doc *designDocument) readViews(views_root string) error {
err := filepath.Walk(views_root, func(path string, f os.FileInfo, err error) error {
if match, _ := filepath.Match(views_root+"/*/*.js", path); match {
// got ourselves a view function
parts := strings.Split(path, string(filepath.Separator))
num_parts := len(parts)
view_name := parts[num_parts-2]
if _, ok := doc.Views[view_name]; !ok {
doc.Views[view_name] = &viewMapReduceFunctions{}
}
view := doc.Views[view_name]
if parts[num_parts-1] == "map.js" {
if err := readFileContents(path, &view.MapFunc); err != nil {
return err
}
} else if parts[num_parts-1] == "reduce.js" {
if err := readFileContents(path, &view.ReduceFunc); err != nil {
return err
}
}
}
return nil
})
return err
}
func (doc *designDocument) readValidateFunction(root string) error {
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if match, _ := filepath.Match(root+"/*.js", path); match {
if err := readFileContents(path, &doc.Validate); err != nil {
return err
}
}
return nil
})
return err
}
func (ddf designDocFunctions) readFunctions(root string) error {
err := filepath.Walk(root, func(path string, f os.FileInfo, err error) error {
if match, _ := filepath.Match(root+"/*.js", path); match {
parts := strings.Split(path, string(filepath.Separator))
num_parts := len(parts)
func_name := strings.TrimSuffix(parts[num_parts-1], ".js")
var f string
if err := readFileContents(path, &f); err != nil {
return err
}
ddf[func_name] = f
}
return nil
})
return err
}
func (doc *designDocument) loadFromDisk(document_root string) error {
err := filepath.Walk(document_root, func(path string, f os.FileInfo, err error) error {
if match, _ := filepath.Match(document_root+"/views", path); match && f.IsDir() {
if err := doc.readViews(path); err != nil {
return err
}
return filepath.SkipDir
} else if match, _ := filepath.Match(document_root+"/shows", path); match && f.IsDir() {
if err := doc.Shows.readFunctions(path); err != nil {
return err
}
return filepath.SkipDir
} else if match, _ := filepath.Match(document_root+"/lists", path); match && f.IsDir() {
if err := doc.Lists.readFunctions(path); err != nil {
return err
}
return filepath.SkipDir
} else if match, _ := filepath.Match(document_root+"/updates", path); match && f.IsDir() {
if err := doc.Updates.readFunctions(path); err != nil {
return err
}
return filepath.SkipDir
} else if match, _ := filepath.Match(document_root+"/filters", path); match && f.IsDir() {
if err := doc.Filters.readFunctions(path); err != nil {
return err
}
return filepath.SkipDir
} else if match, _ := filepath.Match(document_root+"/validate", path); match && f.IsDir() {
if err := doc.readValidateFunction(path); err != nil {
return err
}
return filepath.SkipDir
}
return nil
})
return err
}
func updateDesignDocFunctions(this designDocFunctions, other designDocFunctions, func_type string) (updated bool, changes []string) {
// compare update functions
this_updates := sort.StringSlice(keys(this).([]string))
this_updates.Sort()
other_updates := sort.StringSlice(keys(other).([]string))
other_updates.Sort()
for _, name := range this_updates {
if _, ok := other[name]; !ok {
changes = append(changes, fmt.Sprintf("Function %v/%v needs to be deleted", func_type, name))
delete(this, name)
updated = true
break
}
if this[name] != other[name] {
changes = append(changes, fmt.Sprintf("Function %v/%v is out of date", func_type, name))
this[name] = other[name]
updated = true
}
}
for _, name := range other_updates {
if _, ok := this[name]; !ok {
changes = append(changes, fmt.Sprintf("Function %v/%v is missing", func_type, name))
this[name] = other[name]
updated = true
}
}
return
}
func (this *designDocument) update(other *designDocument) (updated bool, changes []string) {
// TODO: handle designDocument.Language
updated = false
// compare views
this_views := sort.StringSlice(keys(this.Views).([]string))
this_views.Sort()
other_views := sort.StringSlice(keys(other.Views).([]string))
other_views.Sort()
for _, name := range this_views {
if _, ok := other.Views[name]; !ok {
changes = append(changes, fmt.Sprintf("View %v/_view/%v needs to be deleted", this.Name, name))
delete(this.Views, name)
updated = true
continue
}
if this.Views[name].MapFunc != other.Views[name].MapFunc {
changes = append(changes, fmt.Sprintf("Map function for view %v/_view/%v is out of date", this.Name, name))
this.Views[name].MapFunc = other.Views[name].MapFunc
updated = true
}
if this.Views[name].ReduceFunc != other.Views[name].ReduceFunc {
changes = append(changes, fmt.Sprintf("Reduce function for view %v/_view/%v is out of date", this.Name, name))
this.Views[name].ReduceFunc = other.Views[name].ReduceFunc
updated = true
}
}
for _, name := range other_views {
if _, ok := this.Views[name]; !ok {
changes = append(changes, fmt.Sprintf("View %v/_view/%v is missing", this.Name, name))
this.Views[name] = other.Views[name]
updated = true
}
}
if u, c := updateDesignDocFunctions(this.Shows, other.Shows, fmt.Sprintf("%v/_shows", this.Name)); u {
updated = true
changes = append(changes, c...)
}
if u, c := updateDesignDocFunctions(this.Lists, other.Lists, fmt.Sprintf("%v/_lists", this.Name)); u {
updated = true
changes = append(changes, c...)
}
if u, c := updateDesignDocFunctions(this.Filters, other.Filters, fmt.Sprintf("%v/_filters", this.Name)); u {
updated = true
changes = append(changes, c...)
}
if u, c := updateDesignDocFunctions(this.Updates, other.Updates, fmt.Sprintf("%v/_updates", this.Name)); u {
updated = true
changes = append(changes, c...)
}
if this.Validate != other.Validate {
changes = append(changes, fmt.Sprintf("Validate function for %v design is out of date", this.Name))
this.Validate = other.Validate
updated = true
}
return
}