forked from anexia-it/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtyk_doc.go
512 lines (434 loc) · 12.3 KB
/
xtyk_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
package oas
//go:generate go test -run=TestExtractDocFromXTyk . -count 1 -v -timeout 7s -x-tyk-dump-doc
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path"
"reflect"
"runtime"
"strings"
)
// XTykDoc is a list of information for exported struct type info,
// starting from the root struct declaration(XTykGateway).
type XTykDoc []*StructInfo
func (x *XTykDoc) append(newInfo *StructInfo) int {
*x = append(*x, newInfo)
return len(*x)
}
// StructInfo holds ast field information for the docs generator.
type StructInfo struct {
// Name is struct go name.
Name string
// Fields holds information of the fields, if this object is a struct.
Fields []*FieldInfo `json:"fields,omitempty"`
// fileSet holds a token.FileSet, used to resolve symbols to file:line
fileSet *token.FileSet
// structObj is the raw ast.StructType value, private.
structObj *ast.StructType
}
// FieldInfo holds details about a field.
type FieldInfo struct {
// Doc is field docs. comments that are not part of docs are excluded.
Doc string `json:"doc"`
// JSONName is the corresponding json name of the field.
JSONName string `json:"json_name"`
// JSONType valid json type if it was found
JSONType string `json:"json_type"`
// GoPath is the go path of this field starting from root object
GoPath string `json:"go_path"`
// MapKey is the map key type, if this field is a map
MapKey string `json:"map_key,omitempty"`
// IsArray reports if this field is an array.
IsArray bool `json:"is_array"`
// fileSet holds a token.FileSet, used to resolve symbols to file:line
fileSet *token.FileSet
}
// FieldDocError holds a list of errors.
type FieldDocError struct {
errs []string
}
// Error implements the error interface.
func (err *FieldDocError) Error() string {
return strings.Join(err.errs, "\n")
}
// WriteError appends an error message to the error list.
func (err *FieldDocError) WriteError(errMsg string) {
err.errs = append(err.errs, errMsg)
}
// Empty returns true if there are no errors in the list.
func (err *FieldDocError) Empty() bool {
return len(err.errs) == 0
}
// ExtractDocFromXTyk returns documentation associated with XTykAPIGateway struct or error.
// if err is of type *FieldDocError, documentation may not be empty.
func ExtractDocFromXTyk() (XTykDoc, error) {
_, thisFilePath, _, ok := runtime.Caller(0)
if !ok {
return nil, errors.New("missing caller information")
}
fileSet := token.NewFileSet()
pkgs, err := parser.ParseDir(fileSet, path.Dir(thisFilePath), filterXTykGoFile, parser.ParseComments)
if err != nil {
return nil, err
}
const (
rootStructName = "XTykAPIGateway"
rootJSONName = "x-tyk-gateway"
requiredPkgName = "oas"
)
if _, ok = pkgs[requiredPkgName]; !ok {
return nil, fmt.Errorf("required package %s", requiredPkgName)
}
p := newObjParser(pkgs[requiredPkgName])
rootStructInfo := &StructInfo{
Name: rootJSONName,
fileSet: fileSet,
structObj: p.globals[rootStructName].(*ast.StructType),
}
p.parse(rootJSONName, rootJSONName, rootStructInfo)
if p.errList.Empty() {
return p.info, nil
}
return p.info, p.errList
}
type objParser struct {
info XTykDoc
globals map[string]ast.Expr // exported global types
visited map[string]*StructInfo // avoid re-visiting struct type
pkg *ast.Package
errList *FieldDocError
}
func newObjParser(pkg *ast.Package) *objParser {
p := &objParser{
info: XTykDoc{},
visited: map[string]*StructInfo{},
errList: &FieldDocError{},
pkg: pkg,
}
p.parseGlobalExpr()
return p
}
func (p *objParser) parseGlobalExpr() {
p.globals = map[string]ast.Expr{}
for _, fileObj := range p.pkg.Files {
if fileObj.Scope == nil {
continue
}
for objName, obj := range fileObj.Scope.Objects {
if decl, ok := obj.Decl.(*ast.TypeSpec); ok && ast.IsExported(objName) {
switch obj := decl.Type.(type) {
case *ast.StructType, *ast.ArrayType, *ast.MapType:
p.globals[objName] = obj
}
}
}
}
}
func (p *objParser) parse(goPath, name string, structInfo *StructInfo) {
if p.visited[name] != nil {
return
}
p.visited[name] = structInfo
p.info.append(structInfo)
for _, field := range structInfo.structObj.Fields.List {
pos := structInfo.fileSet.Position(field.Pos())
filePos := path.Base(pos.String())
ident := extractIdentFromExpr(field.Type)
if ident == nil {
if len(field.Names) > 0 {
// inline fields
ident = extractIdentFromExpr(p.globals[field.Names[0].Name])
}
if ident == nil {
p.errList.WriteError(fmt.Sprintf("[%s] identifier from %s is not known\n", filePos, goPath))
continue
}
}
var goName string
if len(field.Names) > 0 {
goName = field.Names[0].Name
}
if goName == "_" {
// ignored field.
continue
}
jsonName, isInline := jsonTagFromBasicLit(field.Tag)
if isInline {
p.parseInlineField(goPath, ident.Name, structInfo)
continue
}
if jsonName == "" && !isInline {
// field is for internal use?
continue
}
if goName == "" {
p.errList.WriteError(fmt.Sprintf("[%s] unidentified field in %s", filePos, goPath))
continue
}
docs := cleanDocs(field.Doc)
if docs == "" {
p.errList.WriteError(fmt.Sprintf("[%s] %s.%s is missing documentation", filePos, goPath, goName))
}
if len(docs) <= len(goName) || !strings.HasPrefix(docs, goName+" ") {
p.errList.WriteError(fmt.Sprintf("[%s] %s.%s has invalid documentation, should start with field name", filePos, goPath, goName))
}
fieldInfo := &FieldInfo{
JSONName: jsonName,
GoPath: goPath + "." + goName,
Doc: docs,
JSONType: goTypeToJSON(p.globals, ident.Name),
IsArray: isExprArray(field.Type),
fileSet: structInfo.fileSet,
}
p.parseNestedObj(ident.Name, fieldInfo)
structInfo.Fields = append(structInfo.Fields, fieldInfo)
}
}
func (p *objParser) parseInlineField(pathName, name string, structInfo *StructInfo) {
// for inline Global "struct", keep tree as it was but change the root struct
if structObj, ok := p.globals[name].(*ast.StructType); ok {
newInfo := p.visited[name]
if newInfo == nil {
newInfo = &StructInfo{
structObj: structObj,
fileSet: structInfo.fileSet,
Name: name,
}
}
p.parse(pathName, name, newInfo)
structInfo.Fields = append(structInfo.Fields, newInfo.Fields...)
} else {
// field is inline and exported but was not scanned
p.errList.WriteError(fmt.Sprintf("field %s.%s is declared but not found\n", pathName, name))
}
}
func (p *objParser) parseNestedObj(name string, field *FieldInfo) {
if p.globals[name] != nil {
switch obj := p.globals[name].(type) {
case *ast.StructType:
newInfo := &StructInfo{
structObj: obj,
fileSet: field.fileSet,
Name: name,
}
p.parse(name, name, newInfo)
case *ast.ArrayType:
typeName := extractIdentFromExpr(obj).Name
field.JSONType = typeName
field.IsArray = true
if structObj, ok := p.globals[typeName].(*ast.StructType); ok {
newInfo := &StructInfo{
structObj: structObj,
fileSet: field.fileSet,
Name: typeName,
}
p.parse(typeName, typeName, newInfo)
}
case *ast.MapType:
typeName := extractIdentFromExpr(obj).Name
field.JSONType = typeName
field.MapKey = extractIdentFromExpr(obj.Key).Name
if structObj, ok := p.globals[typeName].(*ast.StructType); ok {
newInfo := &StructInfo{
structObj: structObj,
fileSet: field.fileSet,
Name: typeName,
}
p.parse(typeName, typeName, newInfo)
}
}
}
}
func cleanDocs(docs ...*ast.CommentGroup) string {
s := strings.Builder{}
for _, doc := range docs {
if doc == nil {
continue
}
docText := doc.Text()
var (
codeBlock bool
openBulletList bool
lastCh string
)
for _, lineComment := range strings.Split(docText, "\n") {
lineComment = strings.TrimLeft(lineComment, "/")
lineComment = strings.TrimLeft(lineComment, "/*")
lineComment = strings.TrimLeft(lineComment, "*\\")
if !codeBlock {
lineComment = strings.TrimSpace(lineComment)
}
// Handle codeblock leading/trailing space
if lineComment == "```" {
codeBlock = !codeBlock
s.WriteByte('\n')
if codeBlock {
s.WriteByte('\n')
s.WriteString(lineComment)
} else {
s.WriteString(lineComment)
s.WriteByte('\n')
}
s.WriteByte('\n')
continue
}
// Handle bullet lists formatting
if lineComment != "" && lineComment[0] == '-' {
if !openBulletList {
s.WriteString("\n")
}
s.WriteString(lineComment + "\n")
openBulletList = true
continue
}
if openBulletList {
openBulletList = false
s.WriteString("\n")
}
// Prepend empty line if line starts with `Tyk classic API definition`
if strings.HasPrefix(lineComment, "Tyk classic API definition") {
s.WriteString("\n")
}
// Append dot after Tyk classic API definition, consistency.
if lineComment == "" && lastCh == "`" {
s.WriteString(".\n")
lastCh = ""
continue
}
if lineComment != "" {
s.WriteString(lineComment)
// Each codeblock line needs a trailing \n
if codeBlock {
s.WriteByte('\n')
continue
}
// Group other text as sentences with trailing dot.
length := len(lineComment)
lastCh = lineComment[length-1 : length]
// Line ends with code block, next line determines
// which trailing space goes after
if lastCh == "`" {
continue
}
// Group sentences into individual lines in markdown
// or join them together with a space if split.
if lastCh == "." || lastCh == ":" {
s.WriteByte('\n')
continue
}
s.WriteByte(' ')
continue
}
}
}
return s.String()
}
func extractIdentFromExpr(expr ast.Expr) *ast.Ident {
switch objType := expr.(type) {
case *ast.StarExpr:
return extractIdentFromExpr(objType.X)
case *ast.Ident:
return objType
case *ast.MapType:
return extractIdentFromExpr(objType.Value)
case *ast.ArrayType:
return extractIdentFromExpr(objType.Elt)
case *ast.InterfaceType:
return ast.NewIdent("any")
case *ast.SelectorExpr:
return ast.NewIdent("object")
}
return nil
}
func isExprArray(expr ast.Expr) bool {
_, ok := expr.(*ast.ArrayType)
return ok
}
func jsonTagFromBasicLit(tag *ast.BasicLit) (name string, isInline bool) {
if tag == nil {
return "", false
}
jsonTags := strings.Split(reflect.StructTag(tag.Value).Get("json"), ",")
if len(jsonTags) == 0 {
return "", false
}
if len(jsonTags) > 1 && jsonTags[1] == "inline" {
return "", true
}
if jsonTags[0] == "" || jsonTags[0] == "-" {
return "", false
}
return jsonTags[0], false
}
func filterXTykGoFile(fInfo os.FileInfo) bool {
ignoreList := map[string]bool{
"default.go": true,
"oasutil.go": true,
"oas.go": true,
"validator.go": true,
"xtyk_doc.go": true,
}
return !(ignoreList[fInfo.Name()] || strings.HasSuffix(fInfo.Name(), "_test.go"))
}
func goTypeToJSON(globals map[string]ast.Expr, typeName string) string {
switch typeName {
case "string":
return "string"
case "int", "uint", "int64", "uint64", "int32", "uint32":
return "int"
case "float", "float32":
return "float"
case "float64":
return "double"
case "bool":
return "boolean"
case "any", "object":
return typeName
default:
if _, ok := globals[typeName]; ok {
return typeName
}
}
return ""
}
const xTykDocMarkdownTitle = `
## TYK OAS API Definition Object
`
func xTykDocToMarkdown(xtykDoc XTykDoc) string {
docWriter := strings.Builder{}
docWriter.WriteString(xTykDocMarkdownTitle)
for _, structInfo := range xtykDoc {
docWriter.WriteString(fmt.Sprintf("### **%s**\n\n", structInfo.Name))
for _, field := range structInfo.Fields {
fieldInfoToMarkdown(field, &docWriter)
}
docWriter.WriteByte('\n')
}
return docWriter.String()
}
func fieldInfoToMarkdown(field *FieldInfo, docWriter *strings.Builder) {
docWriter.WriteString(fmt.Sprintf("**Field: `%s` (%s)**\n", field.JSONName, fieldTypeToMarkdown(field)))
docWriter.WriteString(strings.TrimSpace(field.Doc) + "\n\n")
}
func fieldTypeToMarkdown(f *FieldInfo) string {
ext := ""
if f.IsArray {
ext = "[]"
}
if f.MapKey != "" {
ext = fmt.Sprintf("map[%s]", f.MapKey) + ext
}
switch f.JSONType {
case "boolean", "int", "float", "double", "string", "any", "object":
return fmt.Sprintf("`%s%s`", ext, f.JSONType)
}
if ext != "" {
ext = "`" + ext + "`"
}
// markdown link
return fmt.Sprintf("%s[%s](#%s)", ext, f.JSONType, strings.ToLower(f.JSONType))
}