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

fix for the cassandra version unmarshal #1836

Open
wants to merge 1 commit into
base: trunk
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
24 changes: 22 additions & 2 deletions host_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (

type cassVersion struct {
Major, Minor, Patch int
AdditionalNotation string
}

func (c *cassVersion) Set(v string) error {
Expand Down Expand Up @@ -87,13 +88,29 @@ func (c *cassVersion) unmarshal(data []byte) error {

c.Minor, err = strconv.Atoi(v[1])
if err != nil {
return fmt.Errorf("invalid minor version %v: %v", v[1], err)
vMinor := strings.Split(v[1], "-")
if len(vMinor) < 2 {
return fmt.Errorf("invalid minor version %v: %v", v[1], err)
}
c.Minor, err = strconv.Atoi(vMinor[0])
if err != nil {
return fmt.Errorf("invalid minor version %v: %v", v[1], err)
}
c.AdditionalNotation = vMinor[1]
}

if len(v) > 2 {
c.Patch, err = strconv.Atoi(v[2])
if err != nil {
return fmt.Errorf("invalid patch version %v: %v", v[2], err)
vPatch := strings.Split(v[2], "-")
if len(vPatch) < 2 {
return fmt.Errorf("invalid patch version %v: %v", v[2], err)
}
c.Patch, err = strconv.Atoi(vPatch[0])
if err != nil {
return fmt.Errorf("invalid patch version %v: %v", v[2], err)
}
c.AdditionalNotation = vPatch[1]
}
}

Expand Down Expand Up @@ -121,6 +138,9 @@ func (c cassVersion) AtLeast(major, minor, patch int) bool {
}

func (c cassVersion) String() string {
if c.AdditionalNotation != "" {
return fmt.Sprintf("%d.%d.%d-%v", c.Major, c.Minor, c.Patch, c.AdditionalNotation)
}
return fmt.Sprintf("v%d.%d.%d", c.Major, c.Minor, c.Patch)
}

Expand Down
25 changes: 16 additions & 9 deletions host_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package gocql

import (
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
Expand All @@ -41,9 +42,11 @@ func TestUnmarshalCassVersion(t *testing.T) {
data string
version cassVersion
}{
{"3.2", cassVersion{3, 2, 0}},
{"2.10.1-SNAPSHOT", cassVersion{2, 10, 1}},
{"1.2.3", cassVersion{1, 2, 3}},
{"3.2", cassVersion{3, 2, 0, ""}},
{"2.10.1-SNAPSHOT", cassVersion{2, 10, 1, ""}},
{"1.2.3", cassVersion{1, 2, 3, ""}},
{"4.0-rc2", cassVersion{4, 0, 0, "rc2"}},
{"4.3.2-rc1", cassVersion{4, 3, 2, "rc1"}},
}

for i, test := range tests {
Expand All @@ -53,21 +56,25 @@ func TestUnmarshalCassVersion(t *testing.T) {
} else if *v != test.version {
t.Errorf("%d: expected %#+v got %#+v", i, test.version, *v)
}
fmt.Println(v.String())
}
}

func TestCassVersionBefore(t *testing.T) {
tests := [...]struct {
version cassVersion
major, minor, patch int
AdditionalNotation string
}{
{cassVersion{1, 0, 0}, 0, 0, 0},
{cassVersion{0, 1, 0}, 0, 0, 0},
{cassVersion{0, 0, 1}, 0, 0, 0},
{cassVersion{1, 0, 0, ""}, 0, 0, 0, ""},
{cassVersion{0, 1, 0, ""}, 0, 0, 0, ""},
{cassVersion{0, 0, 1, ""}, 0, 0, 0, ""},

{cassVersion{1, 0, 0}, 0, 1, 0},
{cassVersion{0, 1, 0}, 0, 0, 1},
{cassVersion{4, 1, 0}, 3, 1, 2},
{cassVersion{1, 0, 0, ""}, 0, 1, 0, ""},
{cassVersion{0, 1, 0, ""}, 0, 0, 1, ""},
{cassVersion{4, 1, 0, ""}, 3, 1, 2, ""},

{cassVersion{4, 1, 0, ""}, 3, 1, 2, ""},
}

for i, test := range tests {
Expand Down