From 88d3066307ec99755e2c834b6150508fdaba81ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 27 Dec 2023 17:52:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=89=88=E6=9C=AC=E5=8F=B7?= =?UTF-8?q?=E6=AF=94=E8=BE=83=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/define/version.go | 15 +++++++ tool/version.go | 88 ++++++++++++++++++++++++++++++++++++++++++ tool/version_test.go | 22 +++++++++++ 3 files changed, 125 insertions(+) create mode 100644 tool/define/version.go create mode 100644 tool/version.go create mode 100644 tool/version_test.go diff --git a/tool/define/version.go b/tool/define/version.go new file mode 100644 index 0000000..5db13b4 --- /dev/null +++ b/tool/define/version.go @@ -0,0 +1,15 @@ +// Package define ... +// +// Description : define ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-12-27 17:08 +package define + +const ( + VersionCompareFail = -2 // 版本比较失败 + VersionOneMax = -1 // 第一个版本号大 + VersionEqual = 0 // 两个版本号相等 + VersionTwoMax = 1 // 第二个版本号大 +) diff --git a/tool/version.go b/tool/version.go new file mode 100644 index 0000000..01a78e1 --- /dev/null +++ b/tool/version.go @@ -0,0 +1,88 @@ +// Package tool ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-12-27 17:06 +package tool + +import ( + "git.zhangdeman.cn/zhangdeman/wrapper" + "git.zhangdeman.cn/zhangdeman/wrapper/tool/define" +) + +var ( + Version = &version{} +) + +type version struct { +} + +// getVersionArr 解析版本号 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:42 2023/12/27 +func (v *version) getVersionArr(versionOne string, versionTwo string) ([]int64, []int64, error) { + oneWrapper := wrapper.String(versionOne).ToInt64Slice(".") + twoWrapper := wrapper.String(versionTwo).ToInt64Slice(".") + if oneWrapper.Err != nil { + return nil, nil, oneWrapper.Err + } + if twoWrapper.Err != nil { + return nil, nil, twoWrapper.Err + } + return oneWrapper.Value, twoWrapper.Value, nil +} + +// Compare 比较版本号的大小, 版本号格式必须是 x.y.z 的形式,几个 . 不限制, x、y、z 必须是是数字 +// +// strictMode 严格模式, 非严格模式下, 2.4 == 2.4.0 , 开启严格模式, 则认为 2.4 < 2.4.0 , 因为 2.4 没有小版本号 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 17:07 2023/12/27 +func (v *version) Compare(versionOne string, versionTwo string, strictMode bool) (int, error) { + oneVersionArr, twoVersionArr, err := v.getVersionArr(versionOne, versionTwo) + if nil != err { + return define.VersionCompareFail, err + } + oneVersionLength := len(oneVersionArr) + twoVersionLength := len(twoVersionArr) + // 版本号长度不一致补位 + if oneVersionLength > twoVersionLength { + for fill := 0; fill < oneVersionLength-twoVersionLength; fill++ { + twoVersionArr = append(twoVersionArr, 0) + } + } + if oneVersionLength < twoVersionLength { + for fill := 0; fill < twoVersionLength-oneVersionLength; fill++ { + oneVersionArr = append(oneVersionArr, 0) + } + } + + // 开始比较 + for offset := 0; offset < len(oneVersionArr); offset++ { + if oneVersionArr[offset] == twoVersionArr[offset] { + // 当前位版本相等 + continue + } + if oneVersionArr[offset] > twoVersionArr[offset] { + // 第一个版本号大 + return define.VersionOneMax, nil + } else { + // 第二个版本号大 + return define.VersionTwoMax, nil + } + } + // 版本号一致, 判断严格模式与非严格模式 + if !strictMode || oneVersionLength == twoVersionLength { + return define.VersionEqual, nil + } + return wrapper.TernaryOperator.Int( + oneVersionLength > twoVersionLength, + wrapper.Int(define.VersionOneMax), + wrapper.Int(define.VersionTwoMax), + ).ToInt().Value, nil +} diff --git a/tool/version_test.go b/tool/version_test.go new file mode 100644 index 0000000..54ec3b9 --- /dev/null +++ b/tool/version_test.go @@ -0,0 +1,22 @@ +// Package tool ... +// +// Description : tool ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-12-27 17:51 +package tool + +import ( + "fmt" + "testing" +) + +func Test_version_Compare(t *testing.T) { + fmt.Println(Version.Compare("2.4.1", "2.4.1.0", true)) + fmt.Println(Version.Compare("2.4.1", "2.4.1.0", false)) + fmt.Println(Version.Compare("2.4.0", "2.4.1", true)) + fmt.Println(Version.Compare("2.4.2", "2.4.1", true)) + fmt.Println(Version.Compare("2.4", "2.4.1", true)) + fmt.Println(Version.Compare("2.4.2", "2.4", true)) +}