-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.go
124 lines (117 loc) · 2.59 KB
/
template.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
package main
import (
"path"
"path/filepath"
"strings"
"text/template"
)
type CommonData struct {
Filename string
HeaderExt string
MacroPrefix string
MacroSuffix string
NamespacePrefix []string
TypeMap map[string]string
}
type ConstantData struct {
Name string
Value string
Type string
}
type FieldData struct {
Name string
Signature string
Type string
IsStatic bool
IsEnum bool
IsFinal bool
}
type MethodData struct {
Name string
Signature string
ReturnType string
ArgumentTypes []string
IsAbstract bool
IsStatic bool
}
type ClassData struct {
CommonData
FullName string
PackageName string
ClassName string
IsFinal bool
SuperClass string
Dependencies []string
Constants []ConstantData
Initializers []MethodData
Fields []FieldData
Methods []MethodData
}
func makeTemplate(name, tpl string) *template.Template {
t := template.New(name)
t.Funcs(template.FuncMap{
"Back": func(slice []string) string {
return slice[len(slice)-1]
},
"Base": path.Base,
"Dir": func(s string) string {
dir := path.Dir(s)
if dir == "." {
return ""
}
return dir
},
"Ext": path.Ext,
"Front": func(slice []string) string {
return slice[0]
},
"IsPrimitive": isCxxPrimitive,
"IsReserved": isCxxReserved,
"Join": strings.Join,
"LookupType": func(jType string) string {
return lookupCxxType(jType)
},
"LookupHeader": func(jType string) string {
if fname := lookupCxxHeader(jType); fname != nil {
return *fname
}
return ""
},
"PopBack": func(slice []string) []string {
return slice[:len(slice)-1]
},
"PopFront": func(slice []string) []string {
return slice[1:]
},
"Replace": strings.Replace,
"ReplaceAll": func(s, o, n string) string {
return strings.Replace(s, o, n, -1)
},
"ReplaceExt": replaceExt,
"Reverse": func(s []string) []string {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
},
"SnakeCase": snakeCase,
"Split": splitString,
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
"TrimExt": func(fname string) string {
return strings.TrimSuffix(fname, filepath.Ext(fname))
},
"TrimNamespace": func(s string) string {
return strings.TrimPrefix(s, *namespacePrefix+"::")
},
"TrimPrefix": func(s, prefix string) string {
return strings.TrimPrefix(s, prefix)
},
"TrimSuffix": func(s, suffix string) string {
return strings.TrimSuffix(s, suffix)
},
"UpperCamelCase": upperCamelCase,
})
template.Must(t.Parse(tpl))
return t
}