🏗️ Simplify metadata repository usage
This commit is contained in:
parent
2b25fcdfa8
commit
ce9e5534eb
|
@ -7,13 +7,40 @@ import (
|
||||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
"source.toby3d.me/toby3d/auth/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Repository interface {
|
type (
|
||||||
Create(ctx context.Context, u *url.URL, metadata domain.Metadata) error
|
Repository interface {
|
||||||
Get(ctx context.Context, u *url.URL) (*domain.Metadata, error)
|
Fetch(ctx context.Context, u *url.URL) (*domain.Metadata, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dummyMetadataRepository struct{}
|
||||||
|
|
||||||
|
stubMetadataRepository struct {
|
||||||
|
metadata *domain.Metadata
|
||||||
|
error error
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
var ErrNotExist error = domain.NewError(
|
var ErrNotExist error = domain.NewError(
|
||||||
domain.ErrorCodeInvalidClient,
|
domain.ErrorCodeInvalidClient,
|
||||||
"not found 'indieauth-metadata' endpoint on provided me URL",
|
"not found 'indieauth-metadata' endpoint on provided me URL",
|
||||||
"https://indieauth.net/source/#discovery-0",
|
"https://indieauth.net/source/#discovery-0",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func NewDummyMetadataRepository() Repository {
|
||||||
|
return dummyMetadataRepository{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dummyMetadataRepository) Fetch(_ context.Context, _ *url.URL) (*domain.Metadata, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewStubMetadataRepository(metadata *domain.Metadata, err error) Repository {
|
||||||
|
return &stubMetadataRepository{
|
||||||
|
metadata: metadata,
|
||||||
|
error: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *stubMetadataRepository) Fetch(_ context.Context, _ *url.URL) (*domain.Metadata, error) {
|
||||||
|
return s.metadata, s.error
|
||||||
|
}
|
||||||
|
|
|
@ -52,12 +52,7 @@ func NewHTTPMetadataRepository(client *http.Client) metadata.Repository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WARN(toby3d): not implemented.
|
func (repo *httpMetadataRepository) Fetch(_ context.Context, u *url.URL) (*domain.Metadata, error) {
|
||||||
func (httpMetadataRepository) Create(_ context.Context, _ *url.URL, _ domain.Metadata) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *httpMetadataRepository) Get(_ context.Context, u *url.URL) (*domain.Metadata, error) {
|
|
||||||
resp, err := repo.client.Get(u.String())
|
resp, err := repo.client.Get(u.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot make request to provided Me: %w", err)
|
return nil, fmt.Errorf("cannot make request to provided Me: %w", err)
|
||||||
|
|
|
@ -116,7 +116,7 @@ func TestGet(t *testing.T) {
|
||||||
|
|
||||||
u, _ := url.Parse(srv.URL + "/")
|
u, _ := url.Parse(srv.URL + "/")
|
||||||
out, err := repository.NewHTTPMetadataRepository(srv.Client()).
|
out, err := repository.NewHTTPMetadataRepository(srv.Client()).
|
||||||
Get(context.Background(), u)
|
Fetch(context.Background(), u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
package memory
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net/url"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"source.toby3d.me/toby3d/auth/internal/domain"
|
|
||||||
"source.toby3d.me/toby3d/auth/internal/metadata"
|
|
||||||
)
|
|
||||||
|
|
||||||
type memoryMetadataRepository struct {
|
|
||||||
mutex *sync.RWMutex
|
|
||||||
metadata map[string]domain.Metadata
|
|
||||||
}
|
|
||||||
|
|
||||||
const DefaultPathPrefix = "metadata"
|
|
||||||
|
|
||||||
func NewMemoryMetadataRepository() metadata.Repository {
|
|
||||||
return &memoryMetadataRepository{
|
|
||||||
mutex: new(sync.RWMutex),
|
|
||||||
metadata: make(map[string]domain.Metadata),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *memoryMetadataRepository) Create(ctx context.Context, u *url.URL, metadata domain.Metadata) error {
|
|
||||||
repo.mutex.Lock()
|
|
||||||
defer repo.mutex.Unlock()
|
|
||||||
|
|
||||||
repo.metadata[u.String()] = metadata
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (repo *memoryMetadataRepository) Get(ctx context.Context, u *url.URL) (*domain.Metadata, error) {
|
|
||||||
repo.mutex.RLock()
|
|
||||||
defer repo.mutex.RUnlock()
|
|
||||||
|
|
||||||
if out, ok := repo.metadata[u.String()]; ok {
|
|
||||||
return &out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, metadata.ErrNotExist
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user