File: config.go

package info (click to toggle)
golang-github-linuxdeepin-go-dbus-factory 1.9.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,864 kB
  • sloc: xml: 11,754; makefile: 35; sh: 13
file content (248 lines) | stat: -rw-r--r-- 5,301 bytes parent folder | download
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
package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/godbus/dbus/introspect"
)

type ServiceConfig struct {
	Service       string // optional
	Objects       []*ObjectConfig
	PropertyTypes []*PropertyTypeConfig
}

func (sc *ServiceConfig) autoFill() {
	for _, objCfg := range sc.Objects {
		objCfg.autoFill()
	}
	for _, propTypeCfg := range sc.PropertyTypes {
		propTypeCfg.autoFill("", nil)
	}
}

type PropertyTypeConfig struct {
	Type       string
	ValueType  string
	EmptyValue string
}

func (ptc *PropertyTypeConfig) autoFill(propName string, ifcCfg *InterfaceConfig) {
	if ptc.Type == "" &&
		ifcCfg != nil &&
		ifcCfg.Accessor != "" &&
		propName != "" {
		ptc.Type = "Prop" + ifcCfg.Accessor + propName
	}

	if ptc.EmptyValue == "" {
		if strings.HasPrefix(ptc.ValueType, "map[") ||
			strings.HasPrefix(ptc.ValueType, "[]") {
			ptc.EmptyValue = "nil"
		} else {
			ptc.EmptyValue = ptc.ValueType + "{}"
		}
	} else {
		ptc.EmptyValue = strings.Replace(ptc.EmptyValue, "$T",
			ptc.ValueType, 1)
	}
}

type ObjectConfig struct {
	Type       string
	Path       string   // optional
	XMLFile    string   // optional
	XMLFiles   []string // optional
	Interfaces []*InterfaceConfig
}

func (oc *ObjectConfig) autoFill() {
	for _, ifcCfg := range oc.Interfaces {
		ifcCfg.autoFill()
	}
}

func (oc *ObjectConfig) getXmlFiles(dir string) []string {
	var result []string
	if len(oc.XMLFiles) > 0 {
		for _, pat := range oc.XMLFiles {
			matches, _ := filepath.Glob(filepath.Join(dir, pat+".xml"))
			result = append(result, matches...)
		}

	} else if oc.XMLFile != "" {
		if oc.XMLFile == "-" {
			return nil
		}
		result = append(result, filepath.Join(dir, oc.XMLFile+".xml"))
	} else {
		result = append(result, filepath.Join(dir, oc.Type+".xml"))
	}
	return result
}

func (oc *ObjectConfig) loadXml(dir string) ([]introspect.Interface, error) {
	var result []introspect.Interface
	xmlFiles := oc.getXmlFiles(dir)
	for _, file := range xmlFiles {
		interfaces, err := getInterfacesFromXmlFile(file)
		log.Println("load xml file:", file)
		if err != nil {
			return nil, err
		}
		result = append(result, interfaces...)
	}
	return result, nil
}

func getInterfacesFromXmlFile(file string) ([]introspect.Interface, error) {
	f, err := os.Open(file)
	if err != nil {
		return nil, err
	}
	defer f.Close()

	dec := xml.NewDecoder(f)
	var node introspect.Node
	err = dec.Decode(&node)
	if err != nil {
		return nil, err
	}
	return node.Interfaces, nil
}

func (oc *ObjectConfig) getInterface(name string) *InterfaceConfig {
	for _, ifc := range oc.Interfaces {
		if ifc.Name == name {
			return ifc
		}
	}
	return nil
}

type InterfaceConfig struct {
	Name        string
	Type        string
	Accessor    string
	Fixes       map[string]json.RawMessage
	TypeDefined bool
	NoGetInterfaceName bool

	MethodFixes map[string]ArgFixes
	PropFixes   map[string]*PropertyFix
	SignalFixes map[string]ArgFixes
}

func (ic *InterfaceConfig) autoFill() {
	dotRIdx := strings.LastIndex(ic.Name, ".")
	if dotRIdx == -1 {
		panic(fmt.Sprintf("not found dot in interface name %q", ic.Name))
	}
	if ic.Accessor == "" {
		ic.Accessor = strings.Title(ic.Name[dotRIdx+1:])
	}

	if ic.Type == "" && ic.Accessor != "" {
		firstLetter := strings.ToLower(string(ic.Accessor[0]))
		ic.Type = firstLetter + ic.Accessor[1:]
	}

	for key, fix := range ic.Fixes {
		if strings.HasPrefix(key, "p/") {
			propName := strings.TrimPrefix(key, "p/")
			var propFix PropertyFix
			err := json.Unmarshal(fix, &propFix)
			if err != nil {
				panic(err)
			}
			if ic.PropFixes == nil {
				ic.PropFixes = make(map[string]*PropertyFix)
			}
			ic.PropFixes[propName] = &propFix

		} else if strings.HasPrefix(key, "s/") {
			signalName := strings.TrimPrefix(key, "s/")
			var argFixes ArgFixes
			err := json.Unmarshal(fix, &argFixes)
			if err != nil {
				panic(err)
			}
			if ic.SignalFixes == nil {
				ic.SignalFixes = make(map[string]ArgFixes)
			}
			ic.SignalFixes[signalName] = argFixes

		} else if strings.HasPrefix(key, "m/") {
			methodName := strings.TrimPrefix(key, "m/")
			var argFixes ArgFixes
			err := json.Unmarshal(fix, &argFixes)
			if err != nil {
				panic(err)
			}
			if ic.MethodFixes == nil {
				ic.MethodFixes = make(map[string]ArgFixes)
			}
			ic.MethodFixes[methodName] = argFixes
		}
	}
	for propName, propFix := range ic.PropFixes {
		propFix.autoFill(propName, ic)
	}
}

func (ic *InterfaceConfig) getPropertyFix(name string) *PropertyFix {
	v := ic.PropFixes[name]
	return v
}

func (ic *InterfaceConfig) getMethodFix(name string) ArgFixes {
	v := ic.MethodFixes[name]
	return v
}

func (ic *InterfaceConfig) getSignalFix(name string) ArgFixes {
	v := ic.SignalFixes[name]
	return v
}

type ArgFixes []ArgFix

func (fixes ArgFixes) get(name string) ArgFix {
	for _, fix := range fixes {
		if fix.Name == name {
			return fix
		}
	}
	return ArgFix{}
}

type ArgFix struct {
	Name string
	Type string
}

type PropertyFix struct {
	PropertyTypeConfig
	RenameTo string
	RefType  string
}

func loadConfig(file string) (*ServiceConfig, error) {
	data, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	var s ServiceConfig
	err = json.Unmarshal(data, &s)
	if err != nil {
		return nil, err
	}
	return &s, nil
}