-
Notifications
You must be signed in to change notification settings - Fork 1
/
error_code.go
68 lines (57 loc) · 1.17 KB
/
error_code.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package errors
import (
"crypto/md5"
"encoding/binary"
"strconv"
"strings"
)
var (
replace = strings.NewReplacer(
" ", "0",
"O", "0",
"I", "1",
)
hashFunc = hash
)
// ErrorToCode 用 error 生成一个字符串 code
func ErrorToCode(err error) (code string) {
if err == nil {
return ""
}
_, code = genCode(err.Error())
return code
}
// HashFunc 表示用于生成 error code 的哈希函数,返回的数值不得大于 0xFFFFF
type HashFunc func(string) uint64
// SetHashFunc 设置哈希函数
func SetHashFunc(f HashFunc) {
if f != nil {
hashFunc = f
}
}
func genCode(s string) (uint64, string) {
u64 := hashFunc(s) & 0xFFFFF
codeStr := encode(u64)
u64, _ = decode(codeStr)
return u64, codeStr
}
func hash(s string) uint64 {
h := md5.Sum([]byte(s))
u := binary.BigEndian.Uint32(h[0:16])
return uint64(u)
}
func encode(code uint64) string {
s := strconv.FormatUint(code, 36)
s = strings.ToUpper(s)
return replace.Replace(s)
}
func decode(s string) (uint64, bool) {
if len(s) != 4 {
return 0, false
}
s = strings.Replace(s, "l", "1", -1)
s = strings.ToUpper(s)
s = replace.Replace(s)
code, _ := strconv.ParseUint(s, 36, 64)
return code, code > 0
}