-
Notifications
You must be signed in to change notification settings - Fork 10
/
support.go
executable file
·67 lines (54 loc) · 1.56 KB
/
support.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
package model
import (
"encoding/json"
p "github.com/fagongzi/fastim/pkg/protocol"
)
type Support struct {
Addr string `json:"addr,omitempty"`
Product int `json:"product,omitempty"`
CmdList []int `json:"cmdList,omitempty"`
BizList []int `json:"bizList,omitempty"`
MinProtocol int `json:"minProtocol,omitempty"`
MaxProtocol int `json:"maxProtocol,omitempty"`
}
func UnMarshalSupport(data []byte) *Support {
v := &Support{}
json.Unmarshal(data, v)
return v
}
func (self *Support) Marshal() []byte {
v, _ := json.Marshal(self)
return v
}
func (self *Support) Mathces(msg *p.Message) bool {
return self.productMatches(msg) &&
self.protocolMatches(msg) &&
self.bizMatches(msg) &&
self.cmdMatches(msg)
}
func (self *Support) productMatches(msg *p.Message) bool {
return int(msg.GetProduct()) == self.Product
}
func (self *Support) bizMatches(msg *p.Message) bool {
biz := int(msg.GetBiz())
return self.BizList == nil || inArray(self.BizList, biz)
}
func (self *Support) cmdMatches(msg *p.Message) bool {
cmd := int(msg.GetCmd())
return self.CmdList == nil || inArray(self.CmdList, cmd)
}
func (self *Support) protocolMatches(msg *p.Message) bool {
pv := int(msg.GetProduct())
return pv >= self.MinProtocol && pv <= self.MaxProtocol
}
func (self *Support) SupportBiz(biz int) bool {
return self.BizList == nil || inArray(self.BizList, biz)
}
func inArray(arr []int, target int) bool {
for _, v := range arr {
if v == target {
return true
}
}
return false
}