-
Notifications
You must be signed in to change notification settings - Fork 67
/
registry.go
59 lines (44 loc) 路 1009 Bytes
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package resourcemanager
import (
"errors"
"sort"
"github.com/agnivade/levenshtein"
)
type Registry struct {
resources map[string]Client
}
func NewRegistry() *Registry {
return &Registry{
resources: make(map[string]Client),
}
}
func (r *Registry) Register(c Client) *Registry {
r.resources[c.resourceName] = c
return r
}
var ErrResourceNotFound = errors.New("resource not found")
func (r *Registry) Get(resourceName string) (Client, error) {
c, ok := r.resources[resourceName]
if !ok {
return Client{}, ErrResourceNotFound
}
return c, nil
}
func (r *Registry) List() []string {
var resources []string
for k := range r.resources {
resources = append(resources, k)
}
sort.Strings(resources)
return resources
}
const minDistanceForSuggestion = 2
func (r *Registry) Suggest(input string) string {
for resource := range r.resources {
distance := levenshtein.ComputeDistance(input, resource)
if distance <= minDistanceForSuggestion {
return resource
}
}
return ""
}