forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion.go
66 lines (51 loc) · 1.24 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
package types
import (
"encoding/json"
"github.com/hashicorp/go-version"
)
type ZBXVersion version.Version
func NewZBXVersion(input string) (*ZBXVersion, error) {
parsed, err := version.NewVersion(input)
if err != nil {
return nil, err
}
ver := ZBXVersion(*parsed)
return &ver, nil
}
func (v *ZBXVersion) String() string {
ver := version.Version(*v)
return ver.String()
}
func (v *ZBXVersion) Compare(other *ZBXVersion) int {
ver := version.Version(*v)
o := version.Version(*other)
return ver.Compare(&o)
}
func (v *ZBXVersion) LessThan(o *ZBXVersion) bool {
return v.Compare(o) < 0
}
func (v *ZBXVersion) UnmarshalJSON(data []byte) error {
var apiVersion string
if err := json.Unmarshal(data, &apiVersion); err != nil {
return err
}
parsed, err := version.NewVersion(apiVersion)
if err != nil {
return err
}
*v = ZBXVersion(*parsed)
return nil
}
// UnmarshalText implements encoding.TextUnmarshaler interface.
func (v *ZBXVersion) UnmarshalText(b []byte) error {
temp, err := version.NewVersion(string(b))
if err != nil {
return err
}
*v = ZBXVersion(*temp)
return nil
}
// MarshalText implements encoding.TextMarshaler interface.
func (v *ZBXVersion) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}