Skip to content

Commit

Permalink
Merge pull request #69 from strongdm/IDX-461-simplify-string
Browse files Browse the repository at this point in the history
Allow to use types defined from bool, string, int and int64 as ast Values
  • Loading branch information
philhassey authored Nov 21, 2024
2 parents 257623d + 2809a97 commit ccfb96b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 29 additions & 0 deletions ast/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
)

func TestASTByTable(t *testing.T) {
type CustomString string
type CustomBool bool
type CustomInt int
type CustomInt64 int64
t.Parallel()
tests := []struct {
name string
Expand Down Expand Up @@ -143,6 +147,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 +172,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 ccfb96b

Please sign in to comment.