From 91c6e39eadf2cc5e1047007e18bb89c43b3ad904 Mon Sep 17 00:00:00 2001 From: Michael Grosser Date: Tue, 4 Jun 2024 21:18:52 -0700 Subject: [PATCH] change Version to only have pointer receivers --- version.go | 40 ++++++++++++++++++++-------------------- version_test.go | 17 ++++++++++++----- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/version.go b/version.go index a1f2414..a4ab4b9 100644 --- a/version.go +++ b/version.go @@ -240,7 +240,7 @@ func MustParse(v string) *Version { // See the Original() method to retrieve the original value. Semantic Versions // don't contain a leading v per the spec. Instead it's optional on // implementation. -func (v Version) String() string { +func (v *Version) String() string { var buf bytes.Buffer fmt.Fprintf(&buf, "%d.%d.%d", v.major, v.minor, v.patch) @@ -260,32 +260,32 @@ func (v *Version) Original() string { } // Major returns the major version. -func (v Version) Major() uint64 { +func (v *Version) Major() uint64 { return v.major } // Minor returns the minor version. -func (v Version) Minor() uint64 { +func (v *Version) Minor() uint64 { return v.minor } // Patch returns the patch version. -func (v Version) Patch() uint64 { +func (v *Version) Patch() uint64 { return v.patch } // Prerelease returns the pre-release version. -func (v Version) Prerelease() string { +func (v *Version) Prerelease() string { return v.pre } // Metadata returns the metadata on the version. -func (v Version) Metadata() string { +func (v *Version) Metadata() string { return v.metadata } // originalVPrefix returns the original 'v' prefix if any. -func (v Version) originalVPrefix() string { +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] @@ -298,8 +298,8 @@ func (v Version) originalVPrefix() string { // it unsets metadata and prerelease values, increments patch number. // If the current version has any of prerelease or metadata information, // it unsets both values and keeps current patch value -func (v Version) IncPatch() Version { - vNext := v +func (v *Version) IncPatch() Version { + vNext := *v // according to http://semver.org/#spec-item-9 // Pre-release versions have a lower precedence than the associated normal version. // according to http://semver.org/#spec-item-10 @@ -321,8 +321,8 @@ func (v Version) IncPatch() Version { // Increments minor number. // Unsets metadata. // Unsets prerelease status. -func (v Version) IncMinor() Version { - vNext := v +func (v *Version) IncMinor() Version { + vNext := *v vNext.metadata = "" vNext.pre = "" vNext.patch = 0 @@ -337,8 +337,8 @@ func (v Version) IncMinor() Version { // Increments major number. // Unsets metadata. // Unsets prerelease status. -func (v Version) IncMajor() Version { - vNext := v +func (v *Version) IncMajor() Version { + vNext := *v vNext.metadata = "" vNext.pre = "" vNext.patch = 0 @@ -350,8 +350,8 @@ func (v Version) IncMajor() Version { // SetPrerelease defines the prerelease value. // Value must not include the required 'hyphen' prefix. -func (v Version) SetPrerelease(prerelease string) (Version, error) { - vNext := v +func (v *Version) SetPrerelease(prerelease string) (Version, error) { + vNext := *v if len(prerelease) > 0 { if err := validatePrerelease(prerelease); err != nil { return vNext, err @@ -364,8 +364,8 @@ func (v Version) SetPrerelease(prerelease string) (Version, error) { // SetMetadata defines metadata value. // Value must not include the required 'plus' prefix. -func (v Version) SetMetadata(metadata string) (Version, error) { - vNext := v +func (v *Version) SetMetadata(metadata string) (Version, error) { + vNext := *v if len(metadata) > 0 { if err := validateMetadata(metadata); err != nil { return vNext, err @@ -466,7 +466,7 @@ func (v *Version) UnmarshalJSON(b []byte) error { } // MarshalJSON implements JSON.Marshaler interface. -func (v Version) MarshalJSON() ([]byte, error) { +func (v *Version) MarshalJSON() ([]byte, error) { return json.Marshal(v.String()) } @@ -483,7 +483,7 @@ func (v *Version) UnmarshalText(text []byte) error { } // MarshalText implements the encoding.TextMarshaler interface. -func (v Version) MarshalText() ([]byte, error) { +func (v *Version) MarshalText() ([]byte, error) { return []byte(v.String()), nil } @@ -505,7 +505,7 @@ func (v *Version) Scan(value interface{}) error { } // Value implements the Driver.Valuer interface. -func (v Version) Value() (driver.Value, error) { +func (v *Version) Value() (driver.Value, error) { return v.String(), nil } diff --git a/version_test.go b/version_test.go index 74a1e91..f607126 100644 --- a/version_test.go +++ b/version_test.go @@ -539,15 +539,16 @@ func TestSetPrerelease(t *testing.T) { func TestSetMetadata(t *testing.T) { tests := []struct { v1 string + metadataWas string metadata string expectedVersion string expectedMetadata string expectedOriginal string expectedErr error }{ - {"1.2.3", "**", "1.2.3", "", "1.2.3", ErrInvalidMetadata}, - {"1.2.3", "meta", "1.2.3+meta", "meta", "1.2.3+meta", nil}, - {"v1.2.4", "meta", "1.2.4+meta", "meta", "v1.2.4+meta", nil}, + {"1.2.3", "", "**", "1.2.3", "", "1.2.3", ErrInvalidMetadata}, + {"1.2.3", "", "meta", "1.2.3+meta", "meta", "1.2.3+meta", nil}, + {"v1.2.4", "", "meta", "1.2.4+meta", "meta", "v1.2.4+meta", nil}, } for _, tc := range tests { @@ -561,8 +562,14 @@ func TestSetMetadata(t *testing.T) { t.Errorf("Expected to get err=%s, but got err=%s", tc.expectedErr, err) } - a := v2.Metadata() - e := tc.expectedMetadata + a := v1.metadata + e := tc.metadataWas + if a != e { + t.Errorf("Expected metadata to not change value=%q, but got %q", e, a) + } + + a = v2.Metadata() + e = tc.expectedMetadata if a != e { t.Errorf("Expected metadata value=%q, but got %q", e, a) }