-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.go
64 lines (42 loc) · 1.02 KB
/
lookup.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
60
61
62
63
64
package architecture
import (
"context"
"net/url"
"github.com/aaronland/go-roster"
)
type Lookup interface {
Find(context.Context, string) ([]interface{}, error)
Append(context.Context, interface{}) error
}
var lookup_roster roster.Roster
type LookupInitializationFunc func(ctx context.Context, uri string) (Lookup, error)
func RegisterLookup(ctx context.Context, scheme string, init_func LookupInitializationFunc) error {
err := ensureLookupRoster()
if err != nil {
return err
}
return lookup_roster.Register(ctx, scheme, init_func)
}
func ensureLookupRoster() error {
if lookup_roster == nil {
r, err := roster.NewDefaultRoster()
if err != nil {
return err
}
lookup_roster = r
}
return nil
}
func NewLookup(ctx context.Context, uri string) (Lookup, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
scheme := u.Scheme
i, err := lookup_roster.Driver(ctx, scheme)
if err != nil {
return nil, err
}
init_func := i.(LookupInitializationFunc)
return init_func(ctx, uri)
}