-
Notifications
You must be signed in to change notification settings - Fork 0
/
id.go
67 lines (50 loc) · 1.5 KB
/
id.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
65
66
67
package id
import (
_ "github.com/aaronland/go-uid-whosonfirst"
_ "github.com/aaronland/go-uid-proxy"
)
import (
"context"
"fmt"
"github.com/aaronland/go-uid"
)
// type Provider is an interface for providing uniquer identifiers.
type Provider interface {
// NewID returns a new unique 64-bit integers.
NewID(context.Context) (int64, error)
}
// WOFProvider implements the Provider interface for generating unique Who's On First identifiers.
type WOFProvider struct {
Provider
uid_provider uid.Provider
}
// NweProvider returns a new `WOFProvider` instance configured with default
// settings.
func NewProvider(ctx context.Context) (Provider, error) {
uri := "proxy://?provider=whosonfirst://"
return NewProviderWithURI(ctx, uri)
}
// NewProviderWithURI returns a new `WOFProvider` instance configured by
// 'uri' which is expected to be a valid `aaronland/go-uid-proxy` URI.
func NewProviderWithURI(ctx context.Context, uri string) (Provider, error) {
uid_pr, err := uid.NewProvider(ctx, uri)
if err != nil {
return nil, fmt.Errorf("Failed to create new provider, %w", err)
}
wof_pr := &WOFProvider{
uid_provider: uid_pr,
}
return wof_pr, nil
}
// NewID returns a new Who's On First identifier.
func (wof_pr *WOFProvider) NewID(ctx context.Context) (int64, error) {
v, err := wof_pr.uid_provider.UID(ctx)
if err != nil {
return -1, fmt.Errorf("Failed to generate ID, %w", err)
}
id, ok := uid.AsInt64(v)
if !ok {
return -1, fmt.Errorf("Provider return invalid value")
}
return id, nil
}