Skip to content

Commit 7ddb7ed

Browse files
author
Piotr
committed
recognizing semver more granular (pathches are ignored now)
1 parent aa2e566 commit 7ddb7ed

File tree

3 files changed

+565
-11
lines changed

3 files changed

+565
-11
lines changed

utils/semver.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
)
8+
9+
// ComparisonResult represents the result of comparing two semver strings
10+
type ComparisonResult int
11+
12+
const (
13+
Less ComparisonResult = -1
14+
Equal ComparisonResult = 0
15+
Greater ComparisonResult = 1
16+
)
17+
18+
// String returns a string representation of the comparison result
19+
func (c ComparisonResult) String() string {
20+
switch c {
21+
case Less:
22+
return "less than"
23+
case Equal:
24+
return "equal to"
25+
case Greater:
26+
return "greater than"
27+
default:
28+
return "unknown"
29+
}
30+
}
31+
32+
// CompareOptions defines which version components to include in comparison
33+
type CompareOptions struct {
34+
CheckMajor bool
35+
CheckMinor bool
36+
CheckPatch bool
37+
}
38+
39+
// DefaultCompareOptions returns the standard semver comparison options
40+
// which checks all three components (major, minor, patch)
41+
func DefaultCompareOptions() CompareOptions {
42+
return CompareOptions{
43+
CheckMajor: true,
44+
CheckMinor: true,
45+
CheckPatch: true,
46+
}
47+
}
48+
49+
// CompareSemver compares two semantic version strings (A and B)
50+
// with customizable options for which components to check
51+
// Returns:
52+
//
53+
// -1 (Less) if A < B
54+
// 0 (Equal) if A = B
55+
// 1 (Greater) if A > B
56+
//
57+
// Follows semantic versioning rules where versions are in the format: MAJOR.MINOR.PATCH
58+
func CompareSemver(versionA, versionB string, options CompareOptions) (ComparisonResult, error) {
59+
// Parse version A
60+
partsA := strings.Split(versionA, ".")
61+
if len(partsA) != 3 {
62+
return 0, fmt.Errorf("version A (%s) is not in the format MAJOR.MINOR.PATCH", versionA)
63+
}
64+
65+
// Parse version B
66+
partsB := strings.Split(versionB, ".")
67+
if len(partsB) != 3 {
68+
return 0, fmt.Errorf("version B (%s) is not in the format MAJOR.MINOR.PATCH", versionB)
69+
}
70+
71+
// Compare MAJOR version if enabled
72+
if options.CheckMajor {
73+
majorA, err := strconv.Atoi(partsA[0])
74+
if err != nil {
75+
return 0, fmt.Errorf("invalid MAJOR version in A: %s", partsA[0])
76+
}
77+
78+
majorB, err := strconv.Atoi(partsB[0])
79+
if err != nil {
80+
return 0, fmt.Errorf("invalid MAJOR version in B: %s", partsB[0])
81+
}
82+
83+
if majorA != majorB {
84+
if majorA > majorB {
85+
return Greater, nil
86+
}
87+
return Less, nil
88+
}
89+
}
90+
91+
// Compare MINOR version if enabled
92+
if options.CheckMinor {
93+
minorA, err := strconv.Atoi(partsA[1])
94+
if err != nil {
95+
return 0, fmt.Errorf("invalid MINOR version in A: %s", partsA[1])
96+
}
97+
98+
minorB, err := strconv.Atoi(partsB[1])
99+
if err != nil {
100+
return 0, fmt.Errorf("invalid MINOR version in B: %s", partsB[1])
101+
}
102+
103+
if minorA != minorB {
104+
if minorA > minorB {
105+
return Greater, nil
106+
}
107+
return Less, nil
108+
}
109+
}
110+
111+
// Compare PATCH version if enabled
112+
if options.CheckPatch {
113+
patchA, err := strconv.Atoi(partsA[2])
114+
if err != nil {
115+
return 0, fmt.Errorf("invalid PATCH version in A: %s", partsA[2])
116+
}
117+
118+
patchB, err := strconv.Atoi(partsB[2])
119+
if err != nil {
120+
return 0, fmt.Errorf("invalid PATCH version in B: %s", partsB[2])
121+
}
122+
123+
if patchA != patchB {
124+
if patchA > patchB {
125+
return Greater, nil
126+
}
127+
return Less, nil
128+
}
129+
}
130+
131+
// All checked components are equal
132+
return Equal, nil
133+
}
134+
135+
// SimplifiedCompareSemver provides the original function signature for backward compatibility
136+
// It compares all three version components (major, minor, patch)
137+
func SimplifiedCompareSemver(versionA, versionB string) (ComparisonResult, error) {
138+
return CompareSemver(versionA, versionB, DefaultCompareOptions())
139+
}

0 commit comments

Comments
 (0)