Merge branch 'feat/1.0.7/short-id' into test

This commit is contained in:
aichy126 2023-03-20 11:02:20 +08:00
commit b88e6ff5c7
4 changed files with 9 additions and 59 deletions

2
go.mod
View File

@ -27,7 +27,7 @@ require (
github.com/mojocn/base64Captcha v1.3.5
github.com/ory/dockertest/v3 v3.9.1
github.com/robfig/cron/v3 v3.0.1
github.com/segmentfault/pacman v1.0.2
github.com/segmentfault/pacman v1.0.3
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221219081300-f734f4a16aa0

4
go.sum
View File

@ -607,8 +607,8 @@ github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
github.com/segmentfault/pacman v1.0.2 h1:tXWkEzePiSVQXYwFH3tOuxC1/DJ5ISi35F93lKNGs3o=
github.com/segmentfault/pacman v1.0.2/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
github.com/segmentfault/pacman v1.0.3 h1:/K8LJHQMiCaCIvC/e8GQITpYTEG6RH4KTLTZjPTghl4=
github.com/segmentfault/pacman v1.0.3/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0 h1:4x0qG7H2M3qH7Yo2BhGrVlji1iTmRAWgINY/JyENeHs=
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05 h1:BlqTgc3/MYKG6vMI2MI+6o+7P4Gy5PXlawu185wPXAk=

View File

@ -2,49 +2,14 @@ package uid
import (
"strconv"
"github.com/segmentfault/pacman/utils"
)
const salt = int64(100)
var ShortIDSwitch = false
var AlphanumericSet = []rune{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
}
var AlphanumericIndex map[rune]int
func init() {
AlphanumericIndex = make(map[rune]int, len(AlphanumericSet))
for i, ru := range AlphanumericSet {
AlphanumericIndex[ru] = i
}
}
func EnToShortID(id int64) string {
id = id + salt
var code []rune
for id > 0 {
idx := id % int64(len(AlphanumericSet))
code = append(code, AlphanumericSet[idx])
id = id / int64(len(AlphanumericSet))
}
return string(code)
}
func DeToShortID(code string) int64 {
var id int64
runes := []rune(code)
for i := len(runes) - 1; i >= 0; i-- {
ru := runes[i]
idx := AlphanumericIndex[ru]
id = id*int64(len(AlphanumericSet)) + int64(idx)
}
id = id - salt
return id
}
// NumToString num to string
func NumToShortID(id int64) string {
sid := strconv.FormatInt(id, 10)
@ -61,8 +26,8 @@ func NumToShortID(id int64) string {
if err != nil {
return ""
}
code := EnToShortID(id)
tcode := EnToShortID(typeCode)
code := utils.EnShortID(id, salt)
tcode := utils.EnShortID(typeCode, salt)
return string(tcode) + string(code)
}
@ -74,8 +39,8 @@ func ShortIDToNum(code string) int64 {
scodeType := code[0:2]
code = code[2:int32(len(code))]
id := DeToShortID(code)
codeType := DeToShortID(scodeType)
id := utils.DeShortID(code, salt)
codeType := utils.DeShortID(scodeType, salt)
return 10000000000000000 + codeType*10000000000000 + id
}

View File

@ -27,21 +27,6 @@ func Test_ShortID(t *testing.T) {
}
}
func Test_ShortIDBase(t *testing.T) {
nums := []int64{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
100, 200, 300, 400, 500, 600, 700, 800, 900, 999, 1000,
2000, 3000,
3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 3999, 4000,
}
for _, num := range nums {
code := EnToShortID(num)
denum := DeToShortID(code)
fmt.Println(num, code, denum)
}
}
func Test_EnDeShortID(t *testing.T) {
nums := []string{"0", "1", "10", "100", "1000", "10000", "100000", "1234567", "10000000000000000", "10010000000001316", "19930000000001316"}
ShortIDSwitch = true