Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ func init() {
versionRegex = regexp.MustCompile("^" + semVerRegex + "$")
}

const num string = "0123456789"
const allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
const (
num string = "0123456789"
allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
)

// StrictNewVersion parses a given version and returns an instance of Version or
// an error if unable to parse the version. Only parses valid semantic versions.
Expand Down Expand Up @@ -267,7 +269,6 @@ func (v Version) Metadata() string {

// originalVPrefix returns the original 'v' prefix if any.
func (v Version) originalVPrefix() string {

// Note, only lowercase v is supported as a prefix by the parser.
if v.original != "" && v.original[:1] == "v" {
return v.original[:1]
Expand Down Expand Up @@ -436,6 +437,23 @@ func (v Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}

// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (v *Version) UnmarshalText(text []byte) error {
temp, err := NewVersion(string(text))
if err != nil {
return err
}

*v = *temp

return nil
}

// MarshalText implements the encoding.TextMarshaler interface.
func (v Version) MarshalText() ([]byte, error) {
return []byte(v.String()), nil
}

// Scan implements the SQL.Scanner interface.
func (v *Version) Scan(value interface{}) error {
var s string
Expand Down Expand Up @@ -470,7 +488,6 @@ func compareSegment(v, o uint64) int {
}

func comparePrerelease(v, o string) int {

// split the prelease versions by their part. The separator, per the spec,
// is a .
sparts := strings.Split(v, ".")
Expand Down Expand Up @@ -562,7 +579,6 @@ func comparePrePart(s, o string) int {
return 1
}
return -1

}

// Like strings.ContainsAny but does an only instead of any.
Expand Down
36 changes: 36 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,42 @@ func TestJsonUnmarshal(t *testing.T) {
}
}

func TestTextMarshal(t *testing.T) {
sVer := "1.1.1"

x, err := StrictNewVersion(sVer)
if err != nil {
t.Errorf("Error creating version: %s", err)
}

out, err2 := x.MarshalText()
if err2 != nil {
t.Errorf("Error marshaling version: %s", err2)
}

got := string(out)
want := sVer
if got != want {
t.Errorf("Error marshaling unexpected marshaled content: got=%q want=%q", got, want)
}
}

func TestTextUnmarshal(t *testing.T) {
sVer := "1.1.1"
ver := &Version{}

err := ver.UnmarshalText([]byte(sVer))
if err != nil {
t.Errorf("Error unmarshaling version: %s", err)
}

got := ver.String()
want := sVer
if got != want {
t.Errorf("Error unmarshaling unexpected object content: got=%q want=%q", got, want)
}
}

func TestSQLScanner(t *testing.T) {
sVer := "1.1.1"
x, err := StrictNewVersion(sVer)
Expand Down