/
version.go
47 lines (38 loc) · 1.06 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
package builder
import (
"github.com/Masterminds/semver"
"github.com/pkg/errors"
)
// Version is an extension to semver.Version to make it marshalable.
type Version struct {
semver.Version
}
// VersionMustParse parses a string into a Version
func VersionMustParse(v string) *Version {
return &Version{Version: *semver.MustParse(v)}
}
// String returns the string value of the Version
func (v *Version) String() string {
return v.Version.String()
}
// Equal compares two Versions
func (v *Version) Equal(other *Version) bool {
if other != nil {
return v.Version.Equal(&other.Version)
}
return false
}
// MarshalText makes Version satisfy the encoding.TextMarshaler interface.
func (v *Version) MarshalText() ([]byte, error) {
return []byte(v.Version.Original()), nil
}
// UnmarshalText makes Version satisfy the encoding.TextUnmarshaler interface.
func (v *Version) UnmarshalText(text []byte) error {
s := string(text)
w, err := semver.NewVersion(s)
if err != nil {
return errors.Wrapf(err, "invalid semantic version %s", s)
}
v.Version = *w
return nil
}