-
Notifications
You must be signed in to change notification settings - Fork 0
/
precondition.go
60 lines (49 loc) · 1.32 KB
/
precondition.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
package dal
import "time"
type precondition struct {
f func(preconditions *preConditions)
}
func (v precondition) apply(preconditions *preConditions) {
v.f(preconditions)
}
// Precondition defines precondition
type Precondition interface {
apply(preconditions *preConditions)
}
// Preconditions defines preconditions
type Preconditions interface {
Exists() bool
LastUpdateTime() time.Time
}
type preConditions struct {
exists bool
lastUpdateTime time.Time
}
// Exists indicate exists precondition
func (v preConditions) Exists() bool {
return v.exists
}
// LastUpdateTime indicate last update time precondition
func (v preConditions) LastUpdateTime() time.Time {
return v.lastUpdateTime
}
// WithExistsPrecondition sets exists precondition
func WithExistsPrecondition() Precondition {
return precondition{f: func(preconditions *preConditions) {
preconditions.exists = true
}}
}
// WithLastUpdateTimePrecondition sets last update time
func WithLastUpdateTimePrecondition(t time.Time) Precondition {
return precondition{f: func(preconditions *preConditions) {
preconditions.lastUpdateTime = t
}}
}
// GetPreconditions create Preconditions
func GetPreconditions(items ...Precondition) Preconditions {
var result preConditions
for _, precondition := range items {
precondition.apply(&result)
}
return result
}