mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Clean up database/query package
This commit is contained in:
parent
ac6d9db456
commit
60dea8092f
7 changed files with 37 additions and 39 deletions
|
@ -38,7 +38,7 @@ func (c *andCond) check() (err error) {
|
|||
}
|
||||
|
||||
func (c *andCond) string() string {
|
||||
var all []string
|
||||
all := make([]string, 0, len(c.conditions))
|
||||
for _, cond := range c.conditions {
|
||||
all = append(all, cond.string())
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ func newIntCondition(key string, operator uint8, value interface{}) *intConditio
|
|||
case int32:
|
||||
parsedValue = int64(v)
|
||||
case int64:
|
||||
parsedValue = int64(v)
|
||||
parsedValue = v
|
||||
case uint:
|
||||
parsedValue = int64(v)
|
||||
case uint8:
|
||||
|
|
|
@ -38,7 +38,7 @@ func (c *orCond) check() (err error) {
|
|||
}
|
||||
|
||||
func (c *orCond) string() string {
|
||||
var all []string
|
||||
all := make([]string, 0, len(c.conditions))
|
||||
for _, cond := range c.conditions {
|
||||
all = append(all, cond.string())
|
||||
}
|
||||
|
|
|
@ -205,7 +205,7 @@ func parseAndOr(getSnippet func() (*snippet, error), remainingSnippets func() in
|
|||
for {
|
||||
if !expectingMore && rootCondition && remainingSnippets() == 0 {
|
||||
// advance snippetsPos by one, as it will be set back by 1
|
||||
getSnippet()
|
||||
getSnippet() //nolint:errcheck
|
||||
if len(conditions) == 1 {
|
||||
return conditions[0], nil
|
||||
}
|
||||
|
@ -330,7 +330,7 @@ func parseCondition(firstSnippet *snippet, getSnippet func() (*snippet, error))
|
|||
}
|
||||
|
||||
var (
|
||||
escapeReplacer = regexp.MustCompile("\\\\([^\\\\])")
|
||||
escapeReplacer = regexp.MustCompile(`\\([^\\])`)
|
||||
)
|
||||
|
||||
// prepToken removes surrounding parenthesis and escape characters.
|
||||
|
|
|
@ -10,36 +10,36 @@ import (
|
|||
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\ `
|
||||
result1 := []*snippet{
|
||||
&snippet{text: "query", globalPosition: 1},
|
||||
&snippet{text: "test:", globalPosition: 7},
|
||||
&snippet{text: "where", globalPosition: 13},
|
||||
&snippet{text: "(", globalPosition: 19},
|
||||
&snippet{text: "bananas", globalPosition: 21},
|
||||
&snippet{text: ">", globalPosition: 31},
|
||||
&snippet{text: "100", globalPosition: 33},
|
||||
&snippet{text: "and", globalPosition: 37},
|
||||
&snippet{text: "monkeys.#", globalPosition: 41},
|
||||
&snippet{text: "<=", globalPosition: 51},
|
||||
&snippet{text: "12", globalPosition: 54},
|
||||
&snippet{text: ")", globalPosition: 58},
|
||||
&snippet{text: "or", globalPosition: 59},
|
||||
&snippet{text: "(", globalPosition: 61},
|
||||
&snippet{text: "coconuts", globalPosition: 62},
|
||||
&snippet{text: "<", globalPosition: 71},
|
||||
&snippet{text: "10", globalPosition: 73},
|
||||
&snippet{text: "and", globalPosition: 76},
|
||||
&snippet{text: "area", globalPosition: 82},
|
||||
&snippet{text: ">", globalPosition: 87},
|
||||
&snippet{text: "50", globalPosition: 89},
|
||||
&snippet{text: ")", globalPosition: 91},
|
||||
&snippet{text: "or", globalPosition: 93},
|
||||
&snippet{text: "name", globalPosition: 96},
|
||||
&snippet{text: "sameas", globalPosition: 101},
|
||||
&snippet{text: "Julian", globalPosition: 108},
|
||||
&snippet{text: "or", globalPosition: 115},
|
||||
&snippet{text: "name", globalPosition: 118},
|
||||
&snippet{text: "matches", globalPosition: 123},
|
||||
&snippet{text: "^King ", globalPosition: 131},
|
||||
{text: "query", globalPosition: 1},
|
||||
{text: "test:", globalPosition: 7},
|
||||
{text: "where", globalPosition: 13},
|
||||
{text: "(", globalPosition: 19},
|
||||
{text: "bananas", globalPosition: 21},
|
||||
{text: ">", globalPosition: 31},
|
||||
{text: "100", globalPosition: 33},
|
||||
{text: "and", globalPosition: 37},
|
||||
{text: "monkeys.#", globalPosition: 41},
|
||||
{text: "<=", globalPosition: 51},
|
||||
{text: "12", globalPosition: 54},
|
||||
{text: ")", globalPosition: 58},
|
||||
{text: "or", globalPosition: 59},
|
||||
{text: "(", globalPosition: 61},
|
||||
{text: "coconuts", globalPosition: 62},
|
||||
{text: "<", globalPosition: 71},
|
||||
{text: "10", globalPosition: 73},
|
||||
{text: "and", globalPosition: 76},
|
||||
{text: "area", globalPosition: 82},
|
||||
{text: ">", globalPosition: 87},
|
||||
{text: "50", globalPosition: 89},
|
||||
{text: ")", globalPosition: 91},
|
||||
{text: "or", globalPosition: 93},
|
||||
{text: "name", globalPosition: 96},
|
||||
{text: "sameas", globalPosition: 101},
|
||||
{text: "Julian", globalPosition: 108},
|
||||
{text: "or", globalPosition: 115},
|
||||
{text: "name", globalPosition: 118},
|
||||
{text: "matches", globalPosition: 123},
|
||||
{text: "^King ", globalPosition: 131},
|
||||
}
|
||||
|
||||
snippets, err := extractSnippets(text1)
|
||||
|
|
|
@ -96,10 +96,7 @@ func (q *Query) IsChecked() bool {
|
|||
|
||||
// MatchesKey checks whether the query matches the supplied database key (key without database prefix).
|
||||
func (q *Query) MatchesKey(dbKey string) bool {
|
||||
if !strings.HasPrefix(dbKey, q.dbKeyPrefix) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
return strings.HasPrefix(dbKey, q.dbKeyPrefix)
|
||||
}
|
||||
|
||||
// MatchesRecord checks whether the query matches the supplied database record (value only).
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//nolint:unparam
|
||||
package query
|
||||
|
||||
import (
|
||||
|
|
Loading…
Add table
Reference in a new issue