diff --git a/Makefile b/v4/Makefile similarity index 100% rename from Makefile rename to v4/Makefile diff --git a/benchmark_test.go b/v4/benchmark_test.go similarity index 100% rename from benchmark_test.go rename to v4/benchmark_test.go diff --git a/collection.go b/v4/collection.go similarity index 100% rename from collection.go rename to v4/collection.go diff --git a/collection_test.go b/v4/collection_test.go similarity index 100% rename from collection_test.go rename to v4/collection_test.go diff --git a/constraints.go b/v4/constraints.go similarity index 100% rename from constraints.go rename to v4/constraints.go diff --git a/constraints_test.go b/v4/constraints_test.go similarity index 100% rename from constraints_test.go rename to v4/constraints_test.go diff --git a/doc.go b/v4/doc.go similarity index 100% rename from doc.go rename to v4/doc.go diff --git a/fuzz.go b/v4/fuzz.go similarity index 100% rename from fuzz.go rename to v4/fuzz.go diff --git a/go.mod b/v4/go.mod similarity index 100% rename from go.mod rename to v4/go.mod diff --git a/go.sum b/v4/go.sum similarity index 100% rename from go.sum rename to v4/go.sum diff --git a/version.go b/v4/version.go similarity index 97% rename from version.go rename to v4/version.go index 018ab80..bff25ce 100644 --- a/version.go +++ b/v4/version.go @@ -458,6 +458,23 @@ func (v *Version) Value() (driver.Value, error) { return v.String(), nil } +// 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 +} + func compareSegment(v, o uint64) int { if v < o { return -1 diff --git a/version_test.go b/v4/version_test.go similarity index 95% rename from version_test.go rename to v4/version_test.go index bf5b8ac..9d18a69 100644 --- a/version_test.go +++ b/v4/version_test.go @@ -623,3 +623,39 @@ func TestValidateMetadata(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) + } +}