/
doc.go
556 lines (467 loc) · 15.5 KB
/
doc.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*
Copyright 2014 - 2015 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rest
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"reflect"
"regexp"
"sort"
"strings"
"time"
"github.com/hoisie/mustache"
)
type endpoint map[string]interface{}
type field map[string]interface{}
type handlerDoc map[string]string
// templateRenderer is a template which can be rendered as a string.
type templateRenderer interface {
// render will render the template as a string using the provided context values.
render(interface{}) string
}
// templateParser is used to parse a template string into a templateRenderer struct.
type templateParser interface {
// parse will parse the string into a templateRenderer or return an error if the
// template is malformed.
parse(string) (templateRenderer, error)
}
// mustacheRenderer is an implementation of the templateRenderer interface which relies on
// mustache templating.
type mustacheRenderer struct {
*mustache.Template
}
// render will render the template as a string using the provided context values.
func (m *mustacheRenderer) render(context interface{}) string {
return m.Render(context)
}
// mustacheParser is an implementation of the templateParser interface which relies on
// mustache templating.
type mustacheParser struct{}
// parse will parse the string into a templateRenderer or return an error if the template
// is malformed.
func (m *mustacheParser) parse(template string) (templateRenderer, error) {
tpl, err := mustache.ParseString(template)
if err != nil {
return nil, err
}
return &mustacheRenderer{tpl}, nil
}
// docContextGenerator creates template contexts for rendering ResourceHandler documentation.
type docContextGenerator interface {
// generate creates a template context for the provided ResourceHandler.
generate(ResourceHandler, string) (map[string]interface{}, error)
}
// docWriter writes rendered documentation to a persistent medium.
type docWriter interface {
// mkdir creates a directory to store documentation in.
mkdir(string, os.FileMode) error
// write saves the rendered documentation.
write(string, []byte, os.FileMode) error
}
// fsDocWriter is an implementation of the docWriter interface which writes documentation to
// the local file system.
type fsDocWriter struct{}
// mkdir creates a directory to store documentation in.
func (f *fsDocWriter) mkdir(dir string, mode os.FileMode) error {
return os.MkdirAll(dir, mode)
}
// write saves the rendered documentation.
func (f *fsDocWriter) write(file string, data []byte, mode os.FileMode) error {
return ioutil.WriteFile(file, data, mode)
}
// docGenerator produces documentation files for APIs by introspecting ResourceHandlers and
// their Rules.
type docGenerator struct {
templateParser
docContextGenerator
docWriter
}
// newDocGenerator creates a new docGenerator instance which relies on mustache templating.
func newDocGenerator() *docGenerator {
return &docGenerator{
&mustacheParser{},
&defaultContextGenerator{},
&fsDocWriter{},
}
}
// generateDocs creates the HTML documentation for the provided API. The resulting HTML files
// will be placed in the directory specified by the API Configuration. Returns an error if
// generating the documentation failed, nil otherwise.
func (d *docGenerator) generateDocs(api API) error {
dir := api.Configuration().DocsDirectory
if !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
if err := d.mkdir(dir, os.FileMode(0777)); err != nil {
api.Configuration().Logger.Println(err)
return err
}
handlers := api.ResourceHandlers()
docs := map[string][]handlerDoc{}
versions := versions(handlers)
for _, version := range versions {
versionDocs := make([]handlerDoc, 0, len(handlers))
for _, handler := range handlers {
doc, err := d.generateHandlerDoc(handler, version, dir)
if err != nil {
api.Configuration().Logger.Println(err)
return err
} else if doc != nil {
versionDocs = append(versionDocs, doc)
}
}
docs[version] = versionDocs
}
if err := d.generateIndexDocs(docs, versions, dir); err != nil {
api.Configuration().Logger.Println(err)
return err
}
api.Configuration().Debugf("Documentation generated in %s", dir)
return nil
}
// generateIndexDocs creates index files for each API version with documented endpoints.
func (d *docGenerator) generateIndexDocs(docs map[string][]handlerDoc, versions []string,
dir string) error {
tpl, err := d.parse(indexTemplate)
if err != nil {
return err
}
for version, docList := range docs {
rendered := tpl.render(map[string]interface{}{
"handlers": docList,
"version": version,
"versions": versions,
})
if err := d.write(fmt.Sprintf("%sindex_v%s.html", dir, version),
[]byte(rendered), 0644); err != nil {
return err
}
}
return nil
}
// generateHandlerDoc creates a documentation file for the versioned ResourceHandler.
// Returns nil if the handler contains no documented endpoints or has no output fields.
func (d *docGenerator) generateHandlerDoc(handler ResourceHandler, version,
dir string) (handlerDoc, error) {
tpl, err := d.parse(handlerTemplate)
if err != nil {
return nil, err
}
context, err := d.generate(handler, version)
if context == nil || err != nil {
return nil, err
}
rendered := tpl.render(context)
name := handlerTypeName(handler)
file := fileName(name, version)
if err := d.write(fmt.Sprintf("%s%s", dir, file), []byte(rendered), 0644); err != nil {
return nil, err
}
doc := handlerDoc{"name": name, "file": file}
return doc, nil
}
// defaultContextGenerator is an implementation of the docContextGenerator interface.
type defaultContextGenerator struct{}
// generate creates a template context for the provided ResourceHandler.
func (d *defaultContextGenerator) generate(handler ResourceHandler, version string) (
map[string]interface{}, error) {
inputFields := getInputFields(handler.Rules().ForVersion(version))
outputFields := getOutputFields(handler.Rules().ForVersion(version))
if len(inputFields) == 0 && len(outputFields) == 0 {
// Handler has no fields for this version.
return nil, nil
}
index := 0
endpoints := []endpoint{}
if handler.CreateDocumentation() != "" {
endpoints = append(endpoints, endpoint{
"uri": formatURI(handler.CreateURI(), version),
"method": "POST",
"label": "success",
"description": handler.CreateDocumentation(),
"hasInput": true,
"inputFields": inputFields,
"outputFields": outputFields,
"exampleRequest": buildExampleRequest(handler.Rules(), false, version),
"exampleResponse": buildExampleResponse(handler.Rules(), false, version),
"index": index,
})
}
index++
if handler.ReadListDocumentation() != "" {
endpoints = append(endpoints, endpoint{
"uri": formatURI(handler.ReadListURI(), version),
"method": "GET",
"label": "info",
"description": handler.ReadListDocumentation(),
"hasInput": false,
"outputFields": outputFields,
"exampleResponse": buildExampleResponse(handler.Rules(), true, version),
"index": index,
})
}
index++
if handler.ReadDocumentation() != "" {
endpoints = append(endpoints, endpoint{
"uri": formatURI(handler.ReadURI(), version),
"method": "GET",
"label": "info",
"description": handler.ReadDocumentation(),
"hasInput": false,
"outputFields": outputFields,
"exampleResponse": buildExampleResponse(handler.Rules(), false, version),
"index": index,
})
}
index++
if handler.UpdateListDocumentation() != "" {
endpoints = append(endpoints, endpoint{
"uri": formatURI(handler.UpdateListURI(), version),
"method": "PUT",
"label": "warning",
"description": handler.UpdateListDocumentation(),
"hasInput": true,
"inputFields": inputFields,
"outputFields": outputFields,
"exampleRequest": buildExampleRequest(handler.Rules(), true, version),
"exampleResponse": buildExampleResponse(handler.Rules(), true, version),
"index": index,
})
}
index++
if handler.UpdateDocumentation() != "" {
endpoints = append(endpoints, endpoint{
"uri": formatURI(handler.UpdateURI(), version),
"method": "PUT",
"label": "warning",
"description": handler.UpdateDocumentation(),
"hasInput": true,
"inputFields": inputFields,
"outputFields": outputFields,
"exampleRequest": buildExampleRequest(handler.Rules(), false, version),
"exampleResponse": buildExampleResponse(handler.Rules(), false, version),
"index": index,
})
}
index++
if handler.DeleteDocumentation() != "" {
endpoints = append(endpoints, endpoint{
"uri": formatURI(handler.DeleteURI(), version),
"method": "DELETE",
"label": "danger",
"description": handler.DeleteDocumentation(),
"hasInput": false,
"outputFields": outputFields,
"exampleResponse": buildExampleResponse(handler.Rules(), false, version),
"index": index,
})
}
index++
if len(endpoints) == 0 {
// No documented endpoints.
return nil, nil
}
name := handlerTypeName(handler)
context := map[string]interface{}{
"resource": name,
"version": version,
"versions": handlerVersions(handler),
"endpoints": endpoints,
"fileNamePrefix": fileNamePrefix(name),
}
return context, nil
}
// formatURI returns the specified URI replacing templated variable names with their
// human-readable documentation equivalent. It also replaces the version regex with
// the actual version string.
func formatURI(uri, version string) string {
uri = strings.Replace(uri, "{version:[^/]+}", version, -1)
re := regexp.MustCompile("{.*?}")
for _, param := range re.FindAllString(uri, -1) {
uri = replaceURIParam(uri, param)
}
return uri
}
// replaceURIParam replaces the templated variable name with the human-readable
// documentation equivalent, e.g. {foo} is replaced with :foo.
func replaceURIParam(uri, param string) string {
paramName := param[1 : len(param)-1]
return strings.Replace(uri, param, ":"+paramName, -1)
}
// getInputFields returns input field descriptions.
func getInputFields(rules Rules) []field {
rules = rules.Filter(Inbound)
fields := make([]field, 0, rules.Size())
for _, rule := range rules.Contents() {
required := "optional"
if rule.Required {
required = "required"
}
field := field{
"name": rule.Name(),
"required": required,
"type": ruleTypeName(rule, Inbound),
"description": rule.DocString,
}
fields = append(fields, field)
}
return fields
}
// getInputFields returns output field descriptions.
func getOutputFields(rules Rules) []field {
rules = rules.Filter(Outbound)
fields := make([]field, 0, rules.Size())
for _, rule := range rules.Contents() {
field := field{
"name": rule.Name(),
"type": ruleTypeName(rule, Outbound),
"description": rule.DocString,
}
fields = append(fields, field)
}
return fields
}
// ruleTypeName returns the human-readable type name for a Rule.
func ruleTypeName(r *Rule, filter Filter) string {
name := typeToName[r.Type]
nested := r.Rules
if nested != nil && nested.Filter(filter).Size() > 0 {
name = resourceTypeName(nested.ResourceType().String())
if r.Type == Slice {
name = "[]" + name
}
}
return name
}
// resourceTypeName returns the human-readable type name for a resource.
func resourceTypeName(qualifiedName string) string {
i := strings.LastIndex(qualifiedName, ".")
if i < 0 {
return qualifiedName
}
return qualifiedName[i+1 : len(qualifiedName)]
}
// handlerTypeName returns the human-readable type name for a ResourceHandler resource.
func handlerTypeName(handler ResourceHandler) string {
rulesType := handler.Rules().ResourceType()
if rulesType == nil {
return handler.ResourceName()
}
return resourceTypeName(rulesType.String())
}
// fileName returns the constructed HTML file name for the provided name and version.
func fileName(name, version string) string {
return strings.ToLower(fmt.Sprintf("%s_v%s.html", fileNamePrefix(name), version))
}
// fileNamePrefix returns the provided name as lower case with spaces replaced with
// underscores.
func fileNamePrefix(name string) string {
return strings.ToLower(strings.Replace(name, " ", "_", -1))
}
// buildExampleRequest returns a JSON string representing an example endpoint request.
func buildExampleRequest(rules Rules, list bool, version string) string {
return buildExamplePayload(rules, Inbound, list, version)
}
// buildExampleRequest returns a JSON string representing an example endpoint response.
func buildExampleResponse(rules Rules, list bool, version string) string {
return buildExamplePayload(rules, Outbound, list, version)
}
// buildExamplePayload returns a JSON string representing either an example endpoint request
// or response depending on the Filter provided.
func buildExamplePayload(rules Rules, filter Filter, list bool, version string) string {
rules = rules.ForVersion(version).Filter(filter)
if rules.Size() == 0 {
return ""
}
data := map[string]interface{}{}
for _, r := range rules.Contents() {
data[r.Name()] = getExampleValue(r, version)
}
var payload interface{}
payload = data
if list {
payload = []interface{}{data}
}
serialized, err := json.MarshalIndent(payload, "", " ")
if err != nil {
return ""
}
return string(serialized)
}
// getExampleValue returns an example value for the provided Rule.
func getExampleValue(r *Rule, version string) interface{} {
value := r.DocExample
if value != nil {
return value
}
switch r.Type {
case Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64:
return 0
case Float32, Float64:
return 31.5
case String:
return "foo"
case Bool:
return true
case Duration:
return time.Duration(10000)
case Time:
return time.Date(2014, 9, 5, 15, 45, 36, 0, time.UTC)
default:
return getNestedExampleValue(r, version)
}
}
// getNestedExampleValue returns an example value for a nested Rule value.
func getNestedExampleValue(r *Rule, version string) interface{} {
if r.Rules == nil {
switch r.Type {
case Map:
return map[string]interface{}{}
case Slice:
return []interface{}{}
default:
return nil
}
}
ptr := reflect.New(r.Rules.ResourceType())
value := applyOutboundRules(ptr.Elem().Interface(), r.Rules, version)
if r.Type == Slice {
value = []interface{}{value}
}
return value
}
// versions returns a slice containing all versions specified by the provided
// ResourceHandlers.
func versions(handlers []ResourceHandler) []string {
versionMap := map[string]bool{}
for _, handler := range handlers {
for _, rule := range handler.Rules().Contents() {
for _, version := range rule.Versions {
versionMap[version] = true
}
}
}
versions := make([]string, 0, len(versionMap))
for version := range versionMap {
versions = append(versions, version)
}
sort.Strings(versions)
return versions
}
// handlerVersions returns a slice containing all versions specified by the provided
// ResourceHandler.
func handlerVersions(handler ResourceHandler) []string {
return versions([]ResourceHandler{handler})
}