@@ -3,12 +3,12 @@ Package semver provides the ability to work with Semantic Versions (http://semve
33
44Specifically it provides the ability to:
55
6- * Parse semantic versions
7- * Sort semantic versions
8- * Check if a semantic version fits within a set of constraints
9- * Optionally work with a `v` prefix
6+ - Parse semantic versions
7+ - Sort semantic versions
8+ - Check if a semantic version fits within a set of constraints
9+ - Optionally work with a `v` prefix
1010
11- Parsing Semantic Versions
11+ # Parsing Semantic Versions
1212
1313There are two functions that can parse semantic versions. The `StrictNewVersion`
1414function only parses valid version 2 semantic versions as outlined in the
@@ -21,48 +21,48 @@ that can be sorted, compared, and used in constraints.
2121When parsing a version an optional error can be returned if there is an issue
2222parsing the version. For example,
2323
24- v, err := semver.NewVersion("1.2.3-beta.1+b345")
24+ v, err := semver.NewVersion("1.2.3-beta.1+b345")
2525
2626The version object has methods to get the parts of the version, compare it to
2727other versions, convert the version back into a string, and get the original
2828string. For more details please see the documentation
2929at https://godoc.org/github.com/Masterminds/semver.
3030
31- Sorting Semantic Versions
31+ # Sorting Semantic Versions
3232
3333A set of versions can be sorted using the `sort` package from the standard library.
3434For example,
3535
36- raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
37- vs := make([]*semver.Version, len(raw))
38- for i, r := range raw {
39- v, err := semver.NewVersion(r)
40- if err != nil {
41- t.Errorf("Error parsing version: %s", err)
42- }
36+ raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
37+ vs := make([]*semver.Version, len(raw))
38+ for i, r := range raw {
39+ v, err := semver.NewVersion(r)
40+ if err != nil {
41+ t.Errorf("Error parsing version: %s", err)
42+ }
4343
44- vs[i] = v
45- }
44+ vs[i] = v
45+ }
4646
47- sort.Sort(semver.Collection(vs))
47+ sort.Sort(semver.Collection(vs))
4848
49- Checking Version Constraints and Comparing Versions
49+ # Checking Version Constraints and Comparing Versions
5050
5151There are two methods for comparing versions. One uses comparison methods on
5252`Version` instances and the other is using Constraints. There are some important
5353differences to notes between these two methods of comparison.
5454
55- 1. When two versions are compared using functions such as `Compare`, `LessThan`,
56- and others it will follow the specification and always include prereleases
57- within the comparison. It will provide an answer valid with the comparison
58- spec section at https://semver.org/#spec-item-11
59- 2. When constraint checking is used for checks or validation it will follow a
60- different set of rules that are common for ranges with tools like npm/js
61- and Rust/Cargo. This includes considering prereleases to be invalid if the
62- ranges does not include on. If you want to have it include pre-releases a
63- simple solution is to include `-0` in your range.
64- 3. Constraint ranges can have some complex rules including the shorthard use of
65- ~ and ^. For more details on those see the options below.
55+ 1. When two versions are compared using functions such as `Compare`, `LessThan`,
56+ and others it will follow the specification and always include prereleases
57+ within the comparison. It will provide an answer valid with the comparison
58+ spec section at https://semver.org/#spec-item-11
59+ 2. When constraint checking is used for checks or validation it will follow a
60+ different set of rules that are common for ranges with tools like npm/js
61+ and Rust/Cargo. This includes considering prereleases to be invalid if the
62+ ranges does not include on. If you want to have it include pre-releases a
63+ simple solution is to include `-0` in your range.
64+ 3. Constraint ranges can have some complex rules including the shorthard use of
65+ ~ and ^. For more details on those see the options below.
6666
6767There are differences between the two methods or checking versions because the
6868comparison methods on `Version` follow the specification while comparison ranges
@@ -76,19 +76,19 @@ patters with their versions.
7676Checking a version against version constraints is one of the most featureful
7777parts of the package.
7878
79- c, err := semver.NewConstraint(">= 1.2.3")
80- if err != nil {
81- // Handle constraint not being parsable.
82- }
79+ c, err := semver.NewConstraint(">= 1.2.3")
80+ if err != nil {
81+ // Handle constraint not being parsable.
82+ }
8383
84- v, err := semver.NewVersion("1.3")
85- if err != nil {
86- // Handle version not being parsable.
87- }
88- // Check if the version meets the constraints. The a variable will be true.
89- a := c.Check(v)
84+ v, err := semver.NewVersion("1.3")
85+ if err != nil {
86+ // Handle version not being parsable.
87+ }
88+ // Check if the version meets the constraints. The a variable will be true.
89+ a := c.Check(v)
9090
91- Basic Comparisons
91+ # Basic Comparisons
9292
9393There are two elements to the comparisons. First, a comparison string is a list
9494of comma or space separated AND comparisons. These are then separated by || (OR)
@@ -99,43 +99,43 @@ greater than or equal to 4.2.3. This can also be written as
9999
100100The basic comparisons are:
101101
102- * `=`: equal (aliased to no operator)
103- * `!=`: not equal
104- * `>`: greater than
105- * `<`: less than
106- * `>=`: greater than or equal to
107- * `<=`: less than or equal to
102+ - `=`: equal (aliased to no operator)
103+ - `!=`: not equal
104+ - `>`: greater than
105+ - `<`: less than
106+ - `>=`: greater than or equal to
107+ - `<=`: less than or equal to
108108
109- Hyphen Range Comparisons
109+ # Hyphen Range Comparisons
110110
111111There are multiple methods to handle ranges and the first is hyphens ranges.
112112These look like:
113113
114- * `1.2 - 1.4.5` which is equivalent to `>= 1.2, <= 1.4.5`
115- * `2.3.4 - 4.5` which is equivalent to `>= 2.3.4 <= 4.5`
114+ - `1.2 - 1.4.5` which is equivalent to `>= 1.2, <= 1.4.5`
115+ - `2.3.4 - 4.5` which is equivalent to `>= 2.3.4 <= 4.5`
116116
117- Wildcards In Comparisons
117+ # Wildcards In Comparisons
118118
119119The `x`, `X`, and `*` characters can be used as a wildcard character. This works
120120for all comparison operators. When used on the `=` operator it falls
121121back to the tilde operation. For example,
122122
123- * `1.2.x` is equivalent to `>= 1.2.0 < 1.3.0`
124- * `>= 1.2.x` is equivalent to `>= 1.2.0`
125- * `<= 2.x` is equivalent to `<= 3`
126- * `*` is equivalent to `>= 0.0.0`
123+ - `1.2.x` is equivalent to `>= 1.2.0 < 1.3.0`
124+ - `>= 1.2.x` is equivalent to `>= 1.2.0`
125+ - `<= 2.x` is equivalent to `<= 3`
126+ - `*` is equivalent to `>= 0.0.0`
127127
128128Tilde Range Comparisons (Patch)
129129
130130The tilde (`~`) comparison operator is for patch level ranges when a minor
131131version is specified and major level changes when the minor number is missing.
132132For example,
133133
134- * `~1.2.3` is equivalent to `>= 1.2.3 < 1.3.0`
135- * `~1` is equivalent to `>= 1, < 2`
136- * `~2.3` is equivalent to `>= 2.3 < 2.4`
137- * `~1.2.x` is equivalent to `>= 1.2.0 < 1.3.0`
138- * `~1.x` is equivalent to `>= 1 < 2`
134+ - `~1.2.3` is equivalent to `>= 1.2.3 < 1.3.0`
135+ - `~1` is equivalent to `>= 1, < 2`
136+ - `~2.3` is equivalent to `>= 2.3 < 2.4`
137+ - `~1.2.x` is equivalent to `>= 1.2.0 < 1.3.0`
138+ - `~1.x` is equivalent to `>= 1 < 2`
139139
140140Caret Range Comparisons (Major)
141141
@@ -144,41 +144,41 @@ The caret (`^`) comparison operator is for major level changes once a stable
144144as the API stability level. This is useful when comparisons of API versions as a
145145major change is API breaking. For example,
146146
147- * `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
148- * `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
149- * `^2.3` is equivalent to `>= 2.3, < 3`
150- * `^2.x` is equivalent to `>= 2.0.0, < 3`
151- * `^0.2.3` is equivalent to `>=0.2.3 <0.3.0`
152- * `^0.2` is equivalent to `>=0.2.0 <0.3.0`
153- * `^0.0.3` is equivalent to `>=0.0.3 <0.0.4`
154- * `^0.0` is equivalent to `>=0.0.0 <0.1.0`
155- * `^0` is equivalent to `>=0.0.0 <1.0.0`
147+ - `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
148+ - `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
149+ - `^2.3` is equivalent to `>= 2.3, < 3`
150+ - `^2.x` is equivalent to `>= 2.0.0, < 3`
151+ - `^0.2.3` is equivalent to `>=0.2.3 <0.3.0`
152+ - `^0.2` is equivalent to `>=0.2.0 <0.3.0`
153+ - `^0.0.3` is equivalent to `>=0.0.3 <0.0.4`
154+ - `^0.0` is equivalent to `>=0.0.0 <0.1.0`
155+ - `^0` is equivalent to `>=0.0.0 <1.0.0`
156156
157- Validation
157+ # Validation
158158
159159In addition to testing a version against a constraint, a version can be validated
160160against a constraint. When validation fails a slice of errors containing why a
161161version didn't meet the constraint is returned. For example,
162162
163- c, err := semver.NewConstraint("<= 1.2.3, >= 1.4")
164- if err != nil {
165- // Handle constraint not being parseable.
166- }
167-
168- v, _ := semver.NewVersion("1.3")
169- if err != nil {
170- // Handle version not being parseable.
171- }
172-
173- // Validate a version against a constraint.
174- a, msgs := c.Validate(v)
175- // a is false
176- for _, m := range msgs {
177- fmt.Println(m)
178-
179- // Loops over the errors which would read
180- // "1.3 is greater than 1.2.3"
181- // "1.3 is less than 1.4"
182- }
163+ c, err := semver.NewConstraint("<= 1.2.3, >= 1.4")
164+ if err != nil {
165+ // Handle constraint not being parseable.
166+ }
167+
168+ v, _ := semver.NewVersion("1.3")
169+ if err != nil {
170+ // Handle version not being parseable.
171+ }
172+
173+ // Validate a version against a constraint.
174+ a, msgs := c.Validate(v)
175+ // a is false
176+ for _, m := range msgs {
177+ fmt.Println(m)
178+
179+ // Loops over the errors which would read
180+ // "1.3 is greater than 1.2.3"
181+ // "1.3 is less than 1.4"
182+ }
183183*/
184184package semver
0 commit comments