/
account_repository.go
280 lines (209 loc) · 5.45 KB
/
account_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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package jsonmutexdb
import (
"context"
"encoding/json"
"fmt"
"sort"
"sync"
"github.com/go-playground/validator/v10"
"github.com/zbiljic/authzy/pkg/database"
"github.com/zbiljic/authzy/pkg/database/jsonmutexdb"
"github.com/zbiljic/authzy/pkg/domain/account"
"github.com/zbiljic/authzy/pkg/domain/account/storage/json/schema"
"github.com/zbiljic/authzy/pkg/domain/account/storage/json/transformer"
"github.com/zbiljic/authzy/pkg/domain/account/storage/noop"
)
const (
accountsPrefix = "accounts"
)
type jsonMutexDBAccountRepository struct {
noop.UnimplementedAccountRepository
db map[string]schema.Account
mu sync.RWMutex
loadSaver jsonmutexdb.LoadSaver
filename string
validate *validator.Validate
}
// NewAccountRepository returns a new JSONMutexDB repository.
func NewAccountRepository(
loadSaver jsonmutexdb.LoadSaver,
filenamePrefix string,
) (account.AccountRepository, error) {
r := &jsonMutexDBAccountRepository{
db: make(map[string]schema.Account),
loadSaver: loadSaver,
filename: fmt.Sprintf("%s%s.json", filenamePrefix, accountsPrefix),
validate: validator.New(),
}
if err := r.load(); err != nil {
return nil, err
}
return r, nil
}
func (r *jsonMutexDBAccountRepository) load() error {
if r.loadSaver != nil {
data, err := r.loadSaver.Load(r.filename)
if err != nil {
return err
}
if len(data) > 0 {
err = json.Unmarshal(data, &r.db)
if err != nil {
return err
}
}
}
return nil
}
func (r *jsonMutexDBAccountRepository) commit(ctx context.Context) error {
if r.loadSaver != nil {
out, err := json.Marshal(r.db)
if err != nil {
return err
}
return r.loadSaver.Save(r.filename, out)
}
return nil
}
const (
ns = "account/storage/jsonmutexdb."
opSave = ns + "Save"
opFind = ns + "Find"
opExists = ns + "Exists"
opDelete = ns + "Delete"
opFindAllForUser = ns + "FindAllForUser"
)
func (r *jsonMutexDBAccountRepository) Save(ctx context.Context, entity *account.Account) (*account.Account, error) {
r.mu.Lock()
defer r.mu.Unlock()
inS := schema.AccountToSchema(entity)
err := r.validate.Struct(inS)
if err != nil {
return nil, fmt.Errorf("%s: %w", opSave, err)
}
// before save
err = inS.BeforeSave()
if err != nil {
return nil, fmt.Errorf("%s: %w", opSave, err)
}
id := transformer.MarshalAccountID(inS)
r.db[id] = *inS
err = r.commit(ctx)
if err != nil {
return nil, fmt.Errorf("%s: %w", opSave, err)
}
savedEntity := schema.AccountFromSchema(inS)
return savedEntity, nil
}
func (r *jsonMutexDBAccountRepository) Find(ctx context.Context, entity *account.Account) (*account.Account, error) {
r.mu.RLock()
defer r.mu.RUnlock()
inS := schema.AccountToSchema(entity)
err := r.validate.Struct(inS)
if err != nil {
return nil, fmt.Errorf("%s: %w", opFind, err)
}
id := transformer.MarshalAccountID(inS)
value, ok := r.db[id]
if !ok {
return nil, fmt.Errorf("%s(%s): %w", opFind, id, database.ErrNotFound)
}
dbEntity := schema.AccountFromSchema(&value)
return dbEntity, nil
}
func (r *jsonMutexDBAccountRepository) Exists(ctx context.Context, entity *account.Account) (bool, error) {
r.mu.RLock()
defer r.mu.RUnlock()
inS := schema.AccountToSchema(entity)
err := r.validate.Struct(inS)
if err != nil {
return false, fmt.Errorf("%s: %w", opExists, err)
}
id := transformer.MarshalAccountID(inS)
_, has := r.db[id]
return has, nil
}
func (r *jsonMutexDBAccountRepository) FindAll(ctx context.Context, afterCursor string, limit int) ([]*account.Account, string, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if limit <= 0 {
limit = 25
}
var (
offset int
result []*account.Account
lastResultID string
nextCursor string
)
keys := []string{}
for id := range r.db {
keys = append(keys, id)
}
sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
var afterCursorKey string
if afterCursor != "" {
afterCursorKey = afterCursor
}
for _, id := range keys {
if afterCursorKey != "" {
if afterCursorKey == id {
afterCursorKey = ""
}
continue
}
offset++
val := r.db[id]
lastResultID = transformer.MarshalAccountID(&val)
t := schema.AccountFromSchema(&val)
result = append(result, t)
if limit == offset {
break // stops iterator
}
}
if len(result) == limit {
nextCursor = lastResultID
}
return result, nextCursor, nil
}
func (r *jsonMutexDBAccountRepository) Count(ctx context.Context) (int, error) {
r.mu.RLock()
defer r.mu.RUnlock()
return len(r.db), nil
}
func (r *jsonMutexDBAccountRepository) Delete(ctx context.Context, entity *account.Account) error {
r.mu.Lock()
defer r.mu.Unlock()
inS := schema.AccountToSchema(entity)
err := r.validate.Struct(inS)
if err != nil {
return fmt.Errorf("%s: %w", opDelete, err)
}
id := transformer.MarshalAccountID(inS)
delete(r.db, id)
return nil
}
func (r *jsonMutexDBAccountRepository) DeleteAll(ctx context.Context) error {
r.mu.Lock()
defer r.mu.Unlock()
r.db = make(map[string]schema.Account)
return nil
}
func (r *jsonMutexDBAccountRepository) FindAllForUser(ctx context.Context, userID string) ([]*account.Account, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if userID == "" {
return nil, fmt.Errorf("%s: userID cannot be empty", opFindAllForUser)
}
var (
result []*account.Account
)
for _, val := range r.db {
if val.UserID != userID {
continue
}
val := val
t := schema.AccountFromSchema(&val)
result = append(result, t)
}
return result, nil
}