From 0a6db8376eb59a2fe44944ee174c09ec97a9500d Mon Sep 17 00:00:00 2001 From: "Lixia (Sylvia) Lei" Date: Fri, 22 Sep 2023 17:42:15 +0800 Subject: [PATCH] refactor: make credentials.NewMemoryStore return an interface Signed-off-by: Lixia (Sylvia) Lei --- registry/remote/credentials/memory_store.go | 14 +++++++------- registry/remote/credentials/memory_store_test.go | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/registry/remote/credentials/memory_store.go b/registry/remote/credentials/memory_store.go index 0315e87c..6eb7749b 100644 --- a/registry/remote/credentials/memory_store.go +++ b/registry/remote/credentials/memory_store.go @@ -22,18 +22,18 @@ import ( "oras.land/oras-go/v2/registry/remote/auth" ) -// MemoryStore is a store that keeps credentials in memory. -type MemoryStore struct { +// memoryStore is a store that keeps credentials in memory. +type memoryStore struct { store sync.Map } // NewMemoryStore creates a new in-memory credentials store. -func NewMemoryStore() *MemoryStore { - return &MemoryStore{} +func NewMemoryStore() Store { + return &memoryStore{} } // Get retrieves credentials from the store for the given server address. -func (ms *MemoryStore) Get(_ context.Context, serverAddress string) (auth.Credential, error) { +func (ms *memoryStore) Get(_ context.Context, serverAddress string) (auth.Credential, error) { cred, found := ms.store.Load(serverAddress) if !found { return auth.EmptyCredential, nil @@ -42,13 +42,13 @@ func (ms *MemoryStore) Get(_ context.Context, serverAddress string) (auth.Creden } // Put saves credentials into the store for the given server address. -func (ms *MemoryStore) Put(_ context.Context, serverAddress string, cred auth.Credential) error { +func (ms *memoryStore) Put(_ context.Context, serverAddress string, cred auth.Credential) error { ms.store.Store(serverAddress, cred) return nil } // Delete removes credentials from the store for the given server address. -func (ms *MemoryStore) Delete(_ context.Context, serverAddress string) error { +func (ms *memoryStore) Delete(_ context.Context, serverAddress string) error { ms.store.Delete(serverAddress) return nil } diff --git a/registry/remote/credentials/memory_store_test.go b/registry/remote/credentials/memory_store_test.go index f29c3592..6f08c8fc 100644 --- a/registry/remote/credentials/memory_store_test.go +++ b/registry/remote/credentials/memory_store_test.go @@ -40,7 +40,7 @@ func TestMemoryStore_Get_notExistRecord(t *testing.T) { func TestMemoryStore_Get_validRecord(t *testing.T) { ctx := context.Background() - ms := NewMemoryStore() + ms := NewMemoryStore().(*memoryStore) serverAddress := "registry.example.com" want := auth.Credential{