70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
package query
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func MatchKeys(line string, entries *[]ISearchable) (*QueryObject, error) {
|
|
|
|
var Key string = ""
|
|
|
|
// Sanitize and assume properties for each of the keywords
|
|
terms := []*QTerm{}
|
|
for _, val := range strings.Split(line, " ") {
|
|
|
|
if val == "" {
|
|
continue
|
|
}
|
|
|
|
term := QTerm{
|
|
Org: val,
|
|
Value: strings.Trim(val, " "),
|
|
}
|
|
terms = append(terms, &term)
|
|
}
|
|
|
|
lookForKey:
|
|
for _, term := range terms {
|
|
for _, entry := range *entries {
|
|
if term.Value == *entry.GetKey() {
|
|
Key = term.Value
|
|
term.IsKey = true
|
|
break lookForKey
|
|
}
|
|
}
|
|
}
|
|
|
|
matches := []ISearchable{}
|
|
|
|
if Key != "" && len(terms) == 1 {
|
|
for _, entry := range *entries {
|
|
if Key == *entry.GetKey() {
|
|
matches = append(matches, entry)
|
|
}
|
|
}
|
|
return &QueryObject{Key: Key, Results: &matches, SearchTerms: &terms}, nil
|
|
} else if 0 < len(terms) {
|
|
for _, entry := range *entries {
|
|
anyFailed := false
|
|
for _, term := range terms {
|
|
if term.IsKey {
|
|
continue
|
|
}
|
|
|
|
if !((Key == "" || Key == *entry.GetKey()) && entry.Test(term.Value)) {
|
|
anyFailed = true
|
|
break
|
|
}
|
|
}
|
|
|
|
if !anyFailed {
|
|
matches = append(matches, entry)
|
|
}
|
|
}
|
|
return &QueryObject{Key: Key, Results: &matches, SearchTerms: &terms}, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("Cannot parse: %s", line)
|
|
}
|