A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
update short id
aichy1264d146807fff8
diff --git a/pkg/uid/sid.go b/pkg/uid/sid.goindex edcf27be..8b15afec 100644--- a/pkg/uid/sid.go+++ b/pkg/uid/sid.go@@ -4,7 +4,7 @@ import ("strconv")-const salt = int64(0)+const salt = int64(100)var ShortIDSwitch = false@@ -23,6 +23,28 @@ func init() {}}+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 stringfunc NumToShortID(id int64) string {sid := strconv.FormatInt(id, 10)@@ -39,21 +61,8 @@ func NumToShortID(id int64) string {if err != nil {return ""}- id = id + salt- // fmt.Println("[EN1]", typeCode, id)- var code []rune- var tcode []rune- for id > 0 {- idx := id % int64(len(AlphanumericSet))- code = append(code, AlphanumericSet[idx])- id = id / int64(len(AlphanumericSet))- }- for typeCode > 0 {- idx := typeCode % int64(len(AlphanumericSet))- tcode = append(tcode, AlphanumericSet[idx])- typeCode = typeCode / int64(len(AlphanumericSet))- }- // fmt.Println("[EN2]", string(tcode), string(code))+ code := EnToShortID(id)+ tcode := EnToShortID(typeCode)return string(tcode) + string(code)}@@ -62,26 +71,11 @@ func ShortIDToNum(code string) int64 {if len(code) < 2 {return 0}- scodeType := code[0:1]- code = code[1:int32(len(code))]- // fmt.Println("[DE1]", scodeType, code)- var id, codeType int64- runes := []rune(code)- codeRunes := []rune(scodeType)-- for i := len(runes) - 1; i >= 0; i-- {- ru := runes[i]- idx := AlphanumericIndex[ru]- id = id*int64(len(AlphanumericSet)) + int64(idx)- }- for i := len(codeRunes) - 1; i >= 0; i-- {- ru := codeRunes[i]- idx := AlphanumericIndex[ru]- codeType = codeType*int64(len(AlphanumericSet)) + int64(idx)- }- id = id - salt- // fmt.Println("[DE2]", codeType, id)+ scodeType := code[0:2]+ code = code[2:int32(len(code))]+ id := DeToShortID(code)+ codeType := DeToShortID(scodeType)return 10000000000000000 + codeType*10000000000000 + id}diff --git a/pkg/uid/sid_test.go b/pkg/uid/sid_test.goindex d7086c87..3b50efec 100644--- a/pkg/uid/sid_test.go+++ b/pkg/uid/sid_test.go@@ -8,6 +8,7 @@ import (func Test_ShortID(t *testing.T) {nums := []int64{+ 10010000000000001, 10010000000000002, 10010000000000003, 10010000000000004,10030000000000689, 10010000000000676, 10020000000000658, 10020000000000654,10030000009000689, 10010000009000676, 10020000009000658, 10020000009999999,10030000090000689, 10010000090000676, 10020000090000658, 10020000099999999,@@ -17,6 +18,7 @@ func Test_ShortID(t *testing.T) {10030900000000689, 10010900000000676, 10020900000000658, 10020999999999999,10039000000000689, 10019000000000676, 10029000000000658, 10029999999999999,10610000000000689, 10610000000000676, 10610000000000658, 10610000000000654,+ 19990000000000689, 19990000000000676, 19990000000000658, 19990000000000654,}for _, num := range nums {code := NumToShortID(num)@@ -25,8 +27,24 @@ 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", "10030000000001316", "99999999999999999", "999999999999999999", "1999999999999999999"}+ nums := []string{"0", "1", "10", "100", "1000", "10000", "100000", "1234567", "10000000000000000", "10010000000001316", "19930000000001316"}+ ShortIDSwitch = truefor _, num := range nums {code := EnShortID(num)denum := DeShortID(code)