support multiple index values for a single object

This commit is contained in:
deads2k
2015-07-28 10:01:05 -04:00
parent c5bffaaf31
commit 9386db8c99
8 changed files with 172 additions and 28 deletions

View File

@@ -30,18 +30,36 @@ type Indexer interface {
Index(indexName string, obj interface{}) ([]interface{}, error)
// ListIndexFuncValues returns the list of generated values of an Index func
ListIndexFuncValues(indexName string) []string
// ByIndex lists object that match on the named indexing function with the exact key
ByIndex(indexName, indexKey string) ([]interface{}, error)
}
// IndexFunc knows how to provide an indexed value for an object.
type IndexFunc func(obj interface{}) (string, error)
type IndexFunc func(obj interface{}) ([]string, error)
// IndexFuncToKeyFuncAdapter adapts an indexFunc to a keyFunc. This is only useful if your index function returns
// unique values for every object. This is conversion can create errors when more than one key is found. You
// should prefer to make proper key and index functions.
func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc {
return func(obj interface{}) (string, error) {
indexKeys, err := indexFunc(obj)
if err != nil {
return "", err
}
if len(indexKeys) > 1 {
return "", fmt.Errorf("too many keys: %v", indexKeys)
}
return indexKeys[0], nil
}
}
// MetaNamespaceIndexFunc is a default index function that indexes based on an object's namespace
func MetaNamespaceIndexFunc(obj interface{}) (string, error) {
func MetaNamespaceIndexFunc(obj interface{}) ([]string, error) {
meta, err := meta.Accessor(obj)
if err != nil {
return "", fmt.Errorf("object has no meta: %v", err)
return []string{""}, fmt.Errorf("object has no meta: %v", err)
}
return meta.Namespace(), nil
return []string{meta.Namespace()}, nil
}
// Index maps the indexed value to a set of keys in the store that match on that value