Clean up database/query package

This commit is contained in:
Daniel 2019-09-20 22:01:17 +02:00
parent ac6d9db456
commit 60dea8092f
7 changed files with 37 additions and 39 deletions

View file

@ -38,7 +38,7 @@ func (c *andCond) check() (err error) {
} }
func (c *andCond) string() string { func (c *andCond) string() string {
var all []string all := make([]string, 0, len(c.conditions))
for _, cond := range c.conditions { for _, cond := range c.conditions {
all = append(all, cond.string()) all = append(all, cond.string())
} }

View file

@ -28,7 +28,7 @@ func newIntCondition(key string, operator uint8, value interface{}) *intConditio
case int32: case int32:
parsedValue = int64(v) parsedValue = int64(v)
case int64: case int64:
parsedValue = int64(v) parsedValue = v
case uint: case uint:
parsedValue = int64(v) parsedValue = int64(v)
case uint8: case uint8:

View file

@ -38,7 +38,7 @@ func (c *orCond) check() (err error) {
} }
func (c *orCond) string() string { func (c *orCond) string() string {
var all []string all := make([]string, 0, len(c.conditions))
for _, cond := range c.conditions { for _, cond := range c.conditions {
all = append(all, cond.string()) all = append(all, cond.string())
} }

View file

@ -205,7 +205,7 @@ func parseAndOr(getSnippet func() (*snippet, error), remainingSnippets func() in
for { for {
if !expectingMore && rootCondition && remainingSnippets() == 0 { if !expectingMore && rootCondition && remainingSnippets() == 0 {
// advance snippetsPos by one, as it will be set back by 1 // advance snippetsPos by one, as it will be set back by 1
getSnippet() getSnippet() //nolint:errcheck
if len(conditions) == 1 { if len(conditions) == 1 {
return conditions[0], nil return conditions[0], nil
} }
@ -330,7 +330,7 @@ func parseCondition(firstSnippet *snippet, getSnippet func() (*snippet, error))
} }
var ( var (
escapeReplacer = regexp.MustCompile("\\\\([^\\\\])") escapeReplacer = regexp.MustCompile(`\\([^\\])`)
) )
// prepToken removes surrounding parenthesis and escape characters. // prepToken removes surrounding parenthesis and escape characters.

View file

@ -10,36 +10,36 @@ import (
func TestExtractSnippets(t *testing.T) { func TestExtractSnippets(t *testing.T) {
text1 := `query test: where ( "bananas" > 100 and monkeys.# <= "12")or(coconuts < 10 "and" area > 50) or name sameas Julian or name matches ^King\ ` text1 := `query test: where ( "bananas" > 100 and monkeys.# <= "12")or(coconuts < 10 "and" area > 50) or name sameas Julian or name matches ^King\ `
result1 := []*snippet{ result1 := []*snippet{
&snippet{text: "query", globalPosition: 1}, {text: "query", globalPosition: 1},
&snippet{text: "test:", globalPosition: 7}, {text: "test:", globalPosition: 7},
&snippet{text: "where", globalPosition: 13}, {text: "where", globalPosition: 13},
&snippet{text: "(", globalPosition: 19}, {text: "(", globalPosition: 19},
&snippet{text: "bananas", globalPosition: 21}, {text: "bananas", globalPosition: 21},
&snippet{text: ">", globalPosition: 31}, {text: ">", globalPosition: 31},
&snippet{text: "100", globalPosition: 33}, {text: "100", globalPosition: 33},
&snippet{text: "and", globalPosition: 37}, {text: "and", globalPosition: 37},
&snippet{text: "monkeys.#", globalPosition: 41}, {text: "monkeys.#", globalPosition: 41},
&snippet{text: "<=", globalPosition: 51}, {text: "<=", globalPosition: 51},
&snippet{text: "12", globalPosition: 54}, {text: "12", globalPosition: 54},
&snippet{text: ")", globalPosition: 58}, {text: ")", globalPosition: 58},
&snippet{text: "or", globalPosition: 59}, {text: "or", globalPosition: 59},
&snippet{text: "(", globalPosition: 61}, {text: "(", globalPosition: 61},
&snippet{text: "coconuts", globalPosition: 62}, {text: "coconuts", globalPosition: 62},
&snippet{text: "<", globalPosition: 71}, {text: "<", globalPosition: 71},
&snippet{text: "10", globalPosition: 73}, {text: "10", globalPosition: 73},
&snippet{text: "and", globalPosition: 76}, {text: "and", globalPosition: 76},
&snippet{text: "area", globalPosition: 82}, {text: "area", globalPosition: 82},
&snippet{text: ">", globalPosition: 87}, {text: ">", globalPosition: 87},
&snippet{text: "50", globalPosition: 89}, {text: "50", globalPosition: 89},
&snippet{text: ")", globalPosition: 91}, {text: ")", globalPosition: 91},
&snippet{text: "or", globalPosition: 93}, {text: "or", globalPosition: 93},
&snippet{text: "name", globalPosition: 96}, {text: "name", globalPosition: 96},
&snippet{text: "sameas", globalPosition: 101}, {text: "sameas", globalPosition: 101},
&snippet{text: "Julian", globalPosition: 108}, {text: "Julian", globalPosition: 108},
&snippet{text: "or", globalPosition: 115}, {text: "or", globalPosition: 115},
&snippet{text: "name", globalPosition: 118}, {text: "name", globalPosition: 118},
&snippet{text: "matches", globalPosition: 123}, {text: "matches", globalPosition: 123},
&snippet{text: "^King ", globalPosition: 131}, {text: "^King ", globalPosition: 131},
} }
snippets, err := extractSnippets(text1) snippets, err := extractSnippets(text1)

View file

@ -96,10 +96,7 @@ func (q *Query) IsChecked() bool {
// MatchesKey checks whether the query matches the supplied database key (key without database prefix). // MatchesKey checks whether the query matches the supplied database key (key without database prefix).
func (q *Query) MatchesKey(dbKey string) bool { func (q *Query) MatchesKey(dbKey string) bool {
if !strings.HasPrefix(dbKey, q.dbKeyPrefix) { return strings.HasPrefix(dbKey, q.dbKeyPrefix)
return false
}
return true
} }
// MatchesRecord checks whether the query matches the supplied database record (value only). // MatchesRecord checks whether the query matches the supplied database record (value only).

View file

@ -1,3 +1,4 @@
//nolint:unparam
package query package query
import ( import (