Added kmb and refactored query.Parse
This commit is contained in:
5
query/IQueryResult.go
Normal file
5
query/IQueryResult.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package query
|
||||
|
||||
type IQueryResult interface {
|
||||
Message() ( string, error )
|
||||
}
|
40
query/Searchable.go
Normal file
40
query/Searchable.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ISearchable interface {
|
||||
Test( string ) bool
|
||||
GetKey() *string
|
||||
}
|
||||
|
||||
type Searchable struct {
|
||||
Key *string
|
||||
SearchData *[] *string
|
||||
}
|
||||
|
||||
func ( this *Searchable ) Test( val string ) bool {
|
||||
|
||||
data := this.SearchData
|
||||
if data == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, v := range *data {
|
||||
if strings.Contains( *v, val ) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ( this *Searchable ) GetKey() *string {
|
||||
return this.Key
|
||||
}
|
||||
|
||||
type ByKey [] ISearchable
|
||||
|
||||
func (a ByKey) Len() int { return len(a) }
|
||||
func (a ByKey) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a ByKey) Less(i, j int) bool { return *a[i].GetKey() < *a[j].GetKey() }
|
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 )
|
||||
}
|
79
query/query_test.go
Normal file
79
query/query_test.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestItem struct {
|
||||
Searchable
|
||||
}
|
||||
|
||||
func TestQuery( t *testing.T ) {
|
||||
testItems := [] ISearchable{}
|
||||
|
||||
s1234 := "1234"
|
||||
sApple := "Apple"
|
||||
s0000 := "0000"
|
||||
|
||||
sDat0 := "Dat0"
|
||||
data0 := [] *string{}
|
||||
data0 = append( data0, &s1234 )
|
||||
data0 = append( data0, &sApple )
|
||||
t0 := TestItem{}
|
||||
t0.Key = &sDat0
|
||||
t0.SearchData = &data0
|
||||
testItems = append( testItems, &t0 )
|
||||
|
||||
sDat1 := "Dat1"
|
||||
data1 := [] *string{}
|
||||
data1 = append( data1, &s0000 )
|
||||
data1 = append( data1, &sApple )
|
||||
t1 := TestItem{}
|
||||
t1.Key = &sDat1
|
||||
t1.SearchData = &data1
|
||||
testItems = append( testItems, &t1 )
|
||||
|
||||
sDat2 := "Dat2"
|
||||
data2 := [] *string{}
|
||||
data2 = append( data2, &sDat0 )
|
||||
t2 := TestItem{}
|
||||
t2.Key = &sDat2
|
||||
t2.SearchData = &data2
|
||||
testItems = append( testItems, &t2 )
|
||||
|
||||
qo, err := Parse( "Apple", &testItems )
|
||||
if err != nil {
|
||||
t.Error( err )
|
||||
}
|
||||
|
||||
if len(*qo.Results) != 2 {
|
||||
t.Error( "Expected 2 results when searching for \"Apple\"" )
|
||||
}
|
||||
|
||||
qo, err = Parse( "0000", &testItems )
|
||||
if err != nil {
|
||||
t.Error( err )
|
||||
}
|
||||
|
||||
if len(*qo.Results) != 1 {
|
||||
t.Error( "Expected 1 results when searching for \"0000\"" )
|
||||
}
|
||||
|
||||
qo, err = Parse( "Dat0", &testItems )
|
||||
if err != nil {
|
||||
t.Error( err )
|
||||
}
|
||||
|
||||
if len(*qo.Results) != 1 {
|
||||
t.Error( "Expected 1 results when searching for \"Dat0\"" )
|
||||
}
|
||||
|
||||
qo, err = Parse( "Dat2 Dat0", &testItems )
|
||||
if err != nil {
|
||||
t.Error( err )
|
||||
}
|
||||
|
||||
if len(*qo.Results) != 1 {
|
||||
t.Error( "Expected 1 results when searching for \"Dat2 Dat0\"" )
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user