-
Notifications
You must be signed in to change notification settings - Fork 85
/
mock_directory.go
67 lines (57 loc) · 1.54 KB
/
mock_directory.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 identity
import (
"context"
"fmt"
"github.com/bluesky-social/indigo/atproto/syntax"
)
// A fake identity directory, for use in tests
type MockDirectory struct {
Handles map[syntax.Handle]syntax.DID
Identities map[syntax.DID]Identity
}
var _ Directory = (*MockDirectory)(nil)
func NewMockDirectory() MockDirectory {
return MockDirectory{
Handles: make(map[syntax.Handle]syntax.DID),
Identities: make(map[syntax.DID]Identity),
}
}
func (d *MockDirectory) Insert(ident Identity) {
if !ident.Handle.IsInvalidHandle() {
d.Handles[ident.Handle] = ident.DID
}
d.Identities[ident.DID] = ident
}
func (d *MockDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) {
h = h.Normalize()
did, ok := d.Handles[h]
if !ok {
return nil, ErrHandleNotFound
}
ident, ok := d.Identities[did]
if !ok {
return nil, ErrDIDNotFound
}
return &ident, nil
}
func (d *MockDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) {
ident, ok := d.Identities[did]
if !ok {
return nil, ErrDIDNotFound
}
return &ident, nil
}
func (d *MockDirectory) Lookup(ctx context.Context, a syntax.AtIdentifier) (*Identity, error) {
handle, err := a.AsHandle()
if nil == err { // if not an error, is a Handle
return d.LookupHandle(ctx, handle)
}
did, err := a.AsDID()
if nil == err { // if not an error, is a DID
return d.LookupDID(ctx, did)
}
return nil, fmt.Errorf("at-identifier neither a Handle nor a DID")
}
func (d *MockDirectory) Purge(ctx context.Context, a syntax.AtIdentifier) error {
return nil
}