-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocgen.go
649 lines (546 loc) · 17.3 KB
/
docgen.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/olekukonko/tablewriter"
)
type DocumentationGenerator struct {
DbConnConfig *pgx.ConnConfig
Schema Schema
SearchPath string
FilterQueryName string
SortQueryName string
LimitQueryName string
}
func (g *DocumentationGenerator) GenerateDocumentation(outputPath string) {
var err error
ctx := context.Background()
routeParser := regexp.MustCompile("/[:*][^/]+")
tblBorders := tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}
typeDescriptions := make(map[string]string)
var db *pgx.Conn
db, err = pgx.ConnectConfig(ctx, g.DbConnConfig)
if err != nil {
log.Fatalln("Could not connect to database:", err)
}
var tx pgx.Tx
tx, err = db.Begin(ctx)
if err != nil {
log.Fatalln("Could not begin transaction:", err)
}
defer tx.Rollback(ctx)
var routes []*Route
routes, err = g.Schema.LoadRoutes(ctx, tx, g.SearchPath)
if err != nil {
log.Fatalln("Could not load routes:", err)
}
var f *os.File
f, err = os.Create(outputPath)
if err != nil {
log.Fatalln("Could not create output file:", err)
}
defer f.Close()
wtr := bufio.NewWriter(f)
defer wtr.Flush()
wtr.WriteString("# API Specification\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("## Protocol\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("This web service implements a RESTful compatible interface.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("### Requests\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("A resource is identified by its URL. Those can contain several arguments, for which two formats are available:\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("- `/:arg_name`, argument `arg_name` will match a single path segment\r\n")
wtr.WriteString("- `/*arg_name`, placed at the end of a route, will match the end of the path\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("When sending a request, the expected result format is specified by appending an extension `.ext` at the end of the URL. This is compulsory. Several formats are available:\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("- `.json`\r\n")
wtr.WriteString("- `.xlsx`\r\n")
wtr.WriteString("- `.csv`\r\n")
wtr.WriteString("- `.bin`\r\n")
wtr.WriteString("- Other formats may be available depending on route.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("Arguments which are not expected in the URL must be provided in the query string for *get* and *delete* requests, or as the content of the HTTP requests for *post* and *put*.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("The format of arguments in the querystring is the same used for literals in PostgreSQL.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("The recommended content type of *post* and *put* requests is `application/json`. In this case, the content of the HTTP request is either a JSON object with parameters, or an array of sub objects for batch requests. When using batch mode, several result sets will be returned. If no content type is specified, `application/x-www-form-urlencoded` is assumed.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("In addition, *get*, *delete*, and *put* requests on *relations* may accept a filter condition, sort order (except *put*), and limit (except *put*) in the query string. Those follow the [queryme](https://github.com/debackerl/queryme) format:\r\n")
wtr.WriteString("- `")
wtr.WriteString(g.FilterQueryName)
wtr.WriteString("`, filter condition\r\n")
wtr.WriteString("- `")
wtr.WriteString(g.SortQueryName)
wtr.WriteString("`, sort order\r\n")
wtr.WriteString("- `")
wtr.WriteString(g.LimitQueryName)
wtr.WriteString("`, limit\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("### Responses\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("The cache-control and max-age settings are route specific, and will be set accordingly to the specification of routes below.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("*json format*\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("Result sets are encoded as arrays of objects.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("Batches are encoded as arrays.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("*xslx format*\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("Each result set in a batch is saved as a new sheet.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("*csv format*\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("A single result set must be returned. Batch mode is not supported.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("Fields are command separated, and text fields are double-quoted.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("*bin format*\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("Some routes may return binary data.\r\n")
wtr.WriteString("\r\n")
wtr.WriteString("## Resources\r\n\r\n")
routesByUrlPath := make(map[string]*RoutesGroup)
routesGroups := make([]*RoutesGroup, 0, 8)
for _, route := range routes {
var group *RoutesGroup
if match, ok := routesByUrlPath[route.UrlPath]; ok {
group = match
} else {
group = &RoutesGroup{UrlPath: route.UrlPath}
routesByUrlPath[route.UrlPath] = group
routesGroups = append(routesGroups, group)
}
switch route.Method {
case "get":
group.Get = true
case "post":
group.Post = true
case "put":
group.Put = true
case "delete":
group.Delete = true
}
}
sort.Sort(RoutesByUrlPath(routesGroups))
for _, group := range routesGroups {
wtr.WriteString("- `")
wtr.WriteString(group.UrlPath)
wtr.WriteString("`")
if group.Get {
wtr.WriteString(" [get](#")
wtr.WriteString(anchorName("get " + group.UrlPath))
wtr.WriteString(")")
}
if group.Post {
wtr.WriteString(" [post](#")
wtr.WriteString(anchorName("post " + group.UrlPath))
wtr.WriteString(")")
}
if group.Put {
wtr.WriteString(" [put](#")
wtr.WriteString(anchorName("put " + group.UrlPath))
wtr.WriteString(")")
}
if group.Delete {
wtr.WriteString(" [delete](#")
wtr.WriteString(anchorName("delete " + group.UrlPath))
wtr.WriteString(")")
}
wtr.WriteString("\r\n")
}
wtr.WriteString("\r\n## Routes\r\n\r\n")
for route_i, route := range routes {
optionals := make(map[string]struct{})
for _, name := range route.OptionalArguments {
optionals[name] = struct{}{}
}
routeArguments := make(map[string]string)
for _, name := range routeParser.FindAllString(route.UrlPath, -1) {
name = name[2:]
typ := "text"
if t, ok := route.ParametersDeclTypes[name]; ok {
typ = t
}
typ = strings.TrimSuffix(typ, "[]")
routeArguments[name] = typ
}
cacheControl := "private"
maxAge := ""
if route.IsPublic {
cacheControl = "public"
maxAge = fmt.Sprintf("%d sec", route.TTL)
}
if route_i > 0 {
wtr.WriteString("---\r\n\r\n")
}
wtr.WriteString("### ")
wtr.WriteString(route.Method)
wtr.WriteString(" `")
wtr.WriteString(route.UrlPath)
wtr.WriteString("`\r\n\r\n")
if route.Description != "" {
wtr.WriteString(route.Description)
wtr.WriteString("\r\n\r\n")
}
table := tablewriter.NewWriter(wtr)
table.SetAutoWrapText(false)
table.SetBorders(tblBorders)
table.SetCenterSeparator("|")
table.SetHeader([]string{"ID", "Kind", "Cache-Control", "Max-Age"})
table.Append([]string{fmt.Sprintf("%d", route.RouteID), route.ObjectType, cacheControl, maxAge})
table.Render()
wtr.WriteString("\r\n**Arguments**\r\n\r\n")
table = tablewriter.NewWriter(wtr)
table.SetAutoWrapText(false)
table.SetBorders(tblBorders)
table.SetCenterSeparator("|")
table.SetHeader([]string{"Name", "Type", "Optional"})
rows := make(Rows, 0, 8)
for name, typ := range routeArguments {
link, err := describeType(ctx, tx, typ, typeDescriptions)
if err != nil {
log.Fatalln("Error: ", err)
}
rows.Append([]string{"`" + name + "`", link, "false"})
}
if route.ObjectType == "procedure" || route.Method == "post" || route.Method == "put" {
for name, typ := range route.ParametersDeclTypes {
isoptional := IsStringInMap(name, optionals)
isro := IsStringInMap(name, route.ReadOnlyFields)
_, isroutearg := routeArguments[name]
if isro {
if route.Method == "post" && !isoptional && !isroutearg {
log.Fatalf("Route has a read-only field without default value, route_id %d, field %s\n", route.RouteID, name)
}
} else if !isroutearg {
link, err := describeType(ctx, tx, typ, typeDescriptions)
if err != nil {
log.Fatalln("Error: ", err)
}
rows.Append([]string{"`" + name + "`", link, fmt.Sprintf("%t", isoptional)})
}
}
}
sort.Sort(rows)
table.AppendBulk(rows)
table.Render()
wtr.WriteString("\r\n**Result**\r\n\r\n")
switch route.ObjectType {
case "relation":
wtr.WriteString("result set:\r\n\r\n")
table = tablewriter.NewWriter(wtr)
table.SetAutoWrapText(false)
table.SetBorders(tblBorders)
table.SetCenterSeparator("|")
table.SetHeader([]string{"Name", "Type"})
rows = make(Rows, 0, 8)
for name, typ := range route.ParametersDeclTypes {
if _, ok := route.HiddenFields[name]; !ok {
link, err := describeType(ctx, tx, typ, typeDescriptions)
if err != nil {
log.Fatalln("Error: ", err)
}
rows.Append([]string{"`" + name + "`", link})
}
}
sort.Sort(rows)
table.AppendBulk(rows)
table.Render()
case "procedure":
if route.Proretset {
wtr.WriteString("set of ")
}
link, err := describeType(ctx, tx, route.Prorettypname, typeDescriptions)
if err != nil {
log.Fatalln("Error: ", err)
}
wtr.WriteString(link)
wtr.WriteString("\r\n")
}
if route.ContextHeaders.Status == pgtype.Present && len(route.ContextHeaders.Map) > 0 {
wtr.WriteString("\r\n**HTTP Request Headers**\r\n\r\n")
for httpHeaderName := range route.ContextHeaders.Map {
wtr.WriteString("- `")
wtr.WriteString(httpHeaderName)
wtr.WriteString("`\r\n")
}
}
if len(route.AllCookies) > 0 {
wtr.WriteString("\r\n**Cookies**\r\n\r\n")
table = tablewriter.NewWriter(wtr)
table.SetAutoWrapText(false)
table.SetBorders(tblBorders)
table.SetCenterSeparator("|")
table.SetHeader([]string{"Name", "Read/Write", "Domain", "Path", "HTTP Only", "Secure", "Max Age"})
rows = make(Rows, 0, 8)
for _, cookie := range route.AllCookies {
rw := ""
if cookie.Read {
rw += "R"
}
if cookie.Write {
rw += "W"
}
httpOnly := ""
secure := ""
maxAge := ""
if cookie.Write {
httpOnly = fmt.Sprintf("%t", cookie.HttpOnly)
secure = fmt.Sprintf("%t", cookie.Secure)
maxAge = fmt.Sprintf("%d sec", cookie.MaxAge)
}
rows.Append([]string{cookie.Name, rw, cookie.SubDomain.String, cookie.Path.String, httpOnly, secure, maxAge})
}
sort.Sort(rows)
table.AppendBulk(rows)
table.Render()
}
wtr.WriteString("\r\n")
}
wtr.WriteString("\r\n## Types\r\n\r\n")
describedTypes := make([]string, 0, len(typeDescriptions))
for typname, description := range typeDescriptions {
if description != "" {
describedTypes = append(describedTypes, typname)
}
}
sort.Strings(describedTypes)
for _, typname := range describedTypes {
wtr.WriteString(typeDescriptions[typname])
wtr.WriteString("\r\n")
}
}
func describeType(ctx context.Context, tx pgx.Tx, typname string, descriptions map[string]string) (string, error) {
isarray := strings.HasSuffix(typname, "[]")
if isarray {
typname = typname[:len(typname)-2]
}
switch typname {
case "timestamp without time zone", "timestamp with time zone":
typname = "timestamp"
case "jsonb":
typname = "json"
case "serial":
typname = "integer"
case "bigserial":
typname = "bigint"
case "character":
typname = "text"
case "character varying":
typname = "text"
}
description, ok := descriptions[typname]
if !ok {
buf := new(bytes.Buffer)
buf.WriteString("### type `")
buf.WriteString(typname)
buf.WriteString("`\r\n\r\n")
// https://www.postgresql.org/docs/9.6/static/datatype.html
switch typname {
case "boolean":
description = "Boolean value"
case "smallint":
description = "Signed 16-bit integer number"
case "integer":
description = "Signed 32-bit integer number"
case "bigint":
description = "Signed 64-bit integer number"
case "real":
description = "32-bit floating point number"
case "double precision":
description = "64-bit floating point number"
case "numeric", "money":
description = "Decimal number. Encoded as a string in JSON to avoid base-2 floating point approximation error."
case "text":
description = "String value"
case "date":
description = "Date without time. Encoded in yyyy-mm-dd format."
case "timestamp":
description = "Date and time. Encoded in RFC3339 format."
case "hstore":
description = "Object with string keys and string values."
case "json":
description = "JSON value"
case "bytea":
description = "Byte array. Encoded in base64 except for binary HTTP responses."
case "uuid":
description = "Universally unique identifier"
case "inet":
description = "IPv4 or IPv6 host address"
case "cidr":
description = "IPv4 or IPv6 network address"
}
if description != "" {
buf.WriteString(description)
buf.WriteString("\r\n")
description = buf.String()
} else {
descriptions[typname] = ""
typtype, typrelid, typbasetype, pgdesc, err := loadTypeBasics(ctx, tx, typname)
if err != nil {
return "", err
}
if pgdesc.Status == pgtype.Present {
buf.WriteString(pgdesc.String)
buf.WriteString("\r\n\r\n")
}
switch typtype {
case "c":
names, types, err := loadCompositeType(ctx, tx, typname, typrelid)
if err != nil {
return "", err
}
table := tablewriter.NewWriter(buf)
table.SetAutoWrapText(false)
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetHeader([]string{"Name", "Type"})
for i, name := range names {
typ := types[i]
link, err := describeType(ctx, tx, typ, descriptions)
if err != nil {
return "", err
}
table.Append([]string{name, link})
}
table.Render()
description = buf.String()
case "e":
values, err := loadEnumType(ctx, tx, typname)
if err != nil {
return "", err
}
buf.WriteString("Possible values:\r\n\r\n")
for _, value := range values {
buf.WriteString("- `\"")
buf.WriteString(value)
buf.WriteString("\"`\r\n")
}
description = buf.String()
case "d":
if typbasetype.Status == pgtype.Present {
link, err := describeType(ctx, tx, typbasetype.String, descriptions)
if err != nil {
return "", err
}
buf.WriteString("Base type: ")
buf.WriteString(link)
buf.WriteString("\r\n")
description = buf.String()
}
}
}
descriptions[typname] = description
}
suffix := ""
if isarray {
suffix = "[]"
}
if description != "" {
return fmt.Sprintf("[%s](#%s)%s", typname, anchorName("type "+typname), suffix), nil
} else {
return typname + suffix, nil
}
}
func loadCompositeType(ctx context.Context, tx pgx.Tx, typename string, typrelid pgtype.OID) (names []string, types []string, err error) {
rows, err := tx.Query(ctx, `SELECT a.attname, t.typname FROM pg_attribute a INNER JOIN pg_type t ON t.oid = a.atttypid WHERE attrelid = $1 ORDER BY a.attnum`, typrelid)
if err != nil {
return
}
defer rows.Close()
names = make([]string, 0, 4)
types = make([]string, 0, 4)
for rows.Next() {
var name, typ string
if err = rows.Scan(&name, &typ); err != nil {
return
}
names = append(names, name)
types = append(types, typ)
}
return
}
func loadEnumType(ctx context.Context, tx pgx.Tx, typename string) (values []string, err error) {
rows, err := tx.Query(ctx, `SELECT array_agg(enumlabel ORDER BY enumsortorder)::text[] FROM pg_enum WHERE enumtypid = $1::regtype::oid`, typename)
if err != nil {
return
}
defer rows.Close()
values = make([]string, 0, 4)
if rows.Next() {
if err = rows.Scan(&values); err != nil {
return
}
} else {
values = []string{}
}
return
}
func loadTypeBasics(ctx context.Context, tx pgx.Tx, typname string) (typtype string, typrelid pgtype.OID, typbasetype pgtype.Text, description pgtype.Text, err error) {
rows, err := tx.Query(ctx, `SELECT typtype, typrelid, typbasetype::regtype, obj_description(oid) FROM pg_type WHERE typname = $1`, typname)
if err != nil {
return
}
defer rows.Close()
if !rows.Next() {
return
}
if err = rows.Scan(&typtype, &typrelid, &typbasetype, &description); err != nil {
return
}
return
}
func IsStringInMap(x string, xs map[string]struct{}) bool {
_, ok := xs[x]
return ok
}
func anchorName(s string) string {
s = strings.ToLower(s)
s = strings.Replace(s, " ", "-", -1)
return regexp.MustCompile("[^0-9a-z\\-_]").ReplaceAllString(s, "")
}
type RoutesGroup struct {
UrlPath string
Get bool
Post bool
Put bool
Delete bool
}
type RoutesByUrlPath []*RoutesGroup
func (rs RoutesByUrlPath) Len() int {
return len(rs)
}
func (rs RoutesByUrlPath) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
func (rs RoutesByUrlPath) Less(i, j int) bool {
return rs[i].UrlPath < rs[j].UrlPath
}
type Rows [][]string
func (rs *Rows) Append(fields []string) {
*rs = append(*rs, fields)
}
func (rs Rows) Len() int {
return len(rs)
}
func (rs Rows) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
func (rs Rows) Less(i, j int) bool {
return rs[i][0] < rs[j][0]
}