mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 17:20:24 +08:00
【rich function】VersionCompare replace
This commit is contained in:
parent
e18ca9aac1
commit
4c9b481bdf
@ -10,14 +10,15 @@ func Compare(v1, v2 string) int {
|
|||||||
replaceMap := map[string]string{"V": "", "v": "", "-": "."}
|
replaceMap := map[string]string{"V": "", "v": "", "-": "."}
|
||||||
for k, v := range replaceMap {
|
for k, v := range replaceMap {
|
||||||
if strings.Contains(v1, k) {
|
if strings.Contains(v1, k) {
|
||||||
strings.Replace(v1, k, v, -1)
|
v1 = strings.Replace(v1, k, v, -1)
|
||||||
}
|
}
|
||||||
if strings.Contains(v2, k) {
|
if strings.Contains(v2, k) {
|
||||||
strings.Replace(v2, k, v, -1)
|
v2 = strings.Replace(v2, k, v, -1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
verStr1 := strings.Split(v1, ".")
|
verStr1 := strings.Split(v1, ".")
|
||||||
verStr2 := strings.Split(v2, ".")
|
verStr2 := strings.Split(v2, ".")
|
||||||
|
|
||||||
ver1 := strSlice2IntSlice(verStr1)
|
ver1 := strSlice2IntSlice(verStr1)
|
||||||
ver2 := strSlice2IntSlice(verStr2)
|
ver2 := strSlice2IntSlice(verStr2)
|
||||||
|
|
||||||
@ -64,8 +65,8 @@ func strSlice2IntSlice(strs []string) []int64 {
|
|||||||
return retInt
|
return retInt
|
||||||
}
|
}
|
||||||
|
|
||||||
//operator compare returns true if the first field and the second field are equal else false
|
//operator compare returns true if the first field and the third field equation holds else false
|
||||||
func CompareVersions(v1, v2, operator string) bool {
|
func CompareVersions(v1, operator, v2 string) bool {
|
||||||
com := Compare(v1, v2)
|
com := Compare(v1, v2)
|
||||||
switch operator {
|
switch operator {
|
||||||
case "==":
|
case "==":
|
||||||
|
@ -15,7 +15,7 @@ func TestCustomCompareVersions(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{"1", "1.0.1", ">", false},
|
{"1", "1.0.1", ">", false},
|
||||||
{"1", "0.9.9", ">", true},
|
{"1", "0.9.9", ">", true},
|
||||||
{"1", "1-0.1", "<", true},
|
{"1", "1.0-1", "<", true},
|
||||||
{"1.0.1", "1-0.1", "<", false},
|
{"1.0.1", "1-0.1", "<", false},
|
||||||
{"1.0.1", "1.0.1", "==", true},
|
{"1.0.1", "1.0.1", "==", true},
|
||||||
{"1.0.1", "1.0.2", "==", false},
|
{"1.0.1", "1.0.2", "==", false},
|
||||||
@ -30,7 +30,7 @@ func TestCustomCompareVersions(t *testing.T) {
|
|||||||
|
|
||||||
for _, each := range cases {
|
for _, each := range cases {
|
||||||
t.Run(each.ver1, func(t *testing.T) {
|
t.Run(each.ver1, func(t *testing.T) {
|
||||||
actual := CompareVersions(each.ver1, each.ver2, each.operator)
|
actual := CompareVersions(each.ver1, each.operator, each.ver2)
|
||||||
assert.Equal(t, each.out, actual)
|
assert.Equal(t, each.out, actual)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user