-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathutils.go
106 lines (92 loc) · 1.83 KB
/
utils.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
package main
import (
"fmt"
"go/ast"
"strings"
)
func asType(gd *ast.GenDecl) *ast.TypeSpec {
for _, spec := range gd.Specs {
if ts, ok := spec.(*ast.TypeSpec); ok {
return ts
}
}
return nil
}
func isStruct(ts *ast.TypeSpec) bool {
if ts == nil {
return false
}
_, ok := ts.Type.(*ast.StructType)
return ok
}
func isEnum(ts *ast.TypeSpec) bool {
if ts == nil {
return false
}
if id, ok := ts.Type.(*ast.Ident); ok {
if id.Name == "string" || id.Name == "int64" {
return true
}
}
return false
}
func parseTag(line string) (tag string, value string) {
if strings.HasPrefix(line, "@") {
for i := 1; i < len(line); i++ {
if line[i] == ' ' {
tag = line[1:i]
value = line[i+1:]
break
}
}
}
return
}
func linkify(input string) string {
return strings.Replace(strings.ToLower(input), ".", "", -1)
}
func jsonType(goType string) string {
switch goType {
case "string":
return "string"
case "int", "int64", "float64", "int32", "uint32":
return "number"
case "bool":
return "boolean"
default:
return goType
}
}
func typeToString(e ast.Expr) string {
switch node := e.(type) {
case *ast.Ident:
return jsonType(node.Name)
case *ast.StarExpr:
return typeToString(node.X)
case *ast.SelectorExpr:
name := node.Sel.Name
switch name {
case "Time":
return "Date" // javascript type date
default:
return name
}
case *ast.ArrayType:
return typeToString(node.Elt) + "[]"
case *ast.MapType:
return "{ [key: " + typeToString(node.Key) + "]: " + typeToString(node.Value) + " }"
default:
return fmt.Sprintf("%#v", node)
}
}
func getCommentLines(doc *ast.CommentGroup) []string {
if doc == nil {
return nil
}
var lines []string
for _, el := range doc.List {
line := strings.TrimSpace(strings.TrimPrefix(el.Text, "//"))
lines = append(lines, line)
}
return lines
}