github.com/qri-io/qri@v0.10.1-0.20220104210721-c771715036cb/remote/registry/search.go (about) 1 package registry 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/qri-io/dataset" 8 ) 9 10 // Searchable is an opt-in interface for registries that wish to 11 // support search 12 type Searchable interface { 13 Search(p SearchParams) ([]*dataset.Dataset, error) 14 } 15 16 // Indexer is an interface for adding registry values to a search index 17 type Indexer interface { 18 // IndexDatasets adds one or more datasets to a search index 19 IndexDatasets([]*dataset.Dataset) error 20 // UnindexDatasets removes one or more datasets from a search index 21 UnindexDatasets([]*dataset.Dataset) error 22 } 23 24 // SearchParams encapsulates parameters provided to Searchable.Search 25 type SearchParams struct { 26 Q string 27 Limit, Offset int 28 } 29 30 // ErrSearchNotSupported is the canonical error to indicate search 31 // isn't implemented 32 var ErrSearchNotSupported = fmt.Errorf("search not supported") 33 34 // NilSearch is a basic implementation of Searchable which returns 35 // an error to indicate that search is not supported 36 type NilSearch bool 37 38 // Search returns an error indicating that search is not supported 39 func (ns NilSearch) Search(p SearchParams) ([]*dataset.Dataset, error) { 40 return nil, ErrSearchNotSupported 41 } 42 43 // MockSearch is a very naive implementation of search that wraps 44 // registry.Datasets and only checks for exact substring matches of 45 // dataset's meta.title property. It's mainly intended for testing 46 // purposes. 47 type MockSearch struct { 48 Datasets []*dataset.Dataset 49 } 50 51 // Search is a trivial search implementation used for testing 52 func (ms MockSearch) Search(p SearchParams) (results []*dataset.Dataset, err error) { 53 for _, ds := range ms.Datasets { 54 dsname := "" 55 if ds.Meta != nil { 56 dsname = strings.ToLower(ds.Meta.Title) 57 } 58 if strings.Contains(dsname, strings.ToLower(p.Q)) { 59 results = append(results, ds) 60 } 61 } 62 63 return results, nil 64 }