-
-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Added Regexp operator * Added simple type check * Fixed linting issue
- Loading branch information
Showing
10 changed files
with
965 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package compiler_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"testing" | ||
|
||
"github.com/MontFerret/ferret/pkg/compiler" | ||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestRegexpOperator(t *testing.T) { | ||
Convey("Should be possible to use positive regular expression operator", t, func() { | ||
out := compiler.New(). | ||
MustCompile(` | ||
RETURN "foo" =~ "^f[o].$" | ||
`). | ||
MustRun(context.Background()) | ||
|
||
So(string(out), ShouldEqual, `true`) | ||
}) | ||
|
||
Convey("Should be possible to use negative regular expression operator", t, func() { | ||
out := compiler.New(). | ||
MustCompile(` | ||
RETURN "foo" !~ "[a-z]+bar$" | ||
`). | ||
MustRun(context.Background()) | ||
|
||
So(string(out), ShouldEqual, `true`) | ||
}) | ||
|
||
Convey("Should be possible to use negative regular expression operator", t, func() { | ||
c := compiler.New() | ||
c.RegisterFunction("T::REGEXP", func(_ context.Context, _ ...core.Value) (value core.Value, e error) { | ||
return values.NewString("[a-z]+bar$"), nil | ||
}) | ||
|
||
out := c. | ||
MustCompile(` | ||
RETURN "foo" !~ T::REGEXP() | ||
`). | ||
MustRun(context.Background()) | ||
|
||
So(string(out), ShouldEqual, `true`) | ||
}) | ||
|
||
Convey("Should return an error during compilation when a regexp string invalid", t, func() { | ||
_, err := compiler.New(). | ||
Compile(` | ||
RETURN "foo" !~ "[ ]\K(?<!\d )(?=(?: ?\d){8})(?!(?: ?\d){9})\d[ \d]+\d" | ||
`) | ||
|
||
So(err, ShouldBeError) | ||
}) | ||
|
||
Convey("Should return an error during compilation when a regexp is not a string", t, func() { | ||
right := []string{ | ||
"[]", | ||
"{}", | ||
"1", | ||
"1.1", | ||
"TRUE", | ||
} | ||
|
||
for _, r := range right { | ||
_, err := compiler.New(). | ||
Compile(fmt.Sprintf(` | ||
RETURN "foo" !~ %s | ||
`, r)) | ||
|
||
So(err, ShouldBeError) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package operators | ||
|
||
import ( | ||
"context" | ||
"github.com/MontFerret/ferret/pkg/runtime/core" | ||
"github.com/MontFerret/ferret/pkg/runtime/values" | ||
"regexp" | ||
) | ||
|
||
type ( | ||
RegexpOperatorType int | ||
RegexpOperator struct { | ||
*baseOperator | ||
opType RegexpOperatorType | ||
} | ||
) | ||
|
||
const ( | ||
RegexpOperatorTypeNegative RegexpOperatorType = 0 | ||
RegexpOperatorTypePositive RegexpOperatorType = 1 | ||
) | ||
|
||
var regexpOperators = map[string]RegexpOperatorType{ | ||
"!~": RegexpOperatorTypeNegative, | ||
"=~": RegexpOperatorTypePositive, | ||
} | ||
|
||
func NewRegexpOperator( | ||
src core.SourceMap, | ||
left core.Expression, | ||
right core.Expression, | ||
operator string, | ||
) (*RegexpOperator, error) { | ||
op, exists := regexpOperators[operator] | ||
|
||
if !exists { | ||
return nil, core.Error(core.ErrInvalidArgument, "operator") | ||
} | ||
|
||
return &RegexpOperator{ | ||
&baseOperator{ | ||
src, | ||
left, | ||
right, | ||
}, | ||
op, | ||
}, nil | ||
} | ||
|
||
func (operator *RegexpOperator) Type() RegexpOperatorType { | ||
return operator.opType | ||
} | ||
|
||
func (operator *RegexpOperator) Exec(ctx context.Context, scope *core.Scope) (core.Value, error) { | ||
left, err := operator.left.Exec(ctx, scope) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
right, err := operator.right.Exec(ctx, scope) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return operator.Eval(ctx, left, right) | ||
} | ||
|
||
func (operator *RegexpOperator) Eval(_ context.Context, left, right core.Value) (core.Value, error) { | ||
leftStr := left.String() | ||
rightStr := right.String() | ||
|
||
r, err := regexp.Compile(rightStr) | ||
|
||
if err != nil { | ||
return values.None, err | ||
} | ||
|
||
if operator.opType == RegexpOperatorTypePositive { | ||
return values.NewBoolean(r.MatchString(leftStr)), nil | ||
} | ||
|
||
return values.NewBoolean(!r.MatchString(leftStr)), nil | ||
} |