Skip to content

Commit

Permalink
Issue #71, Change interface of Array values to ComparableValue
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Hunt <[email protected]>
  • Loading branch information
independentid committed Nov 27, 2024
1 parent 3c1f1bc commit c309512
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/hexapolicy/conditions/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
const (
AAllow string = "allow"
ADeny string = "deny"
AAudit string = "audit"
// AAudit string = "audit"
)

type ConditionInfo struct {
Expand Down
19 changes: 14 additions & 5 deletions pkg/hexapolicy/types/array.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package types

import "strings"
import (
"fmt"
"strings"
)

type Array struct {
values []Value
values []ComparableValue
}

func NewArray(values []Value) Value {
func NewArray(values []ComparableValue) Value {
return Array{values}
}

Expand All @@ -31,13 +34,19 @@ func ParseArray(s string) (Value, error) {
val = strings.TrimSuffix(val, "]")

vals := strings.Split(val, ",")
values := make([]Value, len(vals))
values := make([]ComparableValue, len(vals))
for i, item := range vals {
iValue, err := ParseValue(item)
if err != nil {
return nil, err
}
values[i] = iValue
switch v := iValue.(type) {
case ComparableValue:
values[i] = v
default:
return nil, fmt.Errorf("arrays cannot contain sub-arrays or objects: %s", s)
}

}
return NewArray(values), nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/hexapolicy/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestTypes(t *testing.T) {
{
"Arrays",
"[\"a\", \"b\", \"c\"]",
Array{[]Value{String{"a"}, String{"b"}, String{"c"}}},
Array{[]ComparableValue{String{"a"}, String{"b"}, String{"c"}}},
"",
"",
false,
Expand Down Expand Up @@ -239,8 +239,8 @@ func TestArray(t *testing.T) {
assert.NoError(t, err)
assert.IsType(t, Array{}, value)
assert.Equal(t, TypeArray, value.ValueType())
assert.IsType(t, []Value{}, value.Value())
vals := value.Value().([]Value)
assert.IsType(t, []ComparableValue{}, value.Value())
vals := value.Value().([]ComparableValue)
val2 := vals[1]
assert.Equal(t, float64(2), val2.Value())
}
Expand Down

0 comments on commit c309512

Please sign in to comment.