Skip to content

Commit

Permalink
Merge pull request #6 from elasticpath/issue-5
Browse files Browse the repository at this point in the history
Resolves #5 - Use args for all operators
  • Loading branch information
steve-r-west authored Mar 31, 2023
2 parents 0626ae1 + 449613f commit 57be93d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 124 deletions.
13 changes: 9 additions & 4 deletions external/epsearchast/v3/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,22 @@ func (a *AstNode) checkValid() error {
return fmt.Errorf("and should have at least two children")
}
case "IN":
if len(a.Args) < 2 {
return fmt.Errorf("insufficient number of arguments to in")
if len(a.Children) > 0 {
return fmt.Errorf("operator %v should not have any children", strings.ToLower(a.NodeType))
}

if len(a.Children) > 0 {
return fmt.Errorf("in should not have any children")
if len(a.Args) < 2 {
return fmt.Errorf("insufficient number of arguments to %s", strings.ToLower(a.NodeType))
}
case "EQ", "LE", "LT", "GT", "GE", "LIKE":
if len(a.Children) > 0 {
return fmt.Errorf("operator %v should not have any children", strings.ToLower(a.NodeType))
}

if len(a.Args) != 2 {
return fmt.Errorf("operator %v should have exactly 2 arguments", strings.ToLower(a.NodeType))

}
default:
return fmt.Errorf("unknown operator %s", a.NodeType)
}
Expand Down
33 changes: 11 additions & 22 deletions external/epsearchast/v3/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func TestValidObjectWithEqReturnsAst(t *testing.T) {
jsonTxt := `
{
"type": "EQ",
"first_arg": "status",
"second_arg": "paid"
"args": [ "status", "paid"]
}
`
// Execute SUT
Expand All @@ -63,8 +62,7 @@ func TestValidObjectWithLeReturnsAst(t *testing.T) {
jsonTxt := `
{
"type": "LE",
"first_arg": "orders",
"second_arg": "5"
"args": [ "orders", "5"]
}
`
// Execute SUT
Expand All @@ -81,8 +79,7 @@ func TestValidObjectWithLtReturnsAst(t *testing.T) {
jsonTxt := `
{
"type": "LT",
"first_arg": "orders",
"second_arg": "5"
"args": [ "orders", "5"]
}
`
// Execute SUT
Expand All @@ -99,8 +96,7 @@ func TestValidObjectWithGeReturnsAst(t *testing.T) {
jsonTxt := `
{
"type": "GE",
"first_arg": "orders",
"second_arg": "5"
"args": [ "orders", "5"]
}
`
// Execute SUT
Expand All @@ -117,8 +113,7 @@ func TestValidObjectWithGtReturnsAst(t *testing.T) {
jsonTxt := `
{
"type": "GT",
"first_arg": "orders",
"second_arg": "5"
"args": [ "orders", "5"]
}
`
// Execute SUT
Expand All @@ -135,8 +130,7 @@ func TestValidObjectWithLikeReturnsAst(t *testing.T) {
jsonTxt := `
{
"type": "LIKE",
"first_arg": "status",
"second_arg": "p*"
"args": [ "status", "p*"]
}
`
// Execute SUT
Expand All @@ -153,12 +147,10 @@ func TestEqWithChildReturnsError(t *testing.T) {
jsonTxt := `
{
"type": "EQ",
"first_arg": "status",
"second_arg": "paid",
"args": [ "status", "paid"],
"children": [{
"type": "EQ",
"first_arg": "status",
"second_arg": "paid"
"args": [ "status", "paid"]
}]
}
`
Expand Down Expand Up @@ -233,8 +225,7 @@ func TestInWithChildReturnsError(t *testing.T) {
"args": ["status", "paid"],
"children": [{
"type": "EQ",
"first_arg": "status",
"second_arg": "paid"
"args": [ "status", "paid"]
}]
}
`
Expand All @@ -255,8 +246,7 @@ func TestAndReturnsErrorWithOneChildren(t *testing.T) {
"type": "AND",
"children": [{
"type": "EQ",
"first_arg": "status",
"second_arg": "paid"
"args": [ "status", "paid"]
}]
}
`
Expand All @@ -277,8 +267,7 @@ func TestAndReturnsErrorWithAnInvalidChild(t *testing.T) {
"type": "AND",
"children": [{
"type": "EQ",
"first_arg": "status",
"second_arg": "paid"
"args": [ "status", "paid"]
},
{
"type": "FOO"
Expand Down
66 changes: 22 additions & 44 deletions external/epsearchast/v3/ast_visitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ func TestPreAndPostAndLeCalledOnAccept(t *testing.T) {
jsonTxt := `
{
"type": "LE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -39,8 +38,7 @@ func TestPreAndPostAndLtCalledOnAccept(t *testing.T) {
jsonTxt := `
{
"type": "LT",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -66,8 +64,7 @@ func TestPreAndPostAndGeCalledOnAccept(t *testing.T) {
jsonTxt := `
{
"type": "GE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -93,8 +90,7 @@ func TestPreAndPostAndGtCalledOnAccept(t *testing.T) {
jsonTxt := `
{
"type": "GT",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -120,8 +116,7 @@ func TestPreAndPostAndEqCalledOnAccept(t *testing.T) {
jsonTxt := `
{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -147,8 +142,7 @@ func TestPreAndPostAndLikeCalledOnAccept(t *testing.T) {
jsonTxt := `
{
"type": "LIKE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand Down Expand Up @@ -204,8 +198,7 @@ func TestPreOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "LE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -228,8 +221,7 @@ func TestPreVisitAndLeAndPostVisitCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "LE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -253,8 +245,7 @@ func TestPreAndLeCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "LE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -278,8 +269,7 @@ func TestPreAndLtCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "LT",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -304,8 +294,7 @@ func TestPreAndGeCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "GE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -330,8 +319,7 @@ func TestPreAndGtCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "GT",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -356,8 +344,7 @@ func TestPreAndEqCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand All @@ -382,8 +369,7 @@ func TestPreAndLikeCalledOnAcceptWithError(t *testing.T) {
jsonTxt := `
{
"type": "LIKE",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}
`

Expand Down Expand Up @@ -439,12 +425,10 @@ func TestPreAndPostAndEqAndAndCalledOnAccept(t *testing.T) {
"children": [
{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
},{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}]
}
`
Expand Down Expand Up @@ -476,12 +460,10 @@ func TestPreAndPreVisitAndCalledOnAcceptWithError(t *testing.T) {
"children": [
{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
},{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}]
}
`
Expand Down Expand Up @@ -509,12 +491,10 @@ func TestPreAndPreVisitAndEqCalledOnAcceptWithError(t *testing.T) {
"children": [
{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
},{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}]
}
`
Expand Down Expand Up @@ -543,12 +523,10 @@ func TestPreAndPreVisitAndEqAndPostVisitCalledOnAcceptWithError(t *testing.T) {
"children": [
{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
},{
"type": "EQ",
"first_arg": "amount",
"second_arg": "5"
"args": [ "amount", "5"]
}]
}
`
Expand Down
Loading

0 comments on commit 57be93d

Please sign in to comment.