Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing options, including option to skip automatic date parsing #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions EvaluableExpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,32 @@ type EvaluableExpression struct {
inputExpression string
}

// An Option is used to define the behavior of the expression parser.
type Option func(*options)

type options struct {
skipDateParsing bool
}

func getOpts(o ...Option) (res options) {
for _, f := range o {
f(&res)
}
return
}

var (
// The SkipDateParsing disables the automatic attempt to parse strings as dates.
// By default, date parsing is automatically tried with any string constant
SkipDateParsing Option = skipDateParsing
)

var (
skipDateParsing = func(o *options) {
o.skipDateParsing = true
}
)

/*
Parses a new EvaluableExpression from the given [expression] string.
Returns an error if the given expression has invalid syntax.
Expand Down Expand Up @@ -87,16 +113,17 @@ func NewEvaluableExpressionFromTokens(tokens []ExpressionToken) (*EvaluableExpre
Similar to [NewEvaluableExpression], except enables the use of user-defined functions.
Functions passed into this will be available to the expression.
*/
func NewEvaluableExpressionWithFunctions(expression string, functions map[string]ExpressionFunction) (*EvaluableExpression, error) {
func NewEvaluableExpressionWithFunctions(expression string, functions map[string]ExpressionFunction, opts ...Option) (*EvaluableExpression, error) {

var ret *EvaluableExpression
var err error

o := getOpts(opts...)
ret = new(EvaluableExpression)
ret.QueryDateFormat = isoDateFormat
ret.inputExpression = expression

ret.tokens, err = parseTokens(expression, functions)
ret.tokens, err = parseTokens(expression, functions, o)
if err != nil {
return nil, err
}
Expand Down
19 changes: 10 additions & 9 deletions parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"unicode"
)

func parseTokens(expression string, functions map[string]ExpressionFunction) ([]ExpressionToken, error) {
func parseTokens(expression string, functions map[string]ExpressionFunction, opts options) ([]ExpressionToken, error) {

var ret []ExpressionToken
var token ExpressionToken
Expand All @@ -25,7 +25,7 @@ func parseTokens(expression string, functions map[string]ExpressionFunction) ([]

for stream.canRead() {

token, err, found = readToken(stream, state, functions)
token, err, found = readToken(stream, state, functions, opts)

if err != nil {
return ret, err
Expand All @@ -52,7 +52,7 @@ func parseTokens(expression string, functions map[string]ExpressionFunction) ([]
return ret, nil
}

func readToken(stream *lexerStream, state lexerState, functions map[string]ExpressionFunction) (ExpressionToken, error, bool) {
func readToken(stream *lexerStream, state lexerState, functions map[string]ExpressionFunction, opts options) (ExpressionToken, error, bool) {

var function ExpressionFunction
var ret ExpressionToken
Expand Down Expand Up @@ -214,12 +214,13 @@ func readToken(stream *lexerStream, state lexerState, functions map[string]Expre
stream.rewind(-1)

// check to see if this can be parsed as a time.
tokenTime, found = tryParseTime(tokenValue.(string))
if found {
kind = TIME
tokenValue = tokenTime
} else {
kind = STRING
kind = STRING
if !opts.skipDateParsing {
tokenTime, found = tryParseTime(tokenValue.(string))
if found {
kind = TIME
tokenValue = tokenTime
}
}
break
}
Expand Down