forked from saltosystems/winrt-go
/
templates.go
302 lines (250 loc) · 6.46 KB
/
templates.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package codegen
import (
"embed"
"strings"
"text/template"
"github.com/KayFelicities/winrt-go/internal/winmd"
)
type genDataFile struct {
Filename string
Data genData
}
type genData struct {
Package string
Imports []string
Classes []*genClass
Enums []*genEnum
Interfaces []*genInterface
Structs []*genStruct
Delegates []*genDelegate
DelegateExports []*genDelegate
}
func (g *genData) ComputeImports(typeDef *winmd.TypeDef) {
// gather all imports
imports := make([]*genImport, 0)
if g.Classes != nil {
for _, c := range g.Classes {
imports = append(imports, c.GetRequiredImports()...)
}
}
if g.Interfaces != nil {
for _, i := range g.Interfaces {
imports = append(imports, i.GetRequiredImports()...)
}
}
for _, i := range imports {
if typeDef.TypeNamespace != i.Namespace {
g.Imports = append(g.Imports, i.ToGoImport())
}
}
}
type genInterface struct {
Name string
GUID string
Signature string
Funcs []*genFunc
}
func (g *genInterface) GetRequiredImports() []*genImport {
imports := make([]*genImport, 0)
for _, f := range g.Funcs {
imports = append(imports, f.RequiresImports...)
}
return imports
}
type genClass struct {
Name string
Signature string
RequiresImports []*genImport
FullyQualifiedName string
ImplInterfaces []*genInterface
ExclusiveInterfaces []*genInterface
HasEmptyConstructor bool
}
func (g *genClass) GetRequiredImports() []*genImport {
imports := make([]*genImport, 0)
if g.RequiresImports != nil {
imports = append(imports, g.RequiresImports...)
}
if g.ExclusiveInterfaces != nil {
for _, i := range g.ExclusiveInterfaces {
imports = append(imports, i.GetRequiredImports()...)
}
}
return imports
}
type genDelegate struct {
Name string
GUID string
Signature string
InParams []*genParam
ReturnParam *genParam // this may be nil
}
type genEnum struct {
Name string
Type string
Signature string
Values []*genEnumValue
}
type genEnumValue struct {
Name string
Value string
}
type genFunc struct {
Name string
RequiresImports []*genImport
Implement bool
FuncOwner string
InParams []*genParam
ReturnParams []*genParam // this may be empty
// ExclusiveTo is the name of the class that this function is exclusive to.
// The funcion will be called statically using the RoGetActivationFactory function.
ExclusiveTo string
RequiresActivation bool
InheritedFrom winmd.QualifiedID
}
type genImport struct {
Namespace, Name string
}
func (i genImport) ToGoImport() string {
if !strings.Contains(i.Namespace, ".") && i.Namespace != "Windows" {
// This is probably a built-in package
return i.Namespace
}
folder := typeToFolder(i.Namespace, i.Name)
return "github.com/KayFelicities/winrt-go/" + folder
}
// some of the variables are not public to avoid using them
// by mistake in the code.
type genDefaultValue struct {
value string
isPrimitive bool
}
// some of the variables are not public to avoid using them
// by mistake in the code.
type genParamType struct {
namespace string
name string
IsPointer bool
IsArray bool
IsPrimitive bool
IsEnum bool
UnderlyingEnumType string
defaultValue genDefaultValue
}
// some of the variables are not public to avoid using them
// by mistake in the code.
type genParam struct {
callerPackage string
varName string
Type *genParamType
IsOut bool
}
func (g *genParam) GoVarName() string {
return typeNameToGoName(g.varName, true) // assume all are public
}
func (g *genParam) GoTypeName() string {
if g.Type.IsPrimitive {
return g.Type.name
}
name := typeNameToGoName(g.Type.name, true) // assume all are public
pkg := typePackage(g.Type.namespace, g.Type.name)
if g.callerPackage != pkg {
name = pkg + "." + name
}
return name
}
func (g *genParam) GoDefaultValue() string {
if g.Type.defaultValue.isPrimitive {
return g.Type.defaultValue.value
}
pkg := typePackage(g.Type.namespace, g.Type.name)
if g.callerPackage != pkg {
return pkg + "." + g.Type.defaultValue.value
}
return g.Type.defaultValue.value
}
type genStruct struct {
Name string
Signature string
Fields []*genParam
}
//go:embed templates/*
var templatesFS embed.FS
func getTemplates() (*template.Template, error) {
return template.New("").
Funcs(funcs()).
ParseFS(templatesFS, "templates/*")
}
func funcs() template.FuncMap {
return template.FuncMap{
"funcName": funcName,
"concat": func(a, b []*genParam) []*genParam {
return append(a, b...)
},
"toLower": func(s string) string {
return strings.ToLower(s[:1]) + s[1:]
},
}
}
// funcName is used to generate the name of a function.
func funcName(m genFunc) string {
switch {
case strings.HasPrefix(m.Name, "get_"):
return strings.Replace(m.Name, "get_", "Get", 1)
case strings.HasPrefix(m.Name, "put_"):
return strings.Replace(m.Name, "put_", "Set", 1)
case strings.HasPrefix(m.Name, "add_"):
return strings.Replace(m.Name, "add_", "Add", 1)
case strings.HasPrefix(m.Name, "remove_"):
return strings.Replace(m.Name, "remove_", "Remove", 1)
}
return m.Name
}
func typeToFolder(ns, name string) string {
fullName := ns
return strings.ToLower(strings.Replace(fullName, ".", "/", -1))
}
func typePackage(ns, name string) string {
sns := strings.Split(ns, ".")
return strings.ToLower(sns[len(sns)-1])
}
func enumName(typeName string, enumName string) string {
return typeName + enumName
}
func typeDefGoName(typeName string, public bool) string {
name := typeName
if isParameterizedName(typeName) {
name = strings.Split(name, "`")[0]
}
if !public {
name = strings.ToLower(name[0:1]) + name[1:]
}
return name
}
func isParameterizedName(typeName string) bool {
// parameterized types contain a '`' followed by the amount of generic parameters in their name.
return strings.Contains(typeName, "`")
}
func typeFilename(typeName string) string {
// public boolean is not relevant, we are going to lower everything
goname := typeDefGoName(typeName, true)
return strings.ToLower(goname)
}
// removes Go reserved words from param names
func cleanReservedWords(name string) string {
switch name {
case "type":
return "mType"
}
return name
}
func typeNameToGoName(typeName string, public bool) string {
name := typeName
if isParameterizedName(typeName) {
name = strings.Split(name, "`")[0]
}
if !public {
name = strings.ToLower(name[0:1]) + name[1:]
}
return name
}