Skip to content

Commit b7c0ca4

Browse files
committed
Cassandra version unmarshal fix
FIx for the issue, when the driver is unable to unmarshal Cassandra version, which contains additional annotation (suffix). patch by Oleksandr Luzhniy; reviewed by João Reis, James Hartig, Danylo Savchenko, for CASSGO-49
1 parent 974fa12 commit b7c0ca4

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
### Changed
1212

1313
### Fixed
14+
- Cassandra version unmarshal fix (CASSGO-49)
1415

1516
## [1.6.0] - 2023-08-28
1617

host_source.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const (
5656

5757
type cassVersion struct {
5858
Major, Minor, Patch int
59+
Qualifier string
5960
}
6061

6162
func (c *cassVersion) Set(v string) error {
@@ -87,13 +88,30 @@ func (c *cassVersion) unmarshal(data []byte) error {
8788

8889
c.Minor, err = strconv.Atoi(v[1])
8990
if err != nil {
90-
return fmt.Errorf("invalid minor version %v: %v", v[1], err)
91+
vMinor := strings.Split(v[1], "-")
92+
if len(vMinor) < 2 {
93+
return fmt.Errorf("invalid minor version %v: %v", v[1], err)
94+
}
95+
c.Minor, err = strconv.Atoi(vMinor[0])
96+
if err != nil {
97+
return fmt.Errorf("invalid minor version %v: %v", v[1], err)
98+
}
99+
c.Qualifier = v[1][strings.Index(v[1], "-")+1:]
100+
return nil
91101
}
92102

93103
if len(v) > 2 {
94104
c.Patch, err = strconv.Atoi(v[2])
95105
if err != nil {
96-
return fmt.Errorf("invalid patch version %v: %v", v[2], err)
106+
vPatch := strings.Split(v[2], "-")
107+
if len(vPatch) < 2 {
108+
return fmt.Errorf("invalid patch version %v: %v", v[2], err)
109+
}
110+
c.Patch, err = strconv.Atoi(vPatch[0])
111+
if err != nil {
112+
return fmt.Errorf("invalid patch version %v: %v", v[2], err)
113+
}
114+
c.Qualifier = v[2][strings.Index(v[2], "-")+1:]
97115
}
98116
}
99117

@@ -121,6 +139,9 @@ func (c cassVersion) AtLeast(major, minor, patch int) bool {
121139
}
122140

123141
func (c cassVersion) String() string {
142+
if c.Qualifier != "" {
143+
return fmt.Sprintf("%d.%d.%d-%v", c.Major, c.Minor, c.Patch, c.Qualifier)
144+
}
124145
return fmt.Sprintf("v%d.%d.%d", c.Major, c.Minor, c.Patch)
125146
}
126147

host_source_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package gocql
2929

3030
import (
3131
"errors"
32+
"fmt"
3233
"net"
3334
"sync"
3435
"sync/atomic"
@@ -41,9 +42,13 @@ func TestUnmarshalCassVersion(t *testing.T) {
4142
data string
4243
version cassVersion
4344
}{
44-
{"3.2", cassVersion{3, 2, 0}},
45-
{"2.10.1-SNAPSHOT", cassVersion{2, 10, 1}},
46-
{"1.2.3", cassVersion{1, 2, 3}},
45+
{"3.2", cassVersion{3, 2, 0, ""}},
46+
{"2.10.1-SNAPSHOT", cassVersion{2, 10, 1, ""}},
47+
{"1.2.3", cassVersion{1, 2, 3, ""}},
48+
{"4.0-rc2", cassVersion{4, 0, 0, "rc2"}},
49+
{"4.3.2-rc1", cassVersion{4, 3, 2, "rc1"}},
50+
{"4.3.2-rc1-qualifier1", cassVersion{4, 3, 2, "rc1-qualifier1"}},
51+
{"4.3-rc1-qualifier1", cassVersion{4, 3, 0, "rc1-qualifier1"}},
4752
}
4853

4954
for i, test := range tests {
@@ -53,21 +58,25 @@ func TestUnmarshalCassVersion(t *testing.T) {
5358
} else if *v != test.version {
5459
t.Errorf("%d: expected %#+v got %#+v", i, test.version, *v)
5560
}
61+
fmt.Println(v.String())
5662
}
5763
}
5864

5965
func TestCassVersionBefore(t *testing.T) {
6066
tests := [...]struct {
6167
version cassVersion
6268
major, minor, patch int
69+
Qualifier string
6370
}{
64-
{cassVersion{1, 0, 0}, 0, 0, 0},
65-
{cassVersion{0, 1, 0}, 0, 0, 0},
66-
{cassVersion{0, 0, 1}, 0, 0, 0},
71+
{cassVersion{1, 0, 0, ""}, 0, 0, 0, ""},
72+
{cassVersion{0, 1, 0, ""}, 0, 0, 0, ""},
73+
{cassVersion{0, 0, 1, ""}, 0, 0, 0, ""},
6774

68-
{cassVersion{1, 0, 0}, 0, 1, 0},
69-
{cassVersion{0, 1, 0}, 0, 0, 1},
70-
{cassVersion{4, 1, 0}, 3, 1, 2},
75+
{cassVersion{1, 0, 0, ""}, 0, 1, 0, ""},
76+
{cassVersion{0, 1, 0, ""}, 0, 0, 1, ""},
77+
{cassVersion{4, 1, 0, ""}, 3, 1, 2, ""},
78+
79+
{cassVersion{4, 1, 0, ""}, 3, 1, 2, ""},
7180
}
7281

7382
for i, test := range tests {

0 commit comments

Comments
 (0)