Skip to content

Commit

Permalink
fix for the cassandra version unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
tengu-alt committed Oct 25, 2024
1 parent 974fa12 commit e976b1a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
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

0 comments on commit e976b1a

Please sign in to comment.