Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
brianvoe committed Mar 11, 2024
1 parent e3563c3 commit 87b337a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 13 deletions.
6 changes: 2 additions & 4 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package gofindit

type Document struct {
Original any
Values map[string]FieldValue

Filters []FilterFunc
Values map[string]Field

Check failure on line 5 in document.go

View workflow job for this annotation

GitHub Actions / Go 1.22.* ubuntu-latest

undefined: Field

Check failure on line 5 in document.go

View workflow job for this annotation

GitHub Actions / Go 1.22.* macos-latest

undefined: Field
}

func NewDoc(doc any) (*Document, error) {
Expand All @@ -20,7 +18,7 @@ func NewDocFilters(doc any, filters ...FilterFunc) (*Document, error) {
// Loop through values and
// if they are a string, tokenize them
for k, v := range values {
if v.Type == "string" {
if v.Type() == "string" {
// Tokenize the string
tokens, error := Tokenize(v.Value.(string), filters)

Check failure on line 23 in document.go

View workflow job for this annotation

GitHub Actions / Go 1.22.* ubuntu-latest

invalid operation: v.Value (value of type func() any) is not an interface

Check failure on line 23 in document.go

View workflow job for this annotation

GitHub Actions / Go 1.22.* macos-latest

invalid operation: v.Value (value of type func() any) is not an interface
if error != nil {
Expand Down
2 changes: 1 addition & 1 deletion index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func Example() {
type Test struct {
Name string `find:"Name"`
Name string `find:"Name" index:"leve_string"`
Age int `find:"Age"`
}

Expand Down
22 changes: 22 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ func (i *Index) Search(searchQuery SearchQuery) ([]any, error) {
return originalResults, nil
}

// intersection returns the intersection of two arrays
func intersection(a []int, b []int) []int {
maxLen := len(a)
if len(b) > maxLen {
maxLen = len(b)
}
r := make([]int, 0, maxLen)
var i, j int
for i < len(a) && j < len(b) {
if a[i] < b[j] {
i++
} else if a[i] > b[j] {
j++
} else {
r = append(r, a[i])
i++
j++
}
}
return r
}

// searchMatch checks if the value matches the query value
func isSearchMatch(value any, valueType string, queryValue any, queryValueType string) (bool, error) {
// If the value type and query value type are the same, then just compare them
Expand Down
12 changes: 4 additions & 8 deletions structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import (
"fmt"
"reflect"
"time"
)

type FieldValue struct {
Type string
Value any
Tokens []string
}
"github.com/brianvoe/gofindit/fields"
)

// getStructure returns an array of
func getStructure(v any, parent string) (map[string]FieldValue, error) {
func getStructure(v any, parent string) (map[string]fields.Field, error) {
// Make sure v is a struct
if reflect.TypeOf(v).Kind() != reflect.Struct {
return nil, fmt.Errorf("v is not a struct")
Expand All @@ -25,7 +21,7 @@ func getStructure(v any, parent string) (map[string]FieldValue, error) {
val = val.Elem()
}

fields := make(map[string]FieldValue)
fields := make(map[string]fields.Field)

// Loop through fields and add name and type to fields
for i := 0; i < val.NumField(); i++ {
Expand Down
60 changes: 60 additions & 0 deletions tokenize_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
package gofindit

import (
"reflect"
"testing"
)

func TestTokenize(t *testing.T) {
tests := []struct {
name string
str string
filters []FilterFunc
want []string
wantErr bool
}{
{
name: "empty",
str: "",
filters: []FilterFunc{},
want: []string{},
wantErr: false,
},
{
name: "single",
str: "Hello, World!",
filters: []FilterFunc{FilterLowercase},
want: []string{"hello", "world"},
wantErr: false,
},
{
name: "multiple",
str: "Hello, World and welcome!",
filters: []FilterFunc{FilterLowercase, FilterOutStopwords},
want: []string{"hello", "world", "welcome"},
wantErr: false,
},
{
name: "apostrophe",
str: "I'm a string with an apostrophe",
filters: []FilterFunc{
FilterLowercase,
FilterOutStopwords,
},
want: []string{"string", "apostrophe"},
wantErr: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := Tokenize(test.str, test.filters)
if (err != nil) != test.wantErr {
t.Errorf("Tokenize() error = %v, wantErr %v", err, test.wantErr)
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("Tokenize() = %v, want %v", got, test.want)
}
})
}
}

0 comments on commit 87b337a

Please sign in to comment.