Skip to content

Commit 3a104d8

Browse files
committed
Added Version type & functions
1 parent 9b52e17 commit 3a104d8

File tree

3 files changed

+200
-1
lines changed

3 files changed

+200
-1
lines changed

client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type VersionInfo struct {
5252
Server string `json:"server,omitempty"`
5353
// The server version string. The string has the format "major.minor.sub".
5454
// Major and minor will be numeric, and sub may contain a number or a textual version.
55-
Version string `json:"version,omitempty"`
55+
Version Version `json:"version,omitempty"`
5656
// Type of license of the server
5757
License string `json:"license,omitempty"`
5858
// Optional additional details. This is returned only if the context is configured using WithDetails.

test/version_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package test
24+
25+
import (
26+
"testing"
27+
28+
driver "github.com/arangodb/go-driver"
29+
)
30+
31+
// TestVersion tests Version functions.
32+
func TestVersion(t *testing.T) {
33+
34+
tests := []struct {
35+
Input driver.Version
36+
Major int
37+
Minor int
38+
Sub string
39+
SubInt int
40+
SubIsInt bool
41+
}{
42+
{"1.2.3", 1, 2, "3", 3, true},
43+
{"", 0, 0, "", 0, false},
44+
{"1.2.3a", 1, 2, "3a", 0, false},
45+
{"13.12", 13, 12, "", 0, false},
46+
}
47+
48+
for _, test := range tests {
49+
if v := test.Input.Major(); v != test.Major {
50+
t.Errorf("Major failed for '%s', expected %d, got %d", test.Input, test.Major, v)
51+
}
52+
if v := test.Input.Minor(); v != test.Minor {
53+
t.Errorf("Minor failed for '%s', expected %d, got %d", test.Input, test.Minor, v)
54+
}
55+
if v := test.Input.Sub(); v != test.Sub {
56+
t.Errorf("Sub failed for '%s', expected '%s', got '%s'", test.Input, test.Sub, v)
57+
}
58+
if v, vIsInt := test.Input.SubInt(); vIsInt != test.SubIsInt || v != test.SubInt {
59+
t.Errorf("SubInt failed for '%s', expected (%d,%v), got (%d,%v)", test.Input, test.SubInt, test.SubIsInt, v, vIsInt)
60+
}
61+
}
62+
}
63+
64+
// TestVersionCompareTo tests Version.CompareTo.
65+
func TestVersionCompareTo(t *testing.T) {
66+
tests := []struct {
67+
A driver.Version
68+
B driver.Version
69+
Result int
70+
}{
71+
{"1.2.3", "1.2.3", 0},
72+
{"1.2", "1.2.3", -1},
73+
{"1.2.3", "2.3.5", -1},
74+
{"1.2", "1.1", 1},
75+
{"1.2.3", "1.1.7", 1},
76+
{"2.2", "1.2.a", 1},
77+
{"1", "1.2.3", -1},
78+
{"1.2.a", "1.2.3", 1},
79+
{"1.2.3", "1.2.a", -1},
80+
{"", "", 0},
81+
{"1", "1", 0},
82+
{"2.1", "2.1", 0},
83+
}
84+
85+
for _, test := range tests {
86+
if r := test.A.CompareTo(test.B); r != test.Result {
87+
t.Errorf("CompareTo('%s', '%s') failed, expected %d, got %d", test.A, test.B, test.Result, r)
88+
}
89+
}
90+
}

version.go

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
package driver
24+
25+
import (
26+
"strconv"
27+
"strings"
28+
)
29+
30+
// Version holds a server version string. The string has the format "major.minor.sub".
31+
// Major and minor will be numeric, and sub may contain a number or a textual version.
32+
type Version string
33+
34+
// Major returns the major part of the version
35+
// E.g. "3.1.7" -> 3
36+
func (v Version) Major() int {
37+
parts := strings.Split(string(v), ".")
38+
result, _ := strconv.Atoi(parts[0])
39+
return result
40+
}
41+
42+
// Minor returns the minor part of the version.
43+
// E.g. "3.1.7" -> 1
44+
func (v Version) Minor() int {
45+
parts := strings.Split(string(v), ".")
46+
if len(parts) >= 2 {
47+
result, _ := strconv.Atoi(parts[1])
48+
return result
49+
}
50+
return 0
51+
}
52+
53+
// Sub returns the sub part of the version.
54+
// E.g. "3.1.7" -> "7"
55+
func (v Version) Sub() string {
56+
parts := strings.SplitN(string(v), ".", 3)
57+
if len(parts) == 3 {
58+
return parts[2]
59+
}
60+
return ""
61+
}
62+
63+
// SubInt returns the sub part of the version as integer.
64+
// The bool return value indicates if the sub part is indeed a number.
65+
// E.g. "3.1.7" -> 7, true
66+
// E.g. "3.1.foo" -> 0, false
67+
func (v Version) SubInt() (int, bool) {
68+
result, err := strconv.Atoi(v.Sub())
69+
return result, err == nil
70+
}
71+
72+
// CompareTo returns an integer comparing two version.
73+
// The result will be 0 if v==other, -1 if v < other, and +1 if v > other.
74+
// If major & minor parts are equal and sub part is not a number,
75+
// the sub part will be compared using lexicographical string comparison.
76+
func (v Version) CompareTo(other Version) int {
77+
a := v.Major()
78+
b := other.Major()
79+
if a < b {
80+
return -1
81+
}
82+
if a > b {
83+
return 1
84+
}
85+
86+
a = v.Minor()
87+
b = other.Minor()
88+
if a < b {
89+
return -1
90+
}
91+
if a > b {
92+
return 1
93+
}
94+
95+
a, aIsInt := v.SubInt()
96+
b, bIsInt := other.SubInt()
97+
98+
if !aIsInt || !bIsInt {
99+
// Do a string comparison
100+
return strings.Compare(v.Sub(), other.Sub())
101+
}
102+
if a < b {
103+
return -1
104+
}
105+
if a > b {
106+
return 1
107+
}
108+
return 0
109+
}

0 commit comments

Comments
 (0)