-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconstraints.go
104 lines (84 loc) · 1.93 KB
/
constraints.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
package semver
import "strings"
// Constraint interface is common to all version constraints.
type Constraint interface {
// Check if the version is allowed by the constraint or not.
Check(v Version) bool
// Check if a range is contained within a constraint.
Contains(or Constraint) bool
// Returns the string representation of this constraint.
String() string
}
// safes original parser input to return via the String method.
type originalInputConstraint struct {
Constraint
original string
}
var _ Constraint = (*originalInputConstraint)(nil)
func (oi *originalInputConstraint) String() string {
return oi.original
}
// and is a list of Ranges that all have to pass.
type and []Constraint
var _ Constraint = and{}
func (and and) Check(v Version) bool {
for _, r := range and {
if !r.Check(v) {
return false
}
}
return true
}
func (and and) Contains(or Constraint) bool {
for _, r := range and {
if !r.Contains(or) {
return false
}
}
return true
}
func (and and) String() string {
parts := make([]string, len(and))
for i := range and {
parts[i] = and[i].String()
}
return strings.Join(parts, " && ")
}
// or is a list of Constraints that need at least one match.
type or []Constraint
var _ Constraint = or{}
func (or or) Check(v Version) bool {
for _, r := range or {
if r.Check(v) {
return true
}
}
return false
}
func (or or) Contains(other Constraint) bool {
for _, r := range or {
if r.Contains(other) {
return true
}
}
return false
}
func (or or) String() string {
parts := make([]string, len(or))
for i := range or {
parts[i] = or[i].String()
}
return strings.Join(parts, " || ")
}
// not negates the given Range.
type not struct{ Range }
var _ Constraint = not{}
func (not not) Check(v Version) bool {
return !not.Range.Check(v)
}
func (not not) Contains(other Constraint) bool {
return !other.Contains(¬.Range)
}
func (not not) String() string {
return "!=" + not.Range.String()
}