+27
atproto/identity/mock_directory.go
+27
atproto/identity/mock_directory.go
···
4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+
"sync"
7
8
8
9
"github.com/bluesky-social/indigo/atproto/syntax"
9
10
)
10
11
11
12
// A fake identity directory, for use in tests
12
13
type MockDirectory struct {
14
+
mu *sync.RWMutex
13
15
Handles map[syntax.Handle]syntax.DID
14
16
Identities map[syntax.DID]Identity
15
17
}
···
19
21
20
22
func NewMockDirectory() MockDirectory {
21
23
return MockDirectory{
24
+
mu: &sync.RWMutex{},
22
25
Handles: make(map[syntax.Handle]syntax.DID),
23
26
Identities: make(map[syntax.DID]Identity),
24
27
}
25
28
}
26
29
27
30
func (d *MockDirectory) Insert(ident Identity) {
31
+
d.mu.Lock()
32
+
defer d.mu.Unlock()
33
+
28
34
if !ident.Handle.IsInvalidHandle() {
29
35
d.Handles[ident.Handle.Normalize()] = ident.DID
30
36
}
···
32
38
}
33
39
34
40
func (d *MockDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) {
41
+
d.mu.RLock()
42
+
defer d.mu.RUnlock()
43
+
35
44
h = h.Normalize()
36
45
did, ok := d.Handles[h]
37
46
if !ok {
···
45
54
}
46
55
47
56
func (d *MockDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) {
57
+
d.mu.RLock()
58
+
defer d.mu.RUnlock()
59
+
48
60
ident, ok := d.Identities[did]
49
61
if !ok {
50
62
return nil, ErrDIDNotFound
···
53
65
}
54
66
55
67
func (d *MockDirectory) Lookup(ctx context.Context, a syntax.AtIdentifier) (*Identity, error) {
68
+
d.mu.RLock()
69
+
defer d.mu.RUnlock()
70
+
56
71
handle, err := a.AsHandle()
57
72
if nil == err { // if not an error, is a Handle
58
73
return d.LookupHandle(ctx, handle)
···
65
80
}
66
81
67
82
func (d *MockDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (syntax.DID, error) {
83
+
d.mu.RLock()
84
+
defer d.mu.RUnlock()
85
+
68
86
h = h.Normalize()
69
87
did, ok := d.Handles[h]
70
88
if !ok {
···
74
92
}
75
93
76
94
func (d *MockDirectory) ResolveDID(ctx context.Context, did syntax.DID) (*DIDDocument, error) {
95
+
d.mu.RLock()
96
+
defer d.mu.RUnlock()
97
+
77
98
ident, ok := d.Identities[did]
78
99
if !ok {
79
100
return nil, ErrDIDNotFound
···
83
104
}
84
105
85
106
func (d *MockDirectory) ResolveDIDRaw(ctx context.Context, did syntax.DID) (json.RawMessage, error) {
107
+
d.mu.RLock()
108
+
defer d.mu.RUnlock()
109
+
86
110
ident, ok := d.Identities[did]
87
111
if !ok {
88
112
return nil, ErrDIDNotFound
···
92
116
}
93
117
94
118
func (d *MockDirectory) Purge(ctx context.Context, a syntax.AtIdentifier) error {
119
+
d.mu.Lock()
120
+
defer d.mu.Unlock()
121
+
95
122
return nil
96
123
}