-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4043e54
commit 97dd0fd
Showing
16 changed files
with
511 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package version | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors" | ||
) | ||
|
||
type version struct { | ||
Major uint64 | ||
Minor uint64 | ||
Patch uint64 | ||
Suffix string | ||
} | ||
|
||
func (lhs version) Less(rhs version) bool { | ||
if lhs.Major < rhs.Major { | ||
return true | ||
} | ||
if lhs.Major > rhs.Major { | ||
return false | ||
} | ||
if lhs.Minor < rhs.Minor { | ||
return true | ||
} | ||
if lhs.Minor > rhs.Minor { | ||
return false | ||
} | ||
if lhs.Patch < rhs.Patch { | ||
return true | ||
} | ||
if lhs.Patch > rhs.Patch { | ||
return false | ||
} | ||
return lhs.Suffix < rhs.Suffix | ||
} | ||
|
||
// Lt compare lhs and rhs as (lhs < rhs) | ||
func Lt(lhs, rhs string) bool { | ||
v1, err := parse(lhs) | ||
if err != nil { | ||
return false | ||
} | ||
v2, err := parse(rhs) | ||
if err != nil { | ||
return false | ||
} | ||
return v1.Less(v2) | ||
} | ||
|
||
// Gte compare lhs and rhs as (lhs >= rhs) | ||
func Gte(lhs, rhs string) bool { | ||
v1, err := parse(lhs) | ||
if err != nil { | ||
return false | ||
} | ||
v2, err := parse(rhs) | ||
if err != nil { | ||
return false | ||
} | ||
if v1.Less(v2) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func parse(s string) (v version, err error) { | ||
ss := strings.SplitN(s, "-", 2) | ||
if len(ss) == 2 { | ||
v.Suffix = ss[1] | ||
} | ||
sss := strings.SplitN(ss[0], ".", 3) | ||
if len(sss) == 3 { | ||
v.Patch, err = strconv.ParseUint(sss[2], 10, 64) | ||
if err != nil { | ||
return version{}, xerrors.WithStackTrace(err) | ||
} | ||
} | ||
if len(sss) >= 2 { | ||
v.Minor, err = strconv.ParseUint(sss[1], 10, 64) | ||
if err != nil { | ||
return version{}, xerrors.WithStackTrace(err) | ||
} | ||
} | ||
if len(sss) >= 1 { | ||
v.Major, err = strconv.ParseUint(sss[0], 10, 64) | ||
if err != nil { | ||
return version{}, xerrors.WithStackTrace(err) | ||
} | ||
} | ||
return v, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package version | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
for _, tt := range []struct { | ||
s string | ||
v version | ||
err bool | ||
}{ | ||
{ | ||
s: "1", | ||
v: version{Major: 1}, | ||
err: false, | ||
}, | ||
{ | ||
s: "1.2", | ||
v: version{Major: 1, Minor: 2}, | ||
err: false, | ||
}, | ||
{ | ||
s: "1.2.3", | ||
v: version{Major: 1, Minor: 2, Patch: 3}, | ||
err: false, | ||
}, | ||
{ | ||
s: "1.2.3-alpha", | ||
v: version{Major: 1, Minor: 2, Patch: 3, Suffix: "alpha"}, | ||
err: false, | ||
}, | ||
{ | ||
s: "22.5", | ||
v: version{Major: 22, Minor: 5}, | ||
err: false, | ||
}, | ||
{ | ||
s: "23.1", | ||
v: version{Major: 23, Minor: 1}, | ||
err: false, | ||
}, | ||
{ | ||
s: "23.2", | ||
v: version{Major: 23, Minor: 2}, | ||
err: false, | ||
}, | ||
{ | ||
s: "trunk", | ||
v: version{}, | ||
err: true, | ||
}, | ||
} { | ||
t.Run(tt.s, func(t *testing.T) { | ||
v, err := parse(tt.s) | ||
if tt.err { | ||
require.Error(t, err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.v, v) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestLt(t *testing.T) { | ||
for _, tt := range []struct { | ||
lhs string | ||
rhs string | ||
less bool | ||
}{ | ||
{ | ||
lhs: "1", | ||
rhs: "2", | ||
less: true, | ||
}, | ||
{ | ||
lhs: "2", | ||
rhs: "1", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "1", | ||
rhs: "1", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "22.5", | ||
rhs: "23.1", | ||
less: true, | ||
}, | ||
{ | ||
lhs: "23.1", | ||
rhs: "22.5", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "trunk", | ||
rhs: "22.5", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "trunk", | ||
rhs: "23.1", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "22.5", | ||
rhs: "trunk", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "23.1", | ||
rhs: "trunk", | ||
less: false, | ||
}, | ||
} { | ||
t.Run("", func(t *testing.T) { | ||
require.Equal(t, tt.less, Lt(tt.lhs, tt.rhs)) | ||
}) | ||
} | ||
} | ||
|
||
func TestGte(t *testing.T) { | ||
for _, tt := range []struct { | ||
lhs string | ||
rhs string | ||
less bool | ||
}{ | ||
{ | ||
lhs: "1", | ||
rhs: "2", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "2", | ||
rhs: "1", | ||
less: true, | ||
}, | ||
{ | ||
lhs: "1", | ||
rhs: "1", | ||
less: true, | ||
}, | ||
{ | ||
lhs: "22.5", | ||
rhs: "23.1", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "23.1", | ||
rhs: "22.5", | ||
less: true, | ||
}, | ||
{ | ||
lhs: "trunk", | ||
rhs: "22.5", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "trunk", | ||
rhs: "23.1", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "22.5", | ||
rhs: "trunk", | ||
less: false, | ||
}, | ||
{ | ||
lhs: "23.1", | ||
rhs: "trunk", | ||
less: false, | ||
}, | ||
} { | ||
t.Run("", func(t *testing.T) { | ||
require.Equal(t, tt.less, Gte(tt.lhs, tt.rhs)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package version | ||
|
||
const ( | ||
Major = "3" | ||
Minor = "48" | ||
Patch = "8" | ||
) | ||
|
||
const Version = Major + "." + Minor + "." + Patch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.