Compare commits
2 Commits
d9aed3d800
...
d7e591277a
Author | SHA1 | Date | |
---|---|---|---|
d7e591277a | |||
88d3066307 |
15
tool/define/version.go
Normal file
15
tool/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 // 第二个版本号大
|
||||
)
|
88
tool/version.go
Normal file
88
tool/version.go
Normal file
@ -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
|
||||
}
|
22
tool/version_test.go
Normal file
22
tool/version_test.go
Normal file
@ -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))
|
||||
}
|
Loading…
Reference in New Issue
Block a user