2021-09-23 23:01:18 +00:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-05 06:54:36 +00:00
|
|
|
"path"
|
2021-09-23 23:01:18 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"source.toby3d.me/website/oauth/internal/client"
|
2021-09-29 20:35:39 +00:00
|
|
|
"source.toby3d.me/website/oauth/internal/domain"
|
2021-09-23 23:01:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type memoryClientRepository struct {
|
|
|
|
clients *sync.Map
|
|
|
|
}
|
|
|
|
|
2021-10-05 06:54:36 +00:00
|
|
|
const Key string = "clients"
|
|
|
|
|
2021-09-23 23:01:18 +00:00
|
|
|
func NewMemoryClientRepository(clients *sync.Map) client.Repository {
|
|
|
|
return &memoryClientRepository{
|
|
|
|
clients: clients,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:35:39 +00:00
|
|
|
func (repo *memoryClientRepository) Get(ctx context.Context, id string) (*domain.Client, error) {
|
2021-10-05 06:54:36 +00:00
|
|
|
src, ok := repo.clients.Load(path.Join(Key, id))
|
2021-09-23 23:01:18 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:35:39 +00:00
|
|
|
c, ok := src.(*domain.Client)
|
2021-09-23 23:01:18 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|