This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
render.go
205 lines (197 loc) · 6 KB
/
render.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
/**
* Copyright (C) 2015 Deepin Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
**/
package main
import "fmt"
import "text/template"
import "pkg.deepin.io/lib/dbus"
import "pkg.deepin.io/lib/dbus/introspect"
import "io"
import "log"
import "strings"
//TODO: try removing dependent on variable of infos
func renderMain(writer io.Writer, tpl string, infos *Infos) {
template.Must(template.New("main").Funcs(template.FuncMap{
"Lower": lower,
"Upper": upper,
"BusType": func() string { return infos.BusType() },
"PkgName": func() string { return infos.PackageName() },
"GetModules": func() map[string]string {
r := make(map[string]string)
for _, ifc := range infos.ListInterfaces() {
r[ifc.OutFile] = ifc.OutFile
}
return r
},
"GetQtSignaturesType": func() map[string]string { return getQtSignaturesType(infos) },
}).Parse(tpl)).Execute(writer, infos)
}
func renderInterfaceInit(writer io.Writer, tpl string, infos *Infos) {
template.Must(template.New("IfcInit").Funcs(template.FuncMap{
"BusType": func() string { return infos.BusType() },
"PkgName": func() string { return infos.PackageName() },
"HasSignals": func() bool { return true },
}).Parse(tpl)).Execute(writer, nil)
}
func renderInterface(target BindingTarget, info introspect.InterfaceInfo, writer io.Writer, ifc_name, exportName string, infos *Infos) {
//TODO: removing dependent on variable of target
filterKeyWord(target, &info)
log.Printf("Generate %q code for service:%q interface:%q ObjectName:%q", target, infos.DestName(), ifc_name, exportName)
//TODO: move the common functions to the file of template_common.go
funcs := template.FuncMap{
"Lower": lower,
"Upper": upper,
"BusType": func() string { return infos.BusType() },
"PkgName": func() string { return infos.PackageName() },
"OBJ_NAME": func() string { return "obj" },
"TypeFor": dbus.TypeFor,
"getQType": getQType,
"DestName": func() string { return infos.DestName() },
"IfcName": func() string { return ifc_name },
"ExportName": func() string { return exportName },
"NormalizeQDBus": normalizeQDBus,
"Normalize": normalizeMethodName,
"Ifc2Obj": ifc2obj,
"PropWritable": func(prop introspect.PropertyInfo) bool { return prop.Access == "readwrite" },
"GetIns": func(args []introspect.ArgInfo) []introspect.ArgInfo {
ret := make([]introspect.ArgInfo, 0)
for _, a := range args {
if a.Direction != "out" {
ret = append(ret, a)
}
}
return ret
},
"GetOuts": func(args []introspect.ArgInfo) []introspect.ArgInfo {
ret := make([]introspect.ArgInfo, 0)
for _, a := range args {
if a.Direction == "out" {
ret = append(ret, a)
}
}
return ret
},
"CalcArgNum": func(args []introspect.ArgInfo, direction string) (r int) {
for _, arg := range args {
if arg.Direction == direction {
r++
}
}
return
},
"Repeat": func(str string, sep string, times int) (r string) {
for i := 0; i < times; i++ {
if i != 0 {
r += sep
}
r += str
}
return
},
"GetParamterNames": func(args []introspect.ArgInfo) (ret string) {
for _, arg := range args {
if arg.Direction == "in" {
ret += ", "
ret += arg.Name
}
}
return
},
"GetParamterOuts": func(args []introspect.ArgInfo) (ret string) {
var notFirst = false
for _, arg := range args {
if arg.Direction == "out" {
if notFirst {
ret += ","
}
notFirst = true
ret += "&" + arg.Name
}
}
return
},
"GetParamterOutsProto": func(args []introspect.ArgInfo) (ret string) {
var notFirst = false
for _, arg := range args {
if arg.Direction == "out" {
if notFirst {
ret += ","
}
notFirst = true
ret += arg.Name + " " + dbus.TypeFor(arg.Type)
}
}
return
},
"GetParamterInsProto": func(args []introspect.ArgInfo) (ret string) {
var notFirst = false
for _, arg := range args {
if arg.Direction == "in" {
if notFirst {
ret += ","
}
notFirst = true
if strings.Contains(arg.Type, "(") {
ret += arg.Name + " interface{}"
} else {
ret += arg.Name + " " + dbus.TypeFor(arg.Type)
}
}
}
return
},
"TryConvertObjectPath": func(prop introspect.PropertyInfo) string {
if v := getObjectPathConvert("Property", prop.Annotations); v != "" {
switch target {
case GoLang:
return tryConvertObjectPathGo(infos, prop.Type, v)
case QML:
return tryConvertObjectPathQML(prop.Type, v)
}
}
return ""
},
"GetObjectPathType": func(prop introspect.PropertyInfo) (ret string) {
if v := getObjectPathConvert("Property", prop.Annotations); v != "" {
switch target {
case GoLang:
ret, _ = guessTypeGo(infos, prop.Type, v)
case QML:
ret, _ = guessTypeQML(prop.Type, v)
}
return
}
return dbus.TypeFor(prop.Type)
},
"QMLI18nWrapper": func(item I18nItem, varName string) string {
dir, domain, ok := item.I18nInfo()
if !ok {
return varName
}
if dir == "" {
dir = "/usr/share/locale"
}
return fmt.Sprintf("translateI18n(%q, %q, %s)", dir, domain, varName)
},
}
templ := template.Must(template.New(exportName).Funcs(funcs).Parse(GetTemplate(target, TemplateTypeInterface)))
templ.Execute(writer, info)
}
type I18nItem interface {
I18nInfo() (string, string, bool)
}
func renderTest(testPath, objName string, writer io.Writer, info introspect.InterfaceInfo, infos *Infos) {
funcs := template.FuncMap{
"TestPath": func() string { return testPath },
"PkgName": func() string { return infos.PackageName() },
"ObjName": func() string { return objName },
/*"GetTestValue": func(args []dbus.ArgInfo) string {*/
/*},*/
}
template.Must(template.New("testing").Funcs(funcs).Parse(__TEST_TEMPLATE)).Execute(writer, info)
}