Skip to content

Commit 2084c82

Browse files
authored
Merge pull request #173 from MarkRosemaker/implement-encoding.TextMarshaler-and-encoding.TextUnmarshaler
Implement encoding.TextMarshaler and encoding.TextUnmarshaler
2 parents b22e1a4 + 601d8ec commit 2084c82

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

version.go

+21-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ func init() {
5555
versionRegex = regexp.MustCompile("^" + semVerRegex + "$")
5656
}
5757

58-
const num string = "0123456789"
59-
const allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
58+
const (
59+
num string = "0123456789"
60+
allowed string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-" + num
61+
)
6062

6163
// StrictNewVersion parses a given version and returns an instance of Version or
6264
// an error if unable to parse the version. Only parses valid semantic versions.
@@ -284,7 +286,6 @@ func (v Version) Metadata() string {
284286

285287
// originalVPrefix returns the original 'v' prefix if any.
286288
func (v Version) originalVPrefix() string {
287-
288289
// Note, only lowercase v is supported as a prefix by the parser.
289290
if v.original != "" && v.original[:1] == "v" {
290291
return v.original[:1]
@@ -453,6 +454,23 @@ func (v Version) MarshalJSON() ([]byte, error) {
453454
return json.Marshal(v.String())
454455
}
455456

457+
// UnmarshalText implements the encoding.TextUnmarshaler interface.
458+
func (v *Version) UnmarshalText(text []byte) error {
459+
temp, err := NewVersion(string(text))
460+
if err != nil {
461+
return err
462+
}
463+
464+
*v = *temp
465+
466+
return nil
467+
}
468+
469+
// MarshalText implements the encoding.TextMarshaler interface.
470+
func (v Version) MarshalText() ([]byte, error) {
471+
return []byte(v.String()), nil
472+
}
473+
456474
// Scan implements the SQL.Scanner interface.
457475
func (v *Version) Scan(value interface{}) error {
458476
var s string
@@ -487,7 +505,6 @@ func compareSegment(v, o uint64) int {
487505
}
488506

489507
func comparePrerelease(v, o string) int {
490-
491508
// split the prelease versions by their part. The separator, per the spec,
492509
// is a .
493510
sparts := strings.Split(v, ".")
@@ -579,7 +596,6 @@ func comparePrePart(s, o string) int {
579596
return 1
580597
}
581598
return -1
582-
583599
}
584600

585601
// Like strings.ContainsAny but does an only instead of any.

version_test.go

+36
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,42 @@ func TestJsonUnmarshal(t *testing.T) {
565565
}
566566
}
567567

568+
func TestTextMarshal(t *testing.T) {
569+
sVer := "1.1.1"
570+
571+
x, err := StrictNewVersion(sVer)
572+
if err != nil {
573+
t.Errorf("Error creating version: %s", err)
574+
}
575+
576+
out, err2 := x.MarshalText()
577+
if err2 != nil {
578+
t.Errorf("Error marshaling version: %s", err2)
579+
}
580+
581+
got := string(out)
582+
want := sVer
583+
if got != want {
584+
t.Errorf("Error marshaling unexpected marshaled content: got=%q want=%q", got, want)
585+
}
586+
}
587+
588+
func TestTextUnmarshal(t *testing.T) {
589+
sVer := "1.1.1"
590+
ver := &Version{}
591+
592+
err := ver.UnmarshalText([]byte(sVer))
593+
if err != nil {
594+
t.Errorf("Error unmarshaling version: %s", err)
595+
}
596+
597+
got := ver.String()
598+
want := sVer
599+
if got != want {
600+
t.Errorf("Error unmarshaling unexpected object content: got=%q want=%q", got, want)
601+
}
602+
}
603+
568604
func TestSQLScanner(t *testing.T) {
569605
sVer := "1.1.1"
570606
x, err := StrictNewVersion(sVer)

0 commit comments

Comments
 (0)