added empty query checks
This commit is contained in:
parent
3b458f6ff1
commit
0e2b25e788
3 changed files with 31 additions and 5 deletions
13
filter.go
13
filter.go
|
|
@ -101,11 +101,12 @@ func NewQuery(s string) (q *Query, err error) {
|
|||
return
|
||||
}
|
||||
qm := make(map[string]struct{})
|
||||
fields := strings.Split(s, " ")
|
||||
fields := clearFields(s)
|
||||
if len(fields) == 0 {
|
||||
err = errors.New("query is empty")
|
||||
return
|
||||
}
|
||||
for _, v := range fields {
|
||||
if v == "" {
|
||||
continue
|
||||
}
|
||||
if q.l == undefined {
|
||||
l, ok := parseLogic(v)
|
||||
q.l = l
|
||||
|
|
@ -160,8 +161,10 @@ func NewQuery(s string) (q *Query, err error) {
|
|||
boolFunc(m[1]),
|
||||
})
|
||||
}
|
||||
if len(q.m) == 0 {
|
||||
if len(q.m) == 0 && q.s.enabled {
|
||||
q.s.only = true
|
||||
} else if len(q.m) == 0 {
|
||||
err = errors.New("query is empty")
|
||||
}
|
||||
if len(q.m) == 1 && q.l != not {
|
||||
q.l = or
|
||||
|
|
|
|||
|
|
@ -69,6 +69,16 @@ func TestNewQuery(t *testing.T) {
|
|||
if err == nil {
|
||||
t.Error("invalid sort")
|
||||
}
|
||||
|
||||
_, err = NewQuery(" ")
|
||||
if err == nil {
|
||||
t.Error("empty query")
|
||||
}
|
||||
|
||||
_, err = NewQuery("OR")
|
||||
if err == nil {
|
||||
t.Error("empty query")
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
|||
13
helper.go
13
helper.go
|
|
@ -21,6 +21,19 @@ func checkFields(s []string, val string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func clearFields(s string) []string {
|
||||
a := strings.Split(s, " ")
|
||||
b := make([]string, len(a))
|
||||
c := 0
|
||||
for _, v := range a {
|
||||
if v != "" {
|
||||
b[c] = v
|
||||
c++
|
||||
}
|
||||
}
|
||||
return b[:c]
|
||||
}
|
||||
|
||||
type less func(i, j int) bool
|
||||
|
||||
func reverse(f less) less {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue