forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
105 lines (87 loc) · 2.38 KB
/
types.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
package swag
import (
"go/ast"
"go/token"
"regexp"
"strings"
"github.com/go-openapi/spec"
)
// Schema parsed schema.
type Schema struct {
*spec.Schema //
PkgPath string // package import path used to rename Name of a definition int case of conflict
Name string // Name in definitions
}
// TypeSpecDef the whole information of a typeSpec.
type TypeSpecDef struct {
// ast file where TypeSpec is
File *ast.File
// the TypeSpec of this type definition
TypeSpec *ast.TypeSpec
Enums []EnumValue
// path of package starting from under ${GOPATH}/src or from module path in go.mod
PkgPath string
ParentSpec ast.Decl
NotUnique bool
}
// Name the name of the typeSpec.
func (t *TypeSpecDef) Name() string {
if t.TypeSpec != nil {
return t.TypeSpec.Name.Name
}
return ""
}
// TypeName the type name of the typeSpec.
func (t *TypeSpecDef) TypeName() string {
if ignoreNameOverride(t.TypeSpec.Name.Name) {
return t.TypeSpec.Name.Name[1:]
} else if t.TypeSpec.Comment != nil {
// get alias from comment '// @name '
const regexCaseInsensitive = "(?i)"
reTypeName, err := regexp.Compile(regexCaseInsensitive + `^@name\s+(\S+)`)
if err != nil {
panic(err)
}
for _, comment := range t.TypeSpec.Comment.List {
trimmedComment := strings.TrimSpace(strings.TrimLeft(comment.Text, "/"))
texts := reTypeName.FindStringSubmatch(trimmedComment)
if len(texts) > 1 {
return texts[1]
}
}
}
var names []string
if t.NotUnique {
pkgPath := strings.Map(func(r rune) rune {
if r == '\\' || r == '/' || r == '.' {
return '_'
}
return r
}, t.PkgPath)
names = append(names, pkgPath)
} else {
names = append(names, t.File.Name.Name)
}
if parentFun, ok := (t.ParentSpec).(*ast.FuncDecl); ok && parentFun != nil {
names = append(names, parentFun.Name.Name)
}
names = append(names, t.TypeSpec.Name.Name)
return fullTypeName(names...)
}
// FullPath return the full path of the typeSpec.
func (t *TypeSpecDef) FullPath() string {
return t.PkgPath + "." + t.Name()
}
// AstFileInfo information of an ast.File.
type AstFileInfo struct {
//FileSet the FileSet object which is used to parse this go source file
FileSet *token.FileSet
// File ast.File
File *ast.File
// Path the path of the ast.File
Path string
// PackagePath package import path of the ast.File
PackagePath string
// ParseFlag determine what to parse
ParseFlag ParseFlag
}