-
Notifications
You must be signed in to change notification settings - Fork 27
/
util.go
162 lines (141 loc) · 3.17 KB
/
util.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
package main
import (
"bufio"
"log"
"os"
"strings"
)
func isMainFile(path string) bool {
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var isMainPackage, hasMainFunc bool
bs := bufio.NewScanner(f)
for bs.Scan() {
l := bs.Text()
if !isMainPackage && strings.HasPrefix(l, "package main") {
isMainPackage = true
}
if !hasMainFunc && strings.HasPrefix(l, "func main()") {
hasMainFunc = true
}
if isMainPackage && hasMainFunc {
break
}
}
if bs.Err() != nil {
log.Fatal(bs.Err())
}
return isMainPackage && hasMainFunc
}
func getModuleNameFromGoMod(path string) string {
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
moduleName := ""
bs := bufio.NewScanner(f)
for bs.Scan() {
l := strings.TrimSpace(bs.Text())
if strings.HasPrefix(l, "module") {
moduleName = strings.TrimSpace(strings.TrimPrefix(l, "module"))
break
}
}
// if bs.Err() != nil {
// return ""
// }
return moduleName
}
func isInStringList(list []string, s string) bool {
for i, _ := range list {
if list[i] == s {
return true
}
}
return false
}
var basicGoTypes = map[string]bool{
"bool": true,
"uint": true,
"uint8": true,
"uint16": true,
"uint32": true,
"uint64": true,
"int": true,
"int8": true,
"int16": true,
"int32": true,
"int64": true,
"float32": true,
"float64": true,
"string": true,
"complex64": true,
"complex128": true,
"byte": true,
"rune": true,
"uintptr": true,
"error": true,
}
func isBasicGoType(typeName string) bool {
_, ok := basicGoTypes[typeName]
return ok
}
var goTypesOASTypes = map[string]string{
"bool": "boolean",
"uint": "integer",
"uint8": "integer",
"uint16": "integer",
"uint32": "integer",
"uint64": "integer",
"int": "integer",
"int8": "integer",
"int16": "integer",
"int32": "integer",
"int64": "integer",
"float32": "number",
"float64": "number",
"string": "string",
}
func isGoTypeOASType(typeName string) bool {
_, ok := goTypesOASTypes[typeName]
return ok
}
var goTypesOASFormats = map[string]string{
"bool": "boolean",
"uint": "int64",
"uint8": "int64",
"uint16": "int64",
"uint32": "int64",
"uint64": "int64",
"int": "int64",
"int8": "int64",
"int16": "int64",
"int32": "int64",
"int64": "int64",
"float32": "float",
"float64": "double",
"string": "string",
}
// var typeDefTranslations = map[string]string{}
// var modelNamesPackageNames = map[string]string{}
func addSchemaRefLinkPrefix(name string) string {
if strings.HasPrefix(name, "#/components/schemas/") {
return replaceBackslash(name)
}
return replaceBackslash("#/components/schemas/" + name)
}
func trimeSchemaRefLinkPrefix(ref string) string {
return strings.TrimPrefix(ref, "#/components/schemas/")
}
func genSchemeaObjectID(pkgName, typeName string) string {
typeNameParts := strings.Split(typeName, ".")
pkgName = replaceBackslash(pkgName)
return strings.Join(append(strings.Split(pkgName, "/"), typeNameParts[len(typeNameParts)-1]), ".")
}
func replaceBackslash(origin string) string {
return strings.ReplaceAll(origin, "\\", "/")
}