-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.go
83 lines (73 loc) · 2.24 KB
/
set.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
package constraints
import "strings"
// A Set is a constraint which consisted of a set of constraints.
type ConstraintSet[
ValueT any,
ConstraintT Constraint[ValueT],
] interface {
Constraint[ValueT]
Validate(v ValueT) Constraint[ValueT]
ValidateAll(v ValueT) (violated []Constraint[ValueT])
ConstraintList() []ConstraintT
}
func Set[ValueT any](
constraints ...Constraint[ValueT],
) ConstraintSet[ValueT, Constraint[ValueT]] {
return &constraintSet[ValueT]{
constraints: constraints, //copy?
}
}
var (
_ Constraint[string] = constraintSet[string]{}
_ ConstraintSet[string, Constraint[string]] = constraintSet[string]{}
)
// constraintSet defines a set of constraints. A value is considered valid
// if every constraint considers the value as valid.
type constraintSet[ValueT any] struct {
constraints []Constraint[ValueT]
}
// ConstraintDescription conforms Constraint interface.
func (cs constraintSet[ValueT]) ConstraintDescription() string {
if cs.constraints != nil {
descs := make([]string, 0, len(cs.constraints))
for _, ci := range cs.constraints {
descs = append(descs, ci.ConstraintDescription())
}
return strings.Join(descs, ", ")
}
return ""
}
// ConstraintList conforms Set interface.
func (cs constraintSet[ValueT]) ConstraintList() []Constraint[ValueT] {
if cs.constraints != nil {
copyConstraints := make([]Constraint[ValueT], 0, len(cs.constraints))
copy(copyConstraints, cs.constraints)
return copyConstraints
}
return nil
}
// IsValid conforms Constraint interface.
func (cs constraintSet[ValueT]) IsValid(v ValueT) bool {
return cs.Validate(v) == nil
}
// Validate checks the value against the constraints. If the value is
// valid, this method will return nil. Otherwise, it'll return the first
// violated constraint.
func (cs constraintSet[ValueT]) Validate(v ValueT) Constraint[ValueT] {
for _, ci := range cs.constraints {
if !ci.IsValid(v) {
return ci
}
}
return nil
}
// ValidateAll validates the value against all constraints. It returns
// a list of violate constraints if any.
func (cs constraintSet[ValueT]) ValidateAll(v ValueT) (violated []Constraint[ValueT]) {
for _, ci := range cs.constraints {
if !ci.IsValid(v) {
violated = append(violated, ci)
}
}
return violated
}