Skip to content

Commit

Permalink
feat: support []string and ast.Value in ast.InterfaceToValue (open-po…
Browse files Browse the repository at this point in the history
…licy-agent#7306)

Signed-off-by: Anthony Regeda <regedaster@gmail.com>
  • Loading branch information
regeda authored Jan 31, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 55e87e7 commit 7ddaff2
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
@@ -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
@@ -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
3 changes: 3 additions & 0 deletions v1/ast/term_test.go
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ func TestInterfaceToValue(t *testing.T) {
null,
"hello",
["goodbye", 1],
["dummy", "tummy"],
{"y": 3.1}
]
}
@@ -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 {

0 comments on commit 7ddaff2

Please sign in to comment.