|
|
|
@ -3,92 +3,75 @@ package utils
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/mathx"
|
|
|
|
|
"github.com/tal-tech/go-zero/core/stringx"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//return 0 if they are equal,and 1 if v1>2,and 2 if v1<v2
|
|
|
|
|
func Compare(v1, v2 string) int {
|
|
|
|
|
replaceMap := map[string]string{"V": "", "v": "", "-": "."}
|
|
|
|
|
for k, v := range replaceMap {
|
|
|
|
|
if strings.Contains(v1, k) {
|
|
|
|
|
v1 = strings.Replace(v1, k, v, -1)
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(v2, k) {
|
|
|
|
|
v2 = strings.Replace(v2, k, v, -1)
|
|
|
|
|
}
|
|
|
|
|
var replacer = stringx.NewReplacer(map[string]string{
|
|
|
|
|
"V": "",
|
|
|
|
|
"v": "",
|
|
|
|
|
"-": ".",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// operator compare returns true if the first field and the third field equation holds else false
|
|
|
|
|
func CompareVersions(v1, op, v2 string) bool {
|
|
|
|
|
result := compare(v1, v2)
|
|
|
|
|
switch op {
|
|
|
|
|
case "=", "==":
|
|
|
|
|
return result == 0
|
|
|
|
|
case "<":
|
|
|
|
|
return result == -1
|
|
|
|
|
case ">":
|
|
|
|
|
return result == 1
|
|
|
|
|
case "<=":
|
|
|
|
|
return result == -1 || result == 0
|
|
|
|
|
case ">=":
|
|
|
|
|
return result == 0 || result == 1
|
|
|
|
|
}
|
|
|
|
|
verStr1 := strings.Split(v1, ".")
|
|
|
|
|
verStr2 := strings.Split(v2, ".")
|
|
|
|
|
|
|
|
|
|
ver1 := strSlice2IntSlice(verStr1)
|
|
|
|
|
ver2 := strSlice2IntSlice(verStr2)
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var shorter int
|
|
|
|
|
if len(ver1) > len(ver2) {
|
|
|
|
|
shorter = len(ver2)
|
|
|
|
|
} else {
|
|
|
|
|
shorter = len(ver1)
|
|
|
|
|
}
|
|
|
|
|
// return -1 if v1<v2, 0 if they are equal, and 1 if v1>v2
|
|
|
|
|
func compare(v1, v2 string) int {
|
|
|
|
|
v1 = replacer.Replace(v1)
|
|
|
|
|
v2 = replacer.Replace(v2)
|
|
|
|
|
fields1 := strings.Split(v1, ".")
|
|
|
|
|
fields2 := strings.Split(v2, ".")
|
|
|
|
|
ver1 := strsToInts(fields1)
|
|
|
|
|
ver2 := strsToInts(fields2)
|
|
|
|
|
shorter := mathx.MinInt(len(ver1), len(ver2))
|
|
|
|
|
|
|
|
|
|
for i := 0; i < shorter; i++ {
|
|
|
|
|
if ver1[i] == ver2[i] {
|
|
|
|
|
if shorter-1 == i {
|
|
|
|
|
if len(ver1) == len(ver2) {
|
|
|
|
|
return 0
|
|
|
|
|
} else {
|
|
|
|
|
if len(ver1) > len(ver2) {
|
|
|
|
|
return 1
|
|
|
|
|
} else {
|
|
|
|
|
return 2
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if ver1[i] > ver2[i] {
|
|
|
|
|
return 1
|
|
|
|
|
continue
|
|
|
|
|
} else if ver1[i] < ver2[i] {
|
|
|
|
|
return -1
|
|
|
|
|
} else {
|
|
|
|
|
return 2
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
|
|
|
|
|
if len(ver1) < len(ver2) {
|
|
|
|
|
return -1
|
|
|
|
|
} else if len(ver1) == len(ver2) {
|
|
|
|
|
return 0
|
|
|
|
|
} else {
|
|
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func strSlice2IntSlice(strs []string) []int64 {
|
|
|
|
|
func strsToInts(strs []string) []int64 {
|
|
|
|
|
if len(strs) == 0 {
|
|
|
|
|
return []int64{}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
retInt := make([]int64, 0, len(strs))
|
|
|
|
|
|
|
|
|
|
ret := make([]int64, 0, len(strs))
|
|
|
|
|
for _, str := range strs {
|
|
|
|
|
i, err := strconv.ParseInt(str, 10, 64)
|
|
|
|
|
if err == nil {
|
|
|
|
|
retInt = append(retInt, i)
|
|
|
|
|
}
|
|
|
|
|
i, _ := strconv.ParseInt(str, 10, 64)
|
|
|
|
|
ret = append(ret, i)
|
|
|
|
|
}
|
|
|
|
|
return retInt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//operator compare returns true if the first field and the third field equation holds else false
|
|
|
|
|
func CompareVersions(v1, operator, v2 string) bool {
|
|
|
|
|
com := Compare(v1, v2)
|
|
|
|
|
switch operator {
|
|
|
|
|
case "==":
|
|
|
|
|
if com == 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
case "<":
|
|
|
|
|
if com == 2 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
case ">":
|
|
|
|
|
if com == 1 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
case "<=":
|
|
|
|
|
if com == 0 || com == 2 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
case ">=":
|
|
|
|
|
if com == 0 || com == 1 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|