From 4c9b481bdf51b1ee51a81162ec703a5dd6fdd794 Mon Sep 17 00:00:00 2001 From: sunwei <18801147024@163.com> Date: Sun, 9 Aug 2020 22:20:47 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90rich=20function=E3=80=91VersionCompare?= =?UTF-8?q?=20replace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/utils/version.go | 9 +++++---- core/utils/version_test.go | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/utils/version.go b/core/utils/version.go index bcd35667..cf43157a 100644 --- a/core/utils/version.go +++ b/core/utils/version.go @@ -10,14 +10,15 @@ func Compare(v1, v2 string) int { replaceMap := map[string]string{"V": "", "v": "", "-": "."} for k, v := range replaceMap { if strings.Contains(v1, k) { - strings.Replace(v1, k, v, -1) + v1 = strings.Replace(v1, k, v, -1) } if strings.Contains(v2, k) { - strings.Replace(v2, k, v, -1) + v2 = strings.Replace(v2, k, v, -1) } } verStr1 := strings.Split(v1, ".") verStr2 := strings.Split(v2, ".") + ver1 := strSlice2IntSlice(verStr1) ver2 := strSlice2IntSlice(verStr2) @@ -64,8 +65,8 @@ func strSlice2IntSlice(strs []string) []int64 { return retInt } -//operator compare returns true if the first field and the second field are equal else false -func CompareVersions(v1, v2, operator string) bool { +//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 "==": diff --git a/core/utils/version_test.go b/core/utils/version_test.go index 80875650..90e75213 100644 --- a/core/utils/version_test.go +++ b/core/utils/version_test.go @@ -15,7 +15,7 @@ func TestCustomCompareVersions(t *testing.T) { }{ {"1", "1.0.1", ">", false}, {"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", "==", true}, {"1.0.1", "1.0.2", "==", false}, @@ -30,7 +30,7 @@ func TestCustomCompareVersions(t *testing.T) { for _, each := range cases { 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) }) }