Refactoring codes for more tg message types

This commit is contained in:
2026-03-07 22:16:14 +08:00
parent a396a381b5
commit 912f9fd0ad
26 changed files with 771 additions and 472 deletions

69
query/match_keys.go Normal file
View 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)
}