aboutsummaryrefslogtreecommitdiffstats
path: root/identity
diff options
context:
space:
mode:
Diffstat (limited to 'identity')
-rw-r--r--identity/resolver.go35
-rw-r--r--identity/version.go22
2 files changed, 41 insertions, 16 deletions
diff --git a/identity/resolver.go b/identity/resolver.go
index ab380a12..8e066e9d 100644
--- a/identity/resolver.go
+++ b/identity/resolver.go
@@ -1,6 +1,8 @@
package identity
import (
+ "sync"
+
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
)
@@ -34,3 +36,36 @@ func NewStubResolver() *StubResolver {
func (s *StubResolver) ResolveIdentity(id entity.Id) (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
+}
diff --git a/identity/version.go b/identity/version.go
index 1c35831e..9a52d089 100644
--- a/identity/version.go
+++ b/identity/version.go
@@ -4,7 +4,6 @@ import (
"crypto/rand"
"encoding/json"
"fmt"
- "strings"
"time"
"github.com/pkg/errors"
@@ -186,25 +185,16 @@ func (v *version) Validate() error {
if text.Empty(v.name) && text.Empty(v.login) {
return fmt.Errorf("either name or login should be set")
}
- if strings.Contains(v.name, "\n") {
- return fmt.Errorf("name should be a single line")
- }
- if !text.Safe(v.name) {
- return fmt.Errorf("name is not fully printable")
+ if !text.SafeOneLine(v.name) {
+ return fmt.Errorf("name has unsafe characters")
}
- if strings.Contains(v.login, "\n") {
- return fmt.Errorf("login should be a single line")
- }
- if !text.Safe(v.login) {
- return fmt.Errorf("login is not fully printable")
+ if !text.SafeOneLine(v.login) {
+ return fmt.Errorf("login has unsafe characters")
}
- if strings.Contains(v.email, "\n") {
- return fmt.Errorf("email should be a single line")
- }
- if !text.Safe(v.email) {
- return fmt.Errorf("email is not fully printable")
+ if !text.SafeOneLine(v.email) {
+ return fmt.Errorf("email has unsafe characters")
}
if v.avatarURL != "" && !text.ValidUrl(v.avatarURL) {