Skip to content

Commit

Permalink
Allow to use types defined from bool, string, int and int64 as ast
Browse files Browse the repository at this point in the history
Values

Signed-off-by: Marcin Swiderski <[email protected]>
  • Loading branch information
forgems committed Nov 21, 2024
1 parent 257623d commit 200793a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
30 changes: 30 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import (
"github.com/cedar-policy/cedar-go/types"
)

type CustomString string
type CustomBool bool
type CustomInt int
type CustomInt64 int64

func TestASTByTable(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down Expand Up @@ -143,6 +148,16 @@ func TestASTByTable(t *testing.T) {
ast.Permit().When(ast.Boolean(true)),
internalast.Permit().When(internalast.Boolean(true)),
},
{
"customValueBoolFalse",
ast.Permit().When(ast.Boolean(CustomBool(false))),
internalast.Permit().When(internalast.Boolean(false)),
},
{
"customValueBoolTrue",
ast.Permit().When(ast.Boolean(CustomBool(true))),
internalast.Permit().When(internalast.Boolean(true)),
},
{
"valueTrue",
ast.Permit().When(ast.True()),
Expand All @@ -158,6 +173,21 @@ func TestASTByTable(t *testing.T) {
ast.Permit().When(ast.String("cedar")),
internalast.Permit().When(internalast.String("cedar")),
},
{
"customValueString",
ast.Permit().When(ast.String(CustomString("cedar"))),
internalast.Permit().When(internalast.String("cedar")),
},
{
"customValueInt",
ast.Permit().When(ast.Long(CustomInt(42))),
internalast.Permit().When(internalast.Long(42)),
},
{
"customValueInt64",
ast.Permit().When(ast.Long(CustomInt64(42))),
internalast.Permit().When(internalast.Long(42)),
},
{
"valueLong",
ast.Permit().When(ast.Long(42)),
Expand Down
6 changes: 3 additions & 3 deletions ast/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Boolean creates a value node containing a Boolean.
func Boolean[T bool | types.Boolean](b T) Node {
func Boolean[T ~bool](b T) Node {
return wrapNode(ast.Boolean(types.Boolean(b)))
}

Expand All @@ -24,12 +24,12 @@ func False() Node {
}

// String creates a value node containing a String.
func String[T string | types.String](s T) Node {
func String[T ~string](s T) Node {
return wrapNode(ast.String(types.String(s)))
}

// Long creates a value node containing a Long.
func Long[T int | int64 | types.Long](l T) Node {
func Long[T ~int | ~int64](l T) Node {
return wrapNode(ast.Long(types.Long(l)))
}

Expand Down

0 comments on commit 200793a

Please sign in to comment.