Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for struct entities in MakeVariant #391

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions variant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`},
Expand Down
Loading