Skip to content

Commit b88e6ff

Browse files
committed
Merge branch 'feat/1.0.7/short-id' into test
2 parents 13733a2 + ecff4e0 commit b88e6ff

File tree

4 files changed

+9
-59
lines changed

4 files changed

+9
-59
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require (
2727
github.com/mojocn/base64Captcha v1.3.5
2828
github.com/ory/dockertest/v3 v3.9.1
2929
github.com/robfig/cron/v3 v3.0.1
30-
github.com/segmentfault/pacman v1.0.2
30+
github.com/segmentfault/pacman v1.0.3
3131
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0
3232
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05
3333
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20221219081300-f734f4a16aa0

go.sum

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

pkg/uid/sid.go

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,14 @@ package uid
22

33
import (
44
"strconv"
5+
6+
"github.com/segmentfault/pacman/utils"
57
)
68

79
const salt = int64(100)
810

911
var ShortIDSwitch = false
1012

11-
var AlphanumericSet = []rune{
12-
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
13-
'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',
14-
'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',
15-
}
16-
17-
var AlphanumericIndex map[rune]int
18-
19-
func init() {
20-
AlphanumericIndex = make(map[rune]int, len(AlphanumericSet))
21-
for i, ru := range AlphanumericSet {
22-
AlphanumericIndex[ru] = i
23-
}
24-
}
25-
26-
func EnToShortID(id int64) string {
27-
id = id + salt
28-
var code []rune
29-
for id > 0 {
30-
idx := id % int64(len(AlphanumericSet))
31-
code = append(code, AlphanumericSet[idx])
32-
id = id / int64(len(AlphanumericSet))
33-
}
34-
return string(code)
35-
}
36-
func DeToShortID(code string) int64 {
37-
var id int64
38-
runes := []rune(code)
39-
for i := len(runes) - 1; i >= 0; i-- {
40-
ru := runes[i]
41-
idx := AlphanumericIndex[ru]
42-
id = id*int64(len(AlphanumericSet)) + int64(idx)
43-
}
44-
id = id - salt
45-
return id
46-
}
47-
4813
// NumToString num to string
4914
func NumToShortID(id int64) string {
5015
sid := strconv.FormatInt(id, 10)
@@ -61,8 +26,8 @@ func NumToShortID(id int64) string {
6126
if err != nil {
6227
return ""
6328
}
64-
code := EnToShortID(id)
65-
tcode := EnToShortID(typeCode)
29+
code := utils.EnShortID(id, salt)
30+
tcode := utils.EnShortID(typeCode, salt)
6631
return string(tcode) + string(code)
6732
}
6833

@@ -74,8 +39,8 @@ func ShortIDToNum(code string) int64 {
7439
scodeType := code[0:2]
7540
code = code[2:int32(len(code))]
7641

77-
id := DeToShortID(code)
78-
codeType := DeToShortID(scodeType)
42+
id := utils.DeShortID(code, salt)
43+
codeType := utils.DeShortID(scodeType, salt)
7944
return 10000000000000000 + codeType*10000000000000 + id
8045
}
8146

pkg/uid/sid_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,6 @@ func Test_ShortID(t *testing.T) {
2727
}
2828
}
2929

30-
func Test_ShortIDBase(t *testing.T) {
31-
nums := []int64{
32-
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
33-
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
34-
100, 200, 300, 400, 500, 600, 700, 800, 900, 999, 1000,
35-
2000, 3000,
36-
3100, 3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 3999, 4000,
37-
}
38-
for _, num := range nums {
39-
code := EnToShortID(num)
40-
denum := DeToShortID(code)
41-
fmt.Println(num, code, denum)
42-
}
43-
}
44-
4530
func Test_EnDeShortID(t *testing.T) {
4631
nums := []string{"0", "1", "10", "100", "1000", "10000", "100000", "1234567", "10000000000000000", "10010000000001316", "19930000000001316"}
4732
ShortIDSwitch = true

0 commit comments

Comments
 (0)