-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsemver.go
144 lines (131 loc) · 2.86 KB
/
semver.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
package surferua
import (
"math/rand"
"strconv"
)
// Semantic versioning 2.0.0
// http://semver.org/
var defaultVersionRange = &Endpoint{0, 0}
type Semver struct {
Major int
Minor int
Patch int
PreRelease PreRelease
Metadata string
}
type PreRelease string
func (s *Semver) String(seps ...string) string {
var sep = "."
if len(seps) >= 1 {
sep = seps[0]
}
// default use '.' as sep
// remove the last 0
if s.Patch == 0 {
return strconv.Itoa(s.Major) + sep + strconv.Itoa(s.Minor)
}
return strconv.Itoa(s.Major) + sep + strconv.Itoa(s.Minor) + sep + strconv.Itoa(s.Patch)
}
type VersionInfo struct {
Major Endpoint
Minor Endpoint
Patch Endpoint
}
type Endpoint struct {
Start int
End int
}
func (vi *VersionInfo) Random() (s *Semver) {
s = &Semver{
Major: randomRange(vi.Major.Start, vi.Major.End),
Minor: randomRange(vi.Minor.Start, vi.Minor.End),
Patch: randomRange(vi.Patch.Start, vi.Patch.End),
}
return s
}
func randomRange(start, end int) int {
if start == end {
return start
} else {
return start + rand.Intn(end-start)
}
}
func mustInt(v interface{}) int {
switch value := v.(type) {
case int:
return value
case int64:
return int(value)
default:
return 0
}
}
func getVersionRange(v interface{}) Endpoint {
// check type of v
switch value := v.(type) {
case int:
// if value if int64 return []
return Endpoint{value, value}
case int64:
// if value if int64 return []
intValue := int(value)
return Endpoint{intValue, intValue}
case []int:
// if value if list of int, can be int64?
switch len(value) {
case 0:
return *defaultVersionRange
case 1:
return Endpoint{value[0], value[0]}
default:
if value[0] > value[1] {
return Endpoint{value[1], value[0]}
}
return Endpoint{value[0], value[1]}
}
case []int64:
// if value if list of int, can be int64?
switch len(value) {
case 0:
return *defaultVersionRange
case 1:
intValue := int(value[0])
return Endpoint{intValue, intValue}
default:
if value[0] > value[1] {
return Endpoint{int(value[1]), int(value[0])}
}
return Endpoint{int(value[0]), int(value[1])}
}
case []interface{}:
// if value if list of int, can be int64?
switch len(value) {
case 0:
return *defaultVersionRange
case 1:
intValue := mustInt(value[0])
return Endpoint{intValue, intValue}
default:
v0 := mustInt(value[0])
v1 := mustInt(value[1])
if v0 > v1 {
return Endpoint{v1, v0}
}
return Endpoint{v0, v1}
}
default:
// default return default 0.0.0
return *defaultVersionRange
}
}
func NewVersionInfo(m interface{}) (vi VersionInfo) {
// we need to get data from map[string]interface{}
if mMap, ok := m.(map[interface{}]interface{}); ok {
return VersionInfo{
Major: getVersionRange(mMap["major"]),
Minor: getVersionRange(mMap["minor"]),
Patch: getVersionRange(mMap["patch"]),
}
}
return
}