forked from subomi/requestmigrations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.go
97 lines (78 loc) · 1.66 KB
/
version.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
package requestmigrations
import (
"time"
"github.com/Masterminds/semver/v3"
)
type Version struct {
Format VersionFormat
Value interface{}
}
func (v *Version) IsValid() bool {
switch v.Format {
case SemverFormat:
_, err := semver.NewVersion(v.Value.(string))
if err != nil {
return false
}
case DateFormat:
_, err := time.Parse(time.DateOnly, v.Value.(string))
if err != nil {
return false
}
}
return true
}
func (v *Version) Equal(vv *Version) bool {
switch v.Format {
case SemverFormat:
sv, err := semver.NewVersion(v.Value.(string))
if err != nil {
return false
}
svv, err := semver.NewVersion(vv.Value.(string))
if err != nil {
return false
}
return sv.Equal(svv)
case DateFormat:
tv, err := time.Parse(time.DateOnly, v.Value.(string))
if err != nil {
return false
}
tvv, err := time.Parse(time.DateOnly, vv.Value.(string))
if err != nil {
return false
}
return tv.Equal(tvv)
}
return false
}
func (v *Version) String() string {
return v.Value.(string)
}
func dateVersionSorter(versions []*Version) func(i, j int) bool {
return func(i, j int) bool {
it, err := time.Parse(time.DateOnly, versions[i].Value.(string))
if err != nil {
return false
}
jt, err := time.Parse(time.DateOnly, versions[j].Value.(string))
if err != nil {
return false
}
return it.Before(jt)
}
}
func semVerSorter(versions []*Version) func(i, j int) bool {
return func(i, j int) bool {
is, err := semver.NewVersion(versions[i].Value.(string))
if err != nil {
return false
}
js, err := semver.NewVersion(versions[j].Value.(string))
if err != nil {
return false
}
return is.LessThan(js)
}
}