diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2021-12-14 10:27:23 +0000 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2021-12-14 10:27:23 +0000 |
| commit | b1f3760dc2f452c3dba7883a538fd14d62a581e9 (patch) | |
| tree | 2266c925aaf71562825dd50d75600a2be691f9f2 | |
| parent | 242d419f1b31755d1d1b3d1a1fd0e7bf61f7768e (diff) | |
Refactor makeWhereConditions
| -rw-r--r-- | doc/querylanguage.md | 4 | ||||
| -rw-r--r-- | internal/mapr/wherecondition.go | 95 |
2 files changed, 55 insertions, 44 deletions
diff --git a/doc/querylanguage.md b/doc/querylanguage.md index a603bd9..725b635 100644 --- a/doc/querylanguage.md +++ b/doc/querylanguage.md @@ -48,7 +48,7 @@ CONDITION := ARG1 OPERATOR ARG2 ARG := FIELD|FLOAT|STRING OPERATOR := FLOATOPERATOR|STRINGOPERATOR FLOATOPERATOR := One of: == != < <= > >= -STRINGOPERATOR := eq|ne|contains|lacks +STRINGOPERATOR := eq|ne|contains|ncontains|lacks|hasprefix|nhasprefix|hassuffix|nhassuffix ORDERFIELD := FIELD|AGGREGATION(FIELD) SET := VARIABLE = FLOAT|STRING|FIELD|FUNCTION(FIELD) LOGFORMAT := default|generic|generickv|... @@ -58,6 +58,6 @@ FUNCTION := md5sum|maskdigits *Notes:* -* `lacks` is the inverse of `contains` +* `lacks` is an alias for `ncontains` (not contains) * `rorder` stands for reverse order and is the inverse of `order` * Available fields (variables and barewords) vary from the log format used. Check out the [log format](./logformats.md) documentation for more information. diff --git a/internal/mapr/wherecondition.go b/internal/mapr/wherecondition.go index c2dd2a1..95e43d2 100644 --- a/internal/mapr/wherecondition.go +++ b/internal/mapr/wherecondition.go @@ -53,12 +53,12 @@ func (wc *whereCondition) String() string { } func makeWhereConditions(tokens []token) (where []whereCondition, err error) { - parse := func(tokens []token) (whereCondition, []token, error) { + // Helper to parse a where condition. + parse := func(tokens []token) (whereCondition, []token, error) { var wc whereCondition if len(tokens) < 3 { - err := errors.New(invalidQuery + "Not enough arguments in 'where' clause") - return wc, nil, err + return wc, nil, errors.New(invalidQuery + "Not enough arguments in 'where' clause") } whereOp := strings.ToLower(tokens[1].str) @@ -102,47 +102,12 @@ func makeWhereConditions(tokens []token) (where []whereCondition, err error) { "Unknown operation in 'where' clause: " + whereOp) } - wc.lString = tokens[0].str - wc.rString = tokens[2].str - - if wc.Operation > FloatOperation { - if !tokens[0].isBareword { - return wc, nil, errors.New(invalidQuery + - "Expected bareword at 'where' clause's lValue: " + tokens[0].str) - } - if f, err := strconv.ParseFloat(wc.lString, 64); err == nil { - wc.lFloat = f - wc.lType = Float - } else { - wc.lType = Field - } - - if !tokens[2].isBareword { - return wc, nil, errors.New(invalidQuery + - "Expected bareword at 'where' clause's rValue: " + tokens[2].str) - } - if f, err := strconv.ParseFloat(wc.rString, 64); err == nil { - wc.rFloat = f - wc.rType = Float - } else { - wc.rType = Field - } - return wc, tokens[3:], nil - } - - if tokens[0].isBareword { - wc.lType = Field - } else { - wc.lType = String - } - if tokens[2].isBareword { - wc.rType = Field - } else { - wc.rType = String - } - return wc, tokens[3:], nil + var err error + tokens, err = wc.fill(tokens) + return wc, tokens, err } + // Consume all where conditions. for len(tokens) > 0 { var wc whereCondition var err error @@ -156,6 +121,52 @@ func makeWhereConditions(tokens []token) (where []whereCondition, err error) { return } +// Fill a where condition. +func (wc *whereCondition) fill(tokens []token) ([]token, error) { + wc.lString = tokens[0].str + wc.rString = tokens[2].str + + if wc.Operation > FloatOperation { + if !tokens[0].isBareword { + return nil, errors.New(invalidQuery + + "Expected bareword at 'where' clause's lValue: " + tokens[0].str) + } + + if f, err := strconv.ParseFloat(wc.lString, 64); err == nil { + wc.lFloat = f + wc.lType = Float + } else { + wc.lType = Field + } + + if !tokens[2].isBareword { + return nil, errors.New(invalidQuery + + "Expected bareword at 'where' clause's rValue: " + tokens[2].str) + } + if f, err := strconv.ParseFloat(wc.rString, 64); err == nil { + wc.rFloat = f + wc.rType = Float + } else { + wc.rType = Field + } + return tokens[3:], nil + } + + if tokens[0].isBareword { + wc.lType = Field + } else { + wc.lType = String + } + + if tokens[2].isBareword { + wc.rType = Field + } else { + wc.rType = String + } + + return tokens[3:], nil +} + func (wc *whereCondition) floatClause(lValue float64, rValue float64) bool { switch wc.Operation { case FloatEq: |
