Added kmb and refactored query.Parse
This commit is contained in:
74
query/query.go
Normal file
74
query/query.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type QTerm struct {
|
||||
Org string
|
||||
Value string
|
||||
IsKey bool
|
||||
}
|
||||
|
||||
type QueryObject struct {
|
||||
Key string
|
||||
SearchTerms *[] *QTerm
|
||||
Results *[] ISearchable
|
||||
}
|
||||
|
||||
func Parse( 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 {
|
||||
for _, term := range terms {
|
||||
if term.IsKey {
|
||||
continue
|
||||
}
|
||||
if ( Key == "" || Key == *entry.GetKey() ) && entry.Test( term.Value ) {
|
||||
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