forked from dexidp/dex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
41 lines (32 loc) · 1.11 KB
/
repo.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
package repo
import "errors"
// Transaction is an abstraction of transactions typically found in database systems.
// One of Commit() or Rollback() must be called on each transaction.
type Transaction interface {
// Commit will persist the changes in the transaction.
Commit() error
// Rollback undoes the changes in a transaction
Rollback() error
}
type TransactionFactory func() (Transaction, error)
// InMemTransaction satisifies the Transaction interface for in-memory systems.
// However, the only thing it really does is ensure that the same transaction is
// can't be committed/rolled back more than once. As such, this can lead to data
// corruption and should not be used in production systems.
type InMemTransaction bool
func InMemTransactionFactory() (Transaction, error) {
return new(InMemTransaction), nil
}
func (i *InMemTransaction) Commit() error {
return i.commitOrRollback()
}
func (i *InMemTransaction) Rollback() error {
return i.commitOrRollback()
}
func (i *InMemTransaction) commitOrRollback() error {
if *i {
return errors.New("Already committed/rolled-back.")
}
*i = true
return nil
}