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.
(indexFunc IndexFunc)
| 49 | // unique values for every object. This is conversion can create errors when more than one key is found. You |
| 50 | // should prefer to make proper key and index functions. |
| 51 | func IndexFuncToKeyFuncAdapter(indexFunc IndexFunc) KeyFunc { |
| 52 | return func(obj interface{}) (string, error) { |
| 53 | indexKeys, err := indexFunc(obj) |
| 54 | if err != nil { |
| 55 | return "", err |
| 56 | } |
| 57 | if len(indexKeys) > 1 { |
| 58 | return "", fmt.Errorf("too many keys: %v", indexKeys) |
| 59 | } |
| 60 | if len(indexKeys) == 0 { |
| 61 | return "", fmt.Errorf("unexpected empty indexKeys") |
| 62 | } |
| 63 | return indexKeys[0], nil |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | const ( |
| 68 | NamespaceIndex string = "namespace" |