...
1 package introspect
2
3 import (
4 "encoding/xml"
5 "reflect"
6 "strings"
7
8 "github.com/godbus/dbus/v5"
9 )
10
11
12
13
14
15
16 type Introspectable string
17
18
19
20
21 func NewIntrospectable(n *Node) Introspectable {
22 found := false
23 for _, v := range n.Interfaces {
24 if v.Name == "org.freedesktop.DBus.Introspectable" {
25 found = true
26 break
27 }
28 }
29 if !found {
30 n.Interfaces = append(n.Interfaces, IntrospectData)
31 }
32 b, err := xml.Marshal(n)
33 if err != nil {
34 panic(err)
35 }
36 return Introspectable(strings.TrimSpace(IntrospectDeclarationString) + string(b))
37 }
38
39
40 func (i Introspectable) Introspect() (string, *dbus.Error) {
41 return string(i), nil
42 }
43
44
45
46 func Methods(v interface{}) []Method {
47 t := reflect.TypeOf(v)
48 ms := make([]Method, 0, t.NumMethod())
49 for i := 0; i < t.NumMethod(); i++ {
50 if t.Method(i).PkgPath != "" {
51 continue
52 }
53 mt := t.Method(i).Type
54 if mt.NumOut() == 0 ||
55 mt.Out(mt.NumOut()-1) != reflect.TypeOf(&dbus.Error{}) {
56
57 continue
58 }
59 var m Method
60 m.Name = t.Method(i).Name
61 m.Args = make([]Arg, 0, mt.NumIn()+mt.NumOut()-2)
62 for j := 1; j < mt.NumIn(); j++ {
63 if mt.In(j) != reflect.TypeOf((*dbus.Sender)(nil)).Elem() &&
64 mt.In(j) != reflect.TypeOf((*dbus.Message)(nil)).Elem() {
65 arg := Arg{"", dbus.SignatureOfType(mt.In(j)).String(), "in"}
66 m.Args = append(m.Args, arg)
67 }
68 }
69 for j := 0; j < mt.NumOut()-1; j++ {
70 arg := Arg{"", dbus.SignatureOfType(mt.Out(j)).String(), "out"}
71 m.Args = append(m.Args, arg)
72 }
73 m.Annotations = make([]Annotation, 0)
74 ms = append(ms, m)
75 }
76 return ms
77 }
78
View as plain text