Skip to content

Commit

Permalink
Resolves #54 - Allow reduction of nil AstNode (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-r-west authored Dec 5, 2024
1 parent e1c5c69 commit 7eb1ed2
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
3 changes: 3 additions & 0 deletions external/epsearchast/v3/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package epsearchast_v3
//
// Depending on what you are doing you may find that [epsearchast_v3.SemanticReduceAst] to be simpler.
func ReduceAst[T any](a *AstNode, f func(*AstNode, []*T) (*T, error)) (*T, error) {
if a == nil {
return nil, nil
}
collector := make([]*T, 0, len(a.Children))
for _, n := range a.Children {
v, err := ReduceAst(n, f)
Expand Down
20 changes: 20 additions & 0 deletions external/epsearchast/v3/reduce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package epsearchast_v3

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestReduceNilAstDoesNotPanic(t *testing.T) {
// Fixture Setup
var ast *AstNode = nil

// Execute SUT
result, err := ReduceAst(ast, func(*AstNode, []*string) (*string, error) {
panic("should not be called")
})

// Verification
require.NoError(t, err)
require.Nil(t, result)
}
71 changes: 71 additions & 0 deletions external/epsearchast/v3/semantic_reduce_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package epsearchast_v3

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestSemanticReduceNilAstDoesNotPanic(t *testing.T) {
// Fixture Setup
var ast *AstNode = nil

// Execute SUT
reducer := PanicyReducer{}

result, err := SemanticReduceAst(ast, reducer)

// Verification
require.NoError(t, err)
require.Nil(t, result)
}

type PanicyReducer struct {
}

func (p PanicyReducer) PostVisitAnd(rs []*string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitIn(args ...string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitEq(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitLe(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitLt(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitGe(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitGt(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitLike(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitILike(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitContains(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitText(first, second string) (*string, error) {
panic("not called")
}

func (p PanicyReducer) VisitIsNull(first string) (*string, error) {
panic("not called")
}

0 comments on commit 7eb1ed2

Please sign in to comment.