Refactoring codes for more tg message types
This commit is contained in:
69
query/match_keys.go
Normal file
69
query/match_keys.go
Normal file
@@ -0,0 +1,69 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user