summaryrefslogtreecommitdiff
path: root/internal/mapr
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2021-12-14 10:27:23 +0000
committerPaul Buetow <pbuetow@mimecast.com>2021-12-14 10:27:23 +0000
commitb1f3760dc2f452c3dba7883a538fd14d62a581e9 (patch)
tree2266c925aaf71562825dd50d75600a2be691f9f2 /internal/mapr
parent242d419f1b31755d1d1b3d1a1fd0e7bf61f7768e (diff)
Refactor makeWhereConditions
Diffstat (limited to 'internal/mapr')
-rw-r--r--internal/mapr/wherecondition.go95
1 files changed, 53 insertions, 42 deletions
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: