11 // Variant represents the D-Bus variant type.
17 // MakeVariant converts the given value to a Variant. It panics if v cannot be
18 // represented as a D-Bus type.
19 func MakeVariant(v interface{}) Variant {
20 return Variant{SignatureOf(v), v}
23 // ParseVariant parses the given string as a variant as described at
24 // https://developer.gnome.org/glib/unstable/gvariant-text.html. If sig is not
25 // empty, it is taken to be the expected signature for the variant.
26 func ParseVariant(s string, sig Signature) (Variant, error) {
28 p := &varParser{tokens: tokens}
29 n, err := varMakeNode(p)
34 sig, err = varInfer(n)
39 v, err := n.Value(sig)
43 return MakeVariant(v), nil
46 // format returns a formatted version of v and whether this string can be parsed
48 func (v Variant) format() (string, bool) {
51 return fmt.Sprint(v.value), true
52 case 'n', 'q', 'u', 'x', 't', 'd', 'h':
53 return fmt.Sprint(v.value), false
55 return strconv.Quote(v.value.(string)), true
57 return strconv.Quote(string(v.value.(ObjectPath))), false
59 return strconv.Quote(v.value.(Signature).str), false
61 s, unamb := v.value.(Variant).format()
63 return "<@" + v.value.(Variant).sig.str + " " + s + ">", true
65 return "<" + s + ">", true
67 return fmt.Sprintf("%#x", v.value.(byte)), false
69 rv := reflect.ValueOf(v.value)
76 buf := bytes.NewBuffer([]byte("["))
77 for i := 0; i < rv.Len(); i++ {
79 s, b := MakeVariant(rv.Index(i).Interface()).format()
87 return buf.String(), unamb
94 kvs := make([]string, rv.Len())
95 for i, k := range rv.MapKeys() {
96 s, b := MakeVariant(k.Interface()).format()
100 buf.WriteString(": ")
101 s, b = MakeVariant(rv.MapIndex(k).Interface()).format()
104 kvs[i] = buf.String()
109 for i, kv := range kvs {
111 buf.WriteString(", ")
116 return buf.String(), unamb
118 return `"INVALID"`, true
121 // Signature returns the D-Bus signature of the underlying value of v.
122 func (v Variant) Signature() Signature {
126 // String returns the string representation of the underlying value of v as
127 // described at https://developer.gnome.org/glib/unstable/gvariant-text.html.
128 func (v Variant) String() string {
129 s, unamb := v.format()
131 return "@" + v.sig.str + " " + s
136 // Value returns the underlying value of v.
137 func (v Variant) Value() interface{} {