Skip to content

Commit

Permalink
Merge pull request #173 from MarkRosemaker/implement-encoding.TextMar…
Browse files Browse the repository at this point in the history
…shaler-and-encoding.TextUnmarshaler

Implement encoding.TextMarshaler and encoding.TextUnmarshaler
  • Loading branch information
mattfarina committed Nov 28, 2022
2 parents b22e1a4 + 601d8ec commit 2084c82
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
26 changes: 21 additions & 5 deletions version.go
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 @@ -284,7 +286,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 @@ -453,6 +454,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 @@ -487,7 +505,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 @@ -579,7 +596,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
Expand Up @@ -565,6 +565,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

0 comments on commit 2084c82

Please sign in to comment.