Skip to content

Commit

Permalink
add support for struct entities in MakeVariant
Browse files Browse the repository at this point in the history
currently, structs are not parsed properly since `format()` has no handling for the reflect type
add a switch case to handle structs and encode some sane defaults if given an empty one

resolves #328

Signed-off-by: Charlie Doern <cdoern@redhat.com>
  • Loading branch information
cdoern authored and guelfey committed Nov 9, 2024
1 parent c266b19 commit b9236d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 17 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,22 @@ 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(", ")
}
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

0 comments on commit b9236d6

Please sign in to comment.