diff options
author | Michael Muré <batolettre@gmail.com> | 2022-08-13 12:08:48 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2022-08-18 15:55:48 +0200 |
commit | 45f5f852b71a63c142bca8b05efe53eebf142594 (patch) | |
tree | cb92d9f598b13dda69fbbc652a21d0ad8dc314c2 /identity | |
parent | cd52872475f1b39f3fb6546606c1e78afb6c08e3 (diff) | |
download | git-bug-45f5f852b71a63c142bca8b05efe53eebf142594.tar.gz |
core: generalized resolvers to resolve any entity time when unmarshalling an operation
Diffstat (limited to 'identity')
-rw-r--r-- | identity/resolver.go | 47 |
1 files changed, 5 insertions, 42 deletions
diff --git a/identity/resolver.go b/identity/resolver.go index 8e066e9d..5468a8f8 100644 --- a/identity/resolver.go +++ b/identity/resolver.go @@ -1,17 +1,11 @@ package identity import ( - "sync" - "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" ) -// Resolver define the interface of an Identity resolver, able to load -// an identity from, for example, a repo or a cache. -type Resolver interface { - ResolveIdentity(id entity.Id) (Interface, error) -} +var _ entity.Resolver = &SimpleResolver{} // SimpleResolver is a Resolver loading Identities directly from a Repo type SimpleResolver struct { @@ -22,10 +16,12 @@ func NewSimpleResolver(repo repository.Repo) *SimpleResolver { return &SimpleResolver{repo: repo} } -func (r *SimpleResolver) ResolveIdentity(id entity.Id) (Interface, error) { +func (r *SimpleResolver) Resolve(id entity.Id) (entity.Interface, error) { return ReadLocal(r.repo, id) } +var _ entity.Resolver = &StubResolver{} + // StubResolver is a Resolver that doesn't load anything, only returning IdentityStub instances type StubResolver struct{} @@ -33,39 +29,6 @@ func NewStubResolver() *StubResolver { return &StubResolver{} } -func (s *StubResolver) ResolveIdentity(id entity.Id) (Interface, error) { +func (s *StubResolver) Resolve(id entity.Id) (entity.Interface, error) { return &IdentityStub{id: id}, nil } - -// CachedResolver is a resolver ensuring that loading is done only once through another Resolver. -type CachedResolver struct { - mu sync.RWMutex - resolver Resolver - identities map[entity.Id]Interface -} - -func NewCachedResolver(resolver Resolver) *CachedResolver { - return &CachedResolver{ - resolver: resolver, - identities: make(map[entity.Id]Interface), - } -} - -func (c *CachedResolver) ResolveIdentity(id entity.Id) (Interface, error) { - c.mu.RLock() - if i, ok := c.identities[id]; ok { - c.mu.RUnlock() - return i, nil - } - c.mu.RUnlock() - - c.mu.Lock() - defer c.mu.Unlock() - - i, err := c.resolver.ResolveIdentity(id) - if err != nil { - return nil, err - } - c.identities[id] = i - return i, nil -} |