Skip to content

Commit

Permalink
refactor: change to switch case for various operators
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersiddhu committed Aug 23, 2022
1 parent 3e1a675 commit f9dbed2
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
`
arrQualEqualTmpl = `
LET %s = (
FILTER '%s' %s %s[*]
FILTER '%s' IN %s[*]
RETURN 1
)
`
Expand All @@ -50,6 +50,12 @@ const (
RETURN 1
)
`
arrQualNotEqualTmpl = `
LET %s = (
FILTER '%s' NOT IN %s[*]
RETURN 1
)
`
dateTmpl = "%s.%s %s DATE_ISO8601('%s')"
)

Expand Down Expand Up @@ -163,60 +169,69 @@ func GenQualifiedAQLFilterStatement(
fmap map[string]string,
filters []*Filter,
) (string, error) {
lmap := map[string]string{",": "OR", ";": "AND"}
omap := getOperatorMap()
dmap := getDateOperatorMap()
amap := getArrayOperatorMap()
stmts := make(map[string]*arraylist.List)
stmts["let"] = arraylist.New()
stmts["nonlet"] = arraylist.New()
stmts := map[string]*arraylist.List{
"let": arraylist.New(),
"nonlet": arraylist.New(),
}
for _, flt := range filters {
// check if operator is used for array item
if _, ok := amap[flt.Operator]; ok {
str := randString(strSeedLen)
if amap[flt.Operator] == "=~" {
switch {
case hasArrayOperator(flt.Operator):
randStr := randString(strSeedLen)
switch getArrayOpertaor(flt.Operator) {
stmts["let"].Insert(
0,
fmt.Sprintf(
arrQualMatchTmpl,
str,
randStr,
fmap[flt.Field],
flt.Value,
),
)
} else {
case "==":
stmts["let"].Insert(
0,
fmt.Sprintf(
arrQualEqualTmpl,
randStr,
flt.Value,
fmap[flt.Field],
),
)
case "!=":
stmts["let"].Insert(
0,
fmt.Sprintf(
arrQualEqualTmpl, str,
arrQualNotEqualTmpl,
randStr,
flt.Value,
arrToAQL[amap[flt.Operator]],
fmap[flt.Field],
))
}
stmts["nonlet"].Add(fmt.Sprintf("LENGTH(%s) > 0", str))
} else if _, ok := dmap[flt.Operator]; ok {
// validate date format
stmts["nonlet"].Add(fmt.Sprintf("LENGTH(%s) > 0", randStr))
case hasDateOperator(flt.Operator):
if err := dateValidator(flt.Value); err != nil {
return "", err
}
// write time conversion into AQL query
stmts["nonlet"].Add(fmt.Sprintf("%s %s DATE_ISO8601('%s')",
fmap[flt.Field], omap[flt.Operator], flt.Value,
fmap[flt.Field], getOperator(flt.Operator), flt.Value,
))
} else {
case hasOperator(flt.Operator):
// write the rest of AQL statement based on regular string data
stmts["nonlet"].Add(fmt.Sprintf(
"%s %s %s",
fmap[flt.Field], omap[flt.Operator],
fmap[flt.Field], getOperator(flt.Operator),
addQuoteToStrings(flt.Operator, flt.Value),
))
// if there's logic, write that too
default:
return "", fmt.Errorf(
"unknown opertaor for parsing %s",
flt.Operator,
)
}
// if there's logic, write that too
if len(flt.Logic) != 0 {
stmts["nonlet"].Add(fmt.Sprintf("\n %s ", lmap[flt.Logic]))
}
addLogic(stmts["nonlet"], flt)
}

return toFullStatement(stmts), nil
Expand Down

0 comments on commit f9dbed2

Please sign in to comment.