diff --git a/variant.go b/variant.go index ca3dbe1..93a2ee8 100644 --- a/variant.go +++ b/variant.go @@ -73,7 +73,7 @@ func (v Variant) format() (string, bool) { } rv := reflect.ValueOf(v.value) switch rv.Kind() { - case reflect.Slice: + case reflect.Slice, reflect.Array: if rv.Len() == 0 { return "[]", false } @@ -119,6 +119,25 @@ func (v Variant) format() (string, bool) { } buf.WriteByte('}') return buf.String(), unamb + case reflect.Struct: + if rv.NumField() == 0 { + return "()", false + } + unamb := true + var buf bytes.Buffer + buf.WriteByte('(') + for i := 0; i < rv.NumField(); i++ { + s, b := MakeVariant(rv.Field(i).Interface()).format() + unamb = unamb && b + buf.WriteString(s) + buf.WriteString(",") + if i != rv.NumField()-1 { + buf.WriteString(" ") + } + } + buf.WriteByte(')') + return buf.String(), unamb + } return `"INVALID"`, true } diff --git a/variant_test.go b/variant_test.go index 23ac645..72153b6 100644 --- a/variant_test.go +++ b/variant_test.go @@ -5,10 +5,16 @@ import ( "testing" ) +type Testing struct { + First string + Second uint64 +} + var variantFormatTests = []struct { v interface{} s string }{ + {[]Testing{{First: "testing", Second: 123}}, "@a(st) [(\"testing\", 123,)]"}, {int32(1), `1`}, {"foo", `"foo"`}, {ObjectPath("/org/foo"), `@o "/org/foo"`},