-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
110 lines (94 loc) · 1.86 KB
/
Copy pathutils.go
File metadata and controls
110 lines (94 loc) · 1.86 KB
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
package go2openapi
import (
"go/ast"
"go/token"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/getkin/kin-openapi/openapi3"
)
var compilerClousureSuffix = "-fm"
func isSubPath(parent, sub string) (bool, error) {
up := ".." + string(os.PathSeparator)
// path-comparisons using filepath.Abs don't work reliably according to docs (no unique representation).
rel, err := filepath.Rel(parent, sub)
if err != nil {
return false, err
}
if !strings.HasPrefix(rel, up) && rel != ".." {
return true, nil
}
return false, nil
}
func reverseSliceString(s []string) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
var primitiveTypeZeroValue = map[string]interface{}{
"string": "",
"int": 0,
"int8": 0,
"int16": 0,
"int32": 0,
"int64": 0,
"uint": 0,
"uint8": 0,
"uint16": 0,
"uint32": 0,
"uint64": 0,
"float": 0.0,
"float32": 0.0,
"float64": 0.0,
"bool": false,
}
// TODO: pointers
func getPrimitiveTypeDefaultValue(typ string) interface{} {
if v, ok := primitiveTypeZeroValue[typ]; ok {
return v
}
switch typ {
case "time":
return &time.Time{}
}
return nil
}
func getTypeFromToken(t token.Token) string {
switch t {
case token.INT:
return "int"
case token.FLOAT:
return "float"
case token.CHAR:
return "string"
case token.STRING:
return "string"
}
return ""
}
func getTypeFromIdent(t *ast.Ident) string {
switch t.Name {
case "true":
return "bool"
case "false":
return "bool"
}
return ""
}
func openAPIOperationByMethod(pathItem *openapi3.PathItem, method string) *openapi3.Operation {
switch method {
case http.MethodGet:
return pathItem.Get
case http.MethodPost:
return pathItem.Post
case http.MethodPut:
return pathItem.Put
case http.MethodPatch:
return pathItem.Patch
case http.MethodDelete:
return pathItem.Delete
}
return nil
}