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
|
package main
import (
"fmt"
"log"
"os"
"strings"
"text/template"
)
const propCode = `
type Prop{{.Type}} struct {
Impl Implementer
Name string
}
func (p Prop{{.Type}}) Get(flags dbus.Flags) (value {{.GoType}}, err error) {
err = p.Impl.GetObject_().GetProperty_(flags, p.Impl.GetInterfaceName_(), p.Name, &value)
return
}
func (p Prop{{.Type}}) Set(flags dbus.Flags, value {{.GoType}}) error {
return p.Impl.GetObject_().SetProperty_(flags, p.Impl.GetInterfaceName_(), p.Name, value)
}
func (p Prop{{.Type}}) ConnectChanged(cb func(hasValue bool, value {{.GoType}})) error {
if cb == nil {
return errNilCallback
}
cb0 := func(hasValue bool, value interface{}) {
if hasValue {
val, ok := value.({{.GoType}})
if ok {
cb(true, val)
}
} else {
cb(false, {{.GoEmptyValue}})
}
}
return p.Impl.GetObject_().ConnectPropertyChanged_(p.Impl.GetInterfaceName_(), p.Name, cb0)
}
`
type config struct {
Type string
GoType string
GoEmptyValue string
}
func main() {
configs := []config{
{
Type: "Bool",
GoType: "bool",
GoEmptyValue: "false",
},
{
Type: "String",
GoType: "string",
GoEmptyValue: `""`,
},
{
Type: "ObjectPath",
GoType: "dbus.ObjectPath",
GoEmptyValue: `""`,
},
{
Type: "Double",
GoType: "float64",
GoEmptyValue: "0",
},
}
addNumType := func(name string) {
configs = append(configs, config{
Type: name,
GoType: strings.ToLower(name),
GoEmptyValue: "0",
})
}
for _, name := range []string{"Byte", "Int16", "Uint16", "Int32", "Uint32", "Int64",
"Uint64"} {
addNumType(name)
}
t := template.Must(template.New("propCode").Parse(propCode))
fmt.Println("package proxy")
fmt.Println("import \"errors\"")
fmt.Println("import \"github.com/godbus/dbus\"")
fmt.Println("\nvar errNilCallback = errors.New(\"nil callback\")")
for _, cfg := range configs {
err := t.Execute(os.Stdout, cfg)
if err != nil {
log.Fatal(err)
}
}
for _, cfg := range configs {
cfg.Type = cfg.Type + "Array"
cfg.GoType = "[]" + cfg.GoType
cfg.GoEmptyValue = "nil"
err := t.Execute(os.Stdout, cfg)
if err != nil {
log.Fatal(err)
}
}
}
|