generated from cunicu/skeleton
-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
61 lines (49 loc) · 1.1 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
// SPDX-FileCopyrightText: 2023-2024 Steffen Vogel <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package iso7816
import (
"errors"
"fmt"
"strconv"
"strings"
)
var ErrInvalidVersion = errors.New("invalid version")
// Version encodes a major, minor, and patch version.
type Version struct {
Major int
Minor int
Patch int
}
func ParseVersion(s string) (v Version, err error) {
var ps []string
if s != "" {
ps = strings.Split(s, ".")
}
l := len(ps)
if l > 3 {
return v, fmt.Errorf("%w: too many dots (%d)", ErrInvalidVersion, l)
}
vs := []*int{&v.Major, &v.Minor, &v.Patch}
for i, q := range vs {
if i >= l {
*q = -1
} else if *q, err = strconv.Atoi(ps[i]); err != nil {
return v, err
} else if *q < 0 {
return v, fmt.Errorf("%w: must be positive", ErrInvalidVersion)
}
}
return v, nil
}
func (v Version) String() string {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
}
func (v Version) Less(w Version) bool {
if v.Major != w.Major {
return v.Major > w.Major
}
if v.Minor != w.Minor {
return v.Minor > w.Minor
}
return v.Patch > w.Patch
}