...

Source file src/github.com/godbus/dbus/v5/introspect/introspectable.go

Documentation: github.com/godbus/dbus/v5/introspect

     1  package introspect
     2  
     3  import (
     4  	"encoding/xml"
     5  	"reflect"
     6  	"strings"
     7  
     8  	"github.com/godbus/dbus/v5"
     9  )
    10  
    11  // Introspectable implements org.freedesktop.Introspectable.
    12  //
    13  // You can create it by converting the XML-formatted introspection data from a
    14  // string to an Introspectable or call NewIntrospectable with a Node. Then,
    15  // export it as org.freedesktop.Introspectable on you object.
    16  type Introspectable string
    17  
    18  // NewIntrospectable returns an Introspectable that returns the introspection
    19  // data that corresponds to the given Node. If n.Interfaces doesn't contain the
    20  // data for org.freedesktop.DBus.Introspectable, it is added automatically.
    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  // Introspect implements org.freedesktop.Introspectable.Introspect.
    40  func (i Introspectable) Introspect() (string, *dbus.Error) {
    41  	return string(i), nil
    42  }
    43  
    44  // Methods returns the description of the methods of v. This can be used to
    45  // create a Node which can be passed to NewIntrospectable.
    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