增加版本号比较方法
This commit is contained in:
parent
445c6407db
commit
dceb0bf682
15
define/version.go
Normal file
15
define/version.go
Normal file
@ -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 // 第二个版本号大
|
||||||
|
)
|
11
go.mod
11
go.mod
@ -7,3 +7,14 @@ require (
|
|||||||
github.com/mozillazg/go-pinyin v0.20.0
|
github.com/mozillazg/go-pinyin v0.20.0
|
||||||
github.com/spaolacci/murmur3 v1.1.0
|
github.com/spaolacci/murmur3 v1.1.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b // indirect
|
||||||
|
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71 // indirect
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d // indirect
|
||||||
|
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000 // indirect
|
||||||
|
github.com/BurntSushi/toml v1.3.2 // indirect
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
||||||
|
github.com/go-ini/ini v1.67.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
)
|
||||||
|
17
go.sum
17
go.sum
@ -1,6 +1,23 @@
|
|||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b h1:C7KftnLh7dOqzNRs5dn/9yqMDvuqMn5RCglvV6bY758=
|
||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||||
|
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71 h1:nvVSH+Ju8EmoPiPHTae5lxHo4kDjROYChs19Yayz+NY=
|
||||||
|
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20231224145141-489e31b07a71/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d h1:TV0BCQQewBEtLsv3i9gXkxLFd5A5bWBTiNd3D/I5o4Q=
|
||||||
|
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20231224125439-01f39b6ea08d/go.mod h1:w7kG4zyTJ1uPFaTWhze+OQuaUBINT2XnDxpyiM6ctc0=
|
||||||
|
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000 h1:ulssHpUMNoxws/fdJD+bhw8tuMPXp+jB54WwLdJNTsU=
|
||||||
|
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20231224145327-d9aed3d80000/go.mod h1:rt9wlUf/Y6lpZBXb0KK8n0JxEbI1J5JB73XgQmGHJqw=
|
||||||
|
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
||||||
|
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||||
|
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||||
|
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||||
|
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||||
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||||
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
|
||||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
84
version.go
Normal file
84
version.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
// Package util ...
|
||||||
|
//
|
||||||
|
// Description : util ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2023-12-27 17:06
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.zhangdeman.cn/zhangdeman/util/define"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user