-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
45 lines (37 loc) · 1.17 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package roles
import (
"sync"
uuid "github.com/google/uuid"
)
// Repository is a storage interface which can be implemented by multiple backend
// (in-memory map, sql database, in-memory cache, file system, ...)
// It allows standard CRUD operation on facts
type Repository interface {
Get(uuid uuid.UUID) (Role, bool, error)
GetByName(name string) (Role, bool, error)
Create(permission Role) (uuid.UUID, error)
Update(permission Role) error
Delete(uuid uuid.UUID) error
GetAll() ([]Role, error)
GetAllForUser(userUUID uuid.UUID) ([]Role, error)
SetRolePermissions(roleUUID uuid.UUID, permissionUUIDs []uuid.UUID) error
}
var (
_globalRepositoryMu sync.RWMutex
_globalRepository Repository
)
// R is used to access the global repository singleton
func R() Repository {
_globalRepositoryMu.RLock()
repository := _globalRepository
_globalRepositoryMu.RUnlock()
return repository
}
// ReplaceGlobals affect a new repository to the global repository singleton
func ReplaceGlobals(repository Repository) func() {
_globalRepositoryMu.Lock()
prev := _globalRepository
_globalRepository = repository
_globalRepositoryMu.Unlock()
return func() { ReplaceGlobals(prev) }
}