Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change Version to only have pointer receivers #240

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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())
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down
17 changes: 12 additions & 5 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
Loading