forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
118 lines (99 loc) · 2.23 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
package dartgen
import (
"log"
"os"
"reflect"
"strings"
"github.com/yilefreedom/go-zero/tools/goctl/api/spec"
"github.com/yilefreedom/go-zero/tools/goctl/api/util"
)
func lowCamelCase(s string) string {
if len(s) < 1 {
return ""
}
s = util.ToCamelCase(util.ToSnakeCase(s))
return util.ToLower(s[:1]) + s[1:]
}
func pathToFuncName(path string) string {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
if !strings.HasPrefix(path, "/api") {
path = "/api" + path
}
path = strings.Replace(path, "/", "_", -1)
path = strings.Replace(path, "-", "_", -1)
camel := util.ToCamelCase(path)
return util.ToLower(camel[:1]) + camel[1:]
}
func tagGet(tag, k string) (reflect.Value, error) {
v, _ := util.TagLookup(tag, k)
out := strings.Split(v, ",")[0]
return reflect.ValueOf(out), nil
}
func convertMemberType(api *spec.ApiSpec) {
for i, t := range api.Types {
for j, mem := range t.Members {
api.Types[i].Members[j].Type = goTypeToDart(mem.Type)
}
}
}
func goTypeToDart(t string) string {
t = strings.Replace(t, "*", "", -1)
if strings.HasPrefix(t, "[]") {
return "List<" + goTypeToDart(t[2:]) + ">"
}
if strings.HasPrefix(t, "map") {
tys, e := util.DecomposeType(t)
if e != nil {
log.Fatal(e)
}
if len(tys) != 2 {
log.Fatal("Map type number !=2")
}
return "Map<String," + goTypeToDart(tys[1]) + ">"
}
switch t {
case "string":
return "String"
case "int", "int32", "int64":
return "int"
case "float", "float32", "float64":
return "double"
case "bool":
return "bool"
default:
return t
}
}
func isDirectType(s string) bool {
return isAtomicType(s) || isListType(s) && isAtomicType(getCoreType(s))
}
func isAtomicType(s string) bool {
switch s {
case "String", "int", "double", "bool":
return true
default:
return false
}
}
func isListType(s string) bool {
return strings.HasPrefix(s, "List<")
}
func isClassListType(s string) bool {
return strings.HasPrefix(s, "List<") && !isAtomicType(getCoreType(s))
}
func getCoreType(s string) string {
if isAtomicType(s) {
return s
}
if isListType(s) {
s = strings.Replace(s, "List<", "", -1)
return strings.Replace(s, ">", "", -1)
}
return s
}
func fileExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}