Skip to content

Commit

Permalink
feat: support []string and ast.Value in ast.InterfaceToValue (#7306)
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Regeda <[email protected]>
  • Loading branch information
regeda authored Jan 31, 2025
1 parent 55e87e7 commit 7ddaff2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
13 changes: 9 additions & 4 deletions v1/ast/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ type Value interface {
// InterfaceToValue converts a native Go value x to a Value.
func InterfaceToValue(x interface{}) (Value, error) {
switch x := x.(type) {
case Value:
return x, nil
case nil:
return NullValue, nil
case bool:
if x {
return InternedBooleanTerm(true).Value, nil
}
return InternedBooleanTerm(false).Value, nil
return InternedBooleanTerm(x).Value, nil
case json.Number:
if interned := InternedIntNumberTermFromString(string(x)); interned != nil {
return interned.Value, nil
Expand All @@ -88,6 +87,12 @@ func InterfaceToValue(x interface{}) (Value, error) {
r[i].Value = e
}
return NewArray(r...), nil
case []string:
r := util.NewPtrSlice[Term](len(x))
for i, e := range x {
r[i].Value = String(e)
}
return NewArray(r...), nil
case map[string]any:
kvs := util.NewPtrSlice[Term](len(x) * 2)
idx := 0
Expand Down
3 changes: 3 additions & 0 deletions v1/ast/term_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func TestInterfaceToValue(t *testing.T) {
null,
"hello",
["goodbye", 1],
["dummy", "tummy"],
{"y": 3.1}
]
}
Expand Down Expand Up @@ -73,6 +74,8 @@ func TestInterfaceToValue(t *testing.T) {
{int(100), "100"},
{map[string]string{"foo": "bar"}, `{"foo": "bar"}`},
{uint64(100), "100"},
{[]string{"dummy", "tummy"}, `["dummy", "tummy"]`},
{String("bob"), `"bob"`},
}

for _, tc := range tests {
Expand Down

0 comments on commit 7ddaff2

Please sign in to comment.