error code optimization

This commit is contained in:
withchao
2023-06-29 19:43:40 +08:00
committed by Xinwei Xiong(cubxxw-openim)
parent ce7ae6a9b9
commit e1f5b2afe0
4 changed files with 311 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
package errs
var Relation = &relation{m: make(map[int]map[int]struct{})}
func init() {
Relation.Add(RecordNotFoundError, UserIDNotFoundError)
Relation.Add(RecordNotFoundError, GroupIDNotFoundError)
Relation.Add(DuplicateKeyError, UserIDExisted)
Relation.Add(DuplicateKeyError, GroupIDExisted)
}
type relation struct {
m map[int]map[int]struct{}
}
func (r *relation) Add(codes ...int) {
if len(codes) < 2 {
panic("codes length must be greater than 2")
}
for i := 1; i < len(codes); i++ {
parent := codes[i-1]
s, ok := r.m[parent]
if !ok {
s = make(map[int]struct{})
r.m[parent] = s
}
for _, code := range codes[i:] {
s[code] = struct{}{}
}
}
}
func (r *relation) Is(parent, child int) bool {
if parent == child {
return true
}
s, ok := r.m[parent]
if !ok {
return false
}
_, ok = s[child]
return ok
}