Skip to content

Commit a86f3ae

Browse files
authored
identify.NewCacheDirectory, apidir.NewAPIDirectory, identity.NewMockDirectory return ptr values (#1318)
Updates #1313 This PR addresses the first three items recorded in #1313. `identify.NewCacheDirectory`, `apidir.NewAPIDirectory`, and `identity.NewMockDirectory` now return a reference to their respective values. In all cases this is a quality of life improvement, to implement the `identify.Directory` and `identity.Resolver` interfaces, a pointer receiver is required, forcing the caller to store, then reference the temporary value. In the case of `identify.NewCacheDirectory` and `identity.NewMockDirectory`, returning by value was a correctness issue as those types contain fields which should not be copied.
2 parents 57c106d + 2e1dd15 commit a86f3ae

18 files changed

Lines changed: 48 additions & 42 deletions

File tree

atproto/atclient/password_auth_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestPasswordAuth(t *testing.T) {
131131

132132
{
133133
// simple GET requests, with token expire/retry
134-
c, err := LoginWithPassword(ctx, &dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
134+
c, err := LoginWithPassword(ctx, dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
135135
require.NoError(err)
136136
err = c.Get(ctx, syntax.NSID("com.example.get"), nil, nil)
137137
assert.NoError(err)
@@ -166,7 +166,7 @@ func TestPasswordAuth(t *testing.T) {
166166

167167
{
168168
// logout
169-
c, err := LoginWithPassword(ctx, &dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
169+
c, err := LoginWithPassword(ctx, dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
170170
require.NoError(err)
171171

172172
passAuth, ok := c.Auth.(*PasswordAuth)
@@ -177,7 +177,7 @@ func TestPasswordAuth(t *testing.T) {
177177

178178
{
179179
// simple POST request, with token expire/retry
180-
c, err := LoginWithPassword(ctx, &dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
180+
c, err := LoginWithPassword(ctx, dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
181181
require.NoError(err)
182182
body := map[string]any{
183183
"a": 123,
@@ -192,7 +192,7 @@ func TestPasswordAuth(t *testing.T) {
192192

193193
{
194194
// POST with bytes.Buffer body
195-
c, err := LoginWithPassword(ctx, &dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
195+
c, err := LoginWithPassword(ctx, dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
196196
require.NoError(err)
197197
body := bytes.NewBufferString("some text")
198198
req := NewAPIRequest(MethodProcedure, syntax.NSID("com.example.expire"), body)
@@ -204,7 +204,7 @@ func TestPasswordAuth(t *testing.T) {
204204

205205
{
206206
// POST with file on disk (can seek and retry)
207-
c, err := LoginWithPassword(ctx, &dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
207+
c, err := LoginWithPassword(ctx, dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
208208
require.NoError(err)
209209
f, err := os.Open("testdata/body.json")
210210
require.NoError(err)
@@ -217,7 +217,7 @@ func TestPasswordAuth(t *testing.T) {
217217

218218
{
219219
// POST with pipe reader (can *not* retry)
220-
c, err := LoginWithPassword(ctx, &dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
220+
c, err := LoginWithPassword(ctx, dir, syntax.Handle("user1.example.com").AtIdentifier(), "password1", "", nil)
221221
require.NoError(err)
222222
r1, w1 := io.Pipe()
223223
go func() {

atproto/auth/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func TestServiceAuthMiddleware(t *testing.T) {
9696

9797
v := ServiceAuthValidator{
9898
Audience: aud,
99-
Dir: &dir,
99+
Dir: dir,
100100
}
101101

102102
{

atproto/auth/jwt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func testSigningValidation(t *testing.T, priv atcrypto.PrivateKey) {
110110

111111
v := ServiceAuthValidator{
112112
Audience: aud,
113-
Dir: &dir,
113+
Dir: dir,
114114
}
115115

116116
t1, err := SignServiceAuth(iss, aud, time.Minute, nil, priv)

atproto/identity/apidir/apidir.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ type handleBody struct {
4040
DID syntax.DID `json:"did"`
4141
}
4242

43-
func NewAPIDirectory(host string) APIDirectory {
44-
return APIDirectory{
43+
func NewAPIDirectory(host string) *APIDirectory {
44+
return &APIDirectory{
4545
Client: &http.Client{
4646
Timeout: time.Second * 10,
4747
Transport: &http.Transport{

atproto/identity/cache_directory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ type identityEntry struct {
3737
var _ Directory = (*CacheDirectory)(nil)
3838

3939
// Capacity of zero means unlimited size. Similarly, ttl of zero means unlimited duration.
40-
func NewCacheDirectory(inner Directory, capacity int, hitTTL, errTTL, invalidHandleTTL time.Duration) CacheDirectory {
41-
return CacheDirectory{
40+
func NewCacheDirectory(inner Directory, capacity int, hitTTL, errTTL, invalidHandleTTL time.Duration) *CacheDirectory {
41+
return &CacheDirectory{
4242
ErrTTL: errTTL,
4343
InvalidHandleTTL: invalidHandleTTL,
4444
Inner: inner,

atproto/identity/directory.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,5 @@ func DefaultDirectory() Directory {
8585
SkipDNSDomainSuffixes: []string{".bsky.social"},
8686
UserAgent: "indigo-identity/" + versioninfo.Short(),
8787
}
88-
cached := NewCacheDirectory(&base, 250_000, time.Hour*24, time.Minute*2, time.Minute*5)
89-
return &cached
88+
return NewCacheDirectory(&base, 250_000, time.Hour*24, time.Minute*2, time.Minute*5)
9089
}

atproto/identity/live_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func TestCacheDirectory(t *testing.T) {
6666
inner := BaseDirectory{}
6767
d := NewCacheDirectory(&inner, 1000, time.Hour*1, time.Hour*1, time.Hour*1)
6868
for i := 0; i < 3; i = i + 1 {
69-
testDirectoryLive(t, &d)
69+
testDirectoryLive(t, d)
7070
}
7171
}
7272

atproto/identity/mock_directory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type MockDirectory struct {
1919
var _ Directory = (*MockDirectory)(nil)
2020
var _ Resolver = (*MockDirectory)(nil)
2121

22-
func NewMockDirectory() MockDirectory {
23-
return MockDirectory{
22+
func NewMockDirectory() *MockDirectory {
23+
return &MockDirectory{
2424
handles: make(map[syntax.Handle]syntax.DID),
2525
identities: make(map[syntax.DID]Identity),
2626
}

automod/capture/testing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func ProcessCaptureRules(eng *automod.Engine, capture AccountCapture) error {
4242
handle := capture.AccountMeta.Identity.Handle.String()
4343
dir := identity.NewMockDirectory()
4444
dir.Insert(*capture.AccountMeta.Identity)
45-
eng.Directory = &dir
45+
eng.Directory = dir
4646

4747
// initial identity rules
4848
identEvent := comatproto.SyncSubscribeRepos_Identity{

automod/engine/circuit_breaker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestTakedownCircuitBreaker(t *testing.T) {
2929
ctx := context.Background()
3030
eng := EngineTestFixture()
3131
dir := identity.NewMockDirectory()
32-
eng.Directory = &dir
32+
eng.Directory = dir
3333
// note that this is a record-level action, not account-level
3434
eng.Rules = RuleSet{
3535
RecordRules: []RecordRuleFunc{
@@ -75,7 +75,7 @@ func TestReportCircuitBreaker(t *testing.T) {
7575
ctx := context.Background()
7676
eng := EngineTestFixture()
7777
dir := identity.NewMockDirectory()
78-
eng.Directory = &dir
78+
eng.Directory = dir
7979
eng.Rules = RuleSet{
8080
RecordRules: []RecordRuleFunc{
8181
alwaysReportRecordRule,

0 commit comments

Comments
 (0)