From bc087ca6ee1edabe43cd6f4b47b7dae01f684f25 Mon Sep 17 00:00:00 2001 From: HD Stich Date: Tue, 26 Jul 2022 09:07:50 +0200 Subject: [PATCH] Moved sources to version folder Merged PR #173 of upstream repository --- Makefile => v4/Makefile | 0 benchmark_test.go => v4/benchmark_test.go | 0 collection.go => v4/collection.go | 0 collection_test.go => v4/collection_test.go | 0 constraints.go => v4/constraints.go | 0 constraints_test.go => v4/constraints_test.go | 0 doc.go => v4/doc.go | 0 fuzz.go => v4/fuzz.go | 0 go.mod => v4/go.mod | 0 go.sum => v4/go.sum | 0 version.go => v4/version.go | 17 +++++++++ version_test.go => v4/version_test.go | 36 +++++++++++++++++++ 12 files changed, 53 insertions(+) rename Makefile => v4/Makefile (100%) rename benchmark_test.go => v4/benchmark_test.go (100%) rename collection.go => v4/collection.go (100%) rename collection_test.go => v4/collection_test.go (100%) rename constraints.go => v4/constraints.go (100%) rename constraints_test.go => v4/constraints_test.go (100%) rename doc.go => v4/doc.go (100%) rename fuzz.go => v4/fuzz.go (100%) rename go.mod => v4/go.mod (100%) rename go.sum => v4/go.sum (100%) rename version.go => v4/version.go (97%) rename version_test.go => v4/version_test.go (95%) 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) + } +}