This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
semantic.go
197 lines (149 loc) · 3.03 KB
/
semantic.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"strconv"
"strings"
)
type Semantic struct {
major int
minor int
patch int
pre string
isPre bool
}
func New(v string) (*Semantic, error) {
items := strings.SplitN(v, ".", 3)
if len(items) < 3 {
return nil, SemanticErrors.VersionIsNotValid
}
major, majorErr := strconv.ParseInt(items[0], 10, 32)
if majorErr != nil {
return nil, SemanticErrors.MajorVersionIsNotValid
}
minor, minorErr := strconv.ParseInt(items[1], 10, 32)
if minorErr != nil {
return nil, SemanticErrors.MinorVersionIsNotValid
}
patchItems := strings.SplitN(items[2], "-", 2)
patch, patchErr := strconv.ParseInt(patchItems[0], 10, 32)
if patchErr != nil {
return nil, SemanticErrors.PatchVersionIsNotValid
}
semantic := &Semantic{
major: int(major),
minor: int(minor),
patch: int(patch),
}
if len(patchItems) > 1 {
semantic.pre = patchItems[1]
semantic.isPre = true
}
return semantic, nil
}
func Compare(version1, operator, version2 string) (bool, error) {
v1, err := New(version1)
if err != nil {
return false, err
}
v2, err := New(version2)
if err != nil {
return false, err
}
switch operator {
case ">":
return GreaterThan(*v1, *v2), nil
case ">=":
return Equal(*v1, *v2) || GreaterThan(*v1, *v2), nil
case "<":
return LessThan(*v1, *v2), nil
case "<=":
return Equal(*v1, *v2) || LessThan(*v1, *v2), nil
case "==":
return Equal(*v1, *v2), nil
}
return false, nil
}
func Equal(v1, v2 Semantic) bool {
return v1.Value() == v2.Value() && v1.pre == v2.pre
}
func GreaterThan(v1, v2 Semantic) bool {
return v1.Value() > v2.Value()
}
func LessThan(v1, v2 Semantic) bool {
return v1.Value() < v2.Value()
}
func (s Semantic) String() string {
items := make([]string, 3, 4)
items[0] = strconv.Itoa(s.major)
items[1] = strconv.Itoa(s.minor)
items[2] = strconv.Itoa(s.patch)
if s.isPre {
items[2] += "-" + s.pre
}
return strings.Join(items, ".")
}
func (s *Semantic) Inc(major, minor, patch, release, beta, alpha bool) {
if major {
s.IncMajor()
}
if minor {
s.IncMinor()
}
if patch {
s.IncPatch()
}
if release {
s.IncRelease()
}
if beta {
s.IncBeta()
}
if alpha {
s.IncAlpha()
}
}
func (s *Semantic) IncMajor() {
s.major++
s.minor = 0
s.patch = 0
s.pre = ""
s.isPre = false
}
func (s *Semantic) IncMinor() {
s.minor++
s.patch = 0
s.pre = ""
s.isPre = false
}
func (s *Semantic) IncPatch() {
s.patch++
s.pre = ""
s.isPre = false
}
func (s *Semantic) incPre(name string) {
if s.isPre && strings.HasPrefix(s.pre, name) {
items := strings.SplitN(s.pre, ".", 2)
if len(items) == 1 {
items = append(items, "0")
} else {
v, _ := strconv.ParseInt(items[1], 10, 32)
v++
items[1] = strconv.Itoa(int(v))
}
s.pre = strings.Join(items, ".")
} else {
s.isPre = true
s.pre = name + ".0"
}
}
func (s *Semantic) IncRelease() {
s.incPre("RELEASE")
}
func (s *Semantic) IncBeta() {
s.incPre("BETA")
}
func (s *Semantic) IncAlpha() {
s.incPre("ALPHA")
}
func (s *Semantic) Value() int {
return (s.major * 100) + (s.minor * 10) + s.patch
}