-
Notifications
You must be signed in to change notification settings - Fork 513
/
semantic_constraint.go
74 lines (61 loc) · 2.02 KB
/
semantic_constraint.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
package version
import (
"fmt"
"strings"
hashiVer "github.com/anchore/go-version"
)
// ruby packages such as activerecord and sprockets don't strictly follow semver
// note: this may result in missed matches for versioned betas
var normalizer = strings.NewReplacer(".alpha", "-alpha", ".beta", "-beta", ".rc", "-rc")
type semanticConstraint struct {
raw string
constraint hashiVer.Constraints
}
func newSemanticConstraint(constStr string) (semanticConstraint, error) {
if constStr == "" {
// an empty constraint is always satisfied
return semanticConstraint{}, nil
}
normalized := normalizer.Replace(constStr)
constraints, err := hashiVer.NewConstraint(normalized)
if err != nil {
return semanticConstraint{}, err
}
return semanticConstraint{
raw: normalized,
constraint: constraints,
}, nil
}
func (c semanticConstraint) supported(format Format) bool {
// gemfiles are a case of semantic version combined with non-semver
// and that doesn't work well. Gemfile_version.go extracts the semVer
// portion and makes a semVer object that is compatible with
// these constraints. In practice two formats (semVer, gem version) follow semVer,
// but one of them needs extra cleanup to function (gem).
return format == SemanticFormat || format == GemFormat
}
func (c semanticConstraint) Satisfied(version *Version) (bool, error) {
if c.raw == "" && version != nil {
// an empty constraint is always satisfied
return true, nil
} else if version == nil {
if c.raw != "" {
// a non-empty constraint with no version given should always fail
return false, nil
}
return true, nil
}
if !c.supported(version.Format) {
return false, fmt.Errorf("(semantic) unsupported format: %s", version.Format)
}
if version.rich.semVer == nil {
return false, fmt.Errorf("no rich semantic version given: %+v", version)
}
return c.constraint.Check(version.rich.semVer.verObj), nil
}
func (c semanticConstraint) String() string {
if c.raw == "" {
return "none (semver)"
}
return fmt.Sprintf("%s (semver)", c.raw)
}