forked from knightpp/dbus-codegen-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
392 lines (347 loc) · 10.1 KB
/
context.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package printer
import (
"errors"
"fmt"
gotoken "go/token"
"io"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/sakura-remote-desktop/dbus-codegen-go/token"
"github.com/sakura-remote-desktop/godbus/v5/introspect"
)
func newContext(ifaces []*token.Interface, opts ...PrintOption) (*context, error) {
ctx := &context{
PackageName: "dbusgen",
Interfaces: ifaces,
gofmt: true,
}
for _, opt := range opts {
opt(ctx)
}
if ctx.PackageName == "" {
return nil, errors.New("package name is empty")
}
if !identRegexp.MatchString(ctx.PackageName) {
return nil, errors.New("package name is not valid")
}
if len(ifaces) == 0 {
return nil, errors.New("no interfaces given")
}
ctx.tpl = template.New("main").Funcs(template.FuncMap{
"haveSignals": ctx.tplHaveSignals,
"ifaceNameConst": ctx.tplIfaceNameConst,
"ifaceType": ctx.tplIfaceType,
"unimplementedType": ctx.tplUnimplementedType,
"serverType": ctx.tplServerType,
"methodType": ctx.tplMethodType,
"methodFlags": ctx.tplMethodFlags,
"methodIsDeprecated": ctx.tplMethodIsDeprecated,
"propType": ctx.tplPropType,
"propGetType": ctx.tplPropGetType,
"propSetType": ctx.tplPropSetType,
"propArgName": ctx.tplPropArgName,
"propNeedsGet": ctx.tplPropNeedsGet,
"propNeedsSet": ctx.tplPropNeedsSet,
"signalType": ctx.tplSignalType,
"signalBodyType": ctx.tplSignalBodyType,
"argName": ctx.tplArgName,
"joinMethodInArgs": ctx.tplJoinMethodInArgs,
"joinMethodOutArgs": ctx.tplJoinMethodOutArgs,
"joinArgNames": ctx.tplJoinArgNames,
"joinStoreArgs": ctx.tplJoinStoreArgs,
"joinSignalValues": ctx.tplJoinSignalValues,
"joinSignalArgs": ctx.tplJoinSignalArgs,
"methodsString": ctx.tplMethodsString,
"signalsString": ctx.tplSignalsString,
"propsString": ctx.tplPropsString,
"annotationsString": ctx.tplAnnotationsString,
})
return ctx, nil
}
type context struct {
PackageName string
Imports []string
Interfaces []*token.Interface
ServerOnly bool
ClientOnly bool
NodeStr string
tpl *template.Template
gofmt bool
camelize bool
prefixes []string
}
// addImport adds a go import package.
func (ctx *context) addImport(pkg string) {
for _, s := range ctx.Imports {
if pkg == s {
return
}
}
ctx.Imports = append(ctx.Imports, pkg)
}
var ifaceRegexp = regexp.MustCompile(`[._][a-zA-Z0-9]`)
func (ctx *context) tplIfaceType(iface *token.Interface) string {
name := iface.Name
for _, prefix := range ctx.prefixes {
if prefix[len(prefix)-1] == '.' {
prefix = prefix[:len(prefix)-1]
}
if i := strings.Index(name, prefix); i != -1 {
name = name[i+len(prefix)+1:]
break
}
}
name = strings.Title(name)
if isKeyword(name) {
return name
}
return ifaceRegexp.ReplaceAllStringFunc(name, func(s string) string {
if ctx.camelize {
return strings.ToUpper(s[1:])
}
return "_" + strings.ToUpper(s[1:])
})
}
func (ctx *context) tplUnimplementedType(iface *token.Interface) string {
return "Unimplemented" + ctx.tplIfaceType(iface)
}
func (ctx *context) tplServerType(iface *token.Interface) string {
return ctx.tplIfaceType(iface) + "er"
}
func isKeyword(s string) bool {
// TODO: validate it doesn't match imported package names
return gotoken.Lookup(s).IsKeyword()
}
func (ctx *context) tplHaveSignals(ifaces []*token.Interface) bool {
for _, iface := range ifaces {
if len(iface.Signals) > 0 {
return true
}
}
return false
}
func (ctx *context) tplIfaceNameConst(iface *token.Interface) string {
return "Interface" + ctx.tplIfaceType(iface)
}
func (ctx *context) tplMethodType(method *token.Method) string {
return strings.Title(method.Name)
}
func (ctx *context) tplMethodFlags(method *token.Method) string {
for _, annotation := range method.Annotations {
if annotation.Name == "org.freedesktop.DBus.Method.NoReply" &&
annotation.Value == "true" {
return "dbus.FlagNoReplyExpected"
}
}
return "0"
}
func (ctx *context) tplMethodIsDeprecated(method *token.Method) bool {
for _, annotation := range method.Annotations {
if annotation.Name == "org.freedesktop.DBus.Deprecated" &&
annotation.Value == "true" {
return true
}
}
return false
}
func (ctx *context) tplPropType(prop *token.Property) string {
return strings.Title(prop.Name)
}
func (ctx *context) tplPropGetType(prop *token.Property) string {
return "Get" + ctx.tplPropType(prop)
}
func (ctx *context) tplPropSetType(prop *token.Property) string {
return "Set" + ctx.tplPropType(prop)
}
func (ctx *context) tplPropNeedsSet(iface *token.Interface, prop *token.Property) bool {
if !prop.Write {
return false
}
return ctx.propNeedsAccessor(iface, ctx.tplPropSetType(prop))
}
func (ctx *context) tplPropNeedsGet(iface *token.Interface, prop *token.Property) bool {
if !prop.Read {
return false
}
return ctx.propNeedsAccessor(iface, ctx.tplPropGetType(prop))
}
func (ctx *context) propNeedsAccessor(iface *token.Interface, name string) bool {
for _, method := range iface.Methods {
if method.Name == name {
return false
}
}
return true
}
func (ctx *context) tplSignalType(iface *token.Interface, signal *token.Signal) string {
join := "_"
if ctx.camelize {
join = ""
}
return ctx.tplIfaceType(iface) + join + strings.Title(signal.Name) + "Signal"
}
func (ctx *context) tplSignalBodyType(iface *token.Interface, signal *token.Signal) string {
return ctx.tplSignalType(iface, signal) + "Body"
}
var varRegexp = regexp.MustCompile("_+[a-zA-Z0-9]")
func (ctx *context) tplArgName(arg *token.Arg, prefix string, i int, export bool) string {
name := arg.Name
if name == "" {
name = prefix + strconv.Itoa(i)
} else {
name = strings.ToLower(name[:1]) +
varRegexp.ReplaceAllStringFunc(name[1:], func(s string) string {
return strings.Title(strings.TrimLeft(s, "_"))
})
}
if export {
name = strings.Title(name)
}
if isKeyword(name) {
return prefix + strings.Title(name)
}
return name
}
func (ctx *context) tplPropArgName(prop *token.Property) string {
return ctx.tplArgName(prop.Arg, "v", 0, false)
}
func (ctx *context) tplJoinStoreArgs(args []*token.Arg) string {
var buf strings.Builder
for i := range args {
if i != 0 {
buf.WriteByte(',')
}
buf.WriteByte('&')
buf.WriteString(ctx.tplArgName(args[i], "out", i, false))
}
return buf.String()
}
func (ctx *context) tplJoinArgs(args []*token.Arg, separator byte, suffix string, export bool) string {
var buf strings.Builder
for i := range args {
buf.WriteString(ctx.tplArgName(args[i], suffix, i, export))
buf.WriteByte(' ')
buf.WriteString(args[i].Type)
buf.WriteByte(separator)
}
return buf.String()
}
func (ctx *context) tplJoinSignalValues(prefix string, sig *token.Signal) string {
var buf strings.Builder
for i := range sig.Args {
buf.WriteString(prefix + ctx.tplArgName(sig.Args[i], "v", i, true))
buf.WriteByte(',')
}
return buf.String()
}
func (ctx *context) tplJoinSignalArgs(sig *token.Signal) string {
return ctx.tplJoinArgs(sig.Args, ';', "v", true)
}
func (ctx *context) tplJoinMethodInArgs(method *token.Method) string {
return ctx.tplJoinArgs(method.In, ',', "in", false)
}
func (ctx *context) tplJoinMethodOutArgs(method *token.Method) string {
return ctx.tplJoinArgs(method.Out, ',', "out", false)
}
func (ctx *context) tplJoinArgNames(args []*token.Arg) string {
var buf strings.Builder
for i := range args {
if i != 0 {
buf.WriteByte(',')
}
buf.WriteString(ctx.tplArgName(args[i], "in", i, false))
}
return buf.String()
}
func (ctx *context) tplPropsString(props []introspect.Property) string {
var buf strings.Builder
buf.WriteString("[]introspect.Property{")
for _, p := range props {
buf.WriteString("{")
writeProp(&buf, p)
buf.WriteString("},\n")
}
buf.WriteString("},")
return buf.String()
}
func (ctx *context) tplSignalsString(singals []introspect.Signal) string {
var buf strings.Builder
buf.WriteString("[]introspect.Signal{")
for _, sig := range singals {
buf.WriteString("{")
writeSignal(&buf, sig)
buf.WriteString("},\n")
}
buf.WriteString("},")
return buf.String()
}
func (ctx *context) tplMethodsString(methods []introspect.Method) string {
var buf strings.Builder
buf.WriteString("[]introspect.Method{")
for _, m := range methods {
buf.WriteString("{")
writeMethod(&buf, m)
buf.WriteString("},\n")
}
buf.WriteString("},")
return buf.String()
}
func writeMethod(out io.Writer, method introspect.Method) {
fmt.Fprintf(out, `Name:"%s",`, method.Name)
fmt.Fprint(out, "Args:[]introspect.Arg{\n")
writeArgs(out, method.Args)
fmt.Fprintf(out, "},")
}
func (ctx *context) tplAnnotationsString(anns []introspect.Annotation) string {
var buf strings.Builder
buf.WriteString("[]introspect.Annotation{\n")
writeAnnotations(&buf, anns)
buf.WriteString("},")
return buf.String()
}
func writeArgs(out io.Writer, args []introspect.Arg) {
for _, arg := range args {
fmt.Fprint(out, "{")
writeArg(out, arg)
fmt.Fprint(out, "},\n")
}
}
func writeArg(out io.Writer, arg introspect.Arg) {
tml := `Name: "%s",Type: "%s",Direction: "%s",`
fmt.Fprintf(out, tml, arg.Name, arg.Type, arg.Direction)
}
func writeAnnotations(out io.Writer, anns []introspect.Annotation) {
for _, ann := range anns {
fmt.Fprint(out, "{")
writeAnnotation(out, ann)
fmt.Fprint(out, "},\n")
}
}
func writeAnnotation(out io.Writer, ann introspect.Annotation) {
fmt.Fprintf(out, `Name:"%s",Value:"%s"`, ann.Name, ann.Value)
}
func writeProp(out io.Writer, prop introspect.Property) {
fmt.Fprintf(out, `Name:"%s",`, prop.Name)
fmt.Fprintf(out, `Type:"%s",`, prop.Type)
fmt.Fprintf(out, `Access:"%s",`, prop.Access)
if len(prop.Annotations) != 0 {
fmt.Fprint(out, "Annotations:[]introspect.Annotation{\n")
writeAnnotations(out, prop.Annotations)
fmt.Fprint(out, "},")
}
}
func writeSignal(out io.Writer, signal introspect.Signal) {
fmt.Fprintf(out, `Name:"%s",`, signal.Name)
if len(signal.Args) != 0 {
fmt.Fprint(out, "Args:[]introspect.Arg{\n")
writeArgs(out, signal.Args)
fmt.Fprint(out, "},")
}
if len(signal.Annotations) != 0 {
fmt.Fprint(out, "Annotations:[]introspect.Annotation{\n")
writeAnnotations(out, signal.Annotations)
fmt.Fprint(out, "},")
}
}