-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.go
96 lines (77 loc) · 1.59 KB
/
common.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cluster
import (
"fmt"
"gopkg.in/yaml.v3"
)
const (
None Side = iota
Server Side = 1 << iota
Client Side = 1 << iota
ClientAndServer Side = Server | Client
)
var _ yaml.Unmarshaler = (*Side)(nil)
type Side uint8
type Provider interface {
Clusters() Clusters
}
type Cluster interface {
ID() ID
CAttrType() string
CVarName() string
// ReportAttrCount returns how many attributes are reportable
ReportAttrCount() int
// Side tells if the cluster is client and/or server. ZCL 1.3.
Side() Side
}
type Clusters []Cluster
func (s Side) String() string {
switch s {
case Client:
return "ZB_ZCL_CLUSTER_CLIENT_ROLE"
case Server:
return "ZB_ZCL_CLUSTER_SERVER_ROLE"
default:
return "<unsupported>" // Idea is that build will break on invalid value.
}
}
func (s Side) IsClient() bool {
return s == Client
}
func (s Side) IsServer() bool {
return s == Server
}
func (s *Side) UnmarshalYAML(node *yaml.Node) error {
switch node.Value {
case "server":
*s = Server
case "client":
*s = Client
case "client_and_server":
*s = ClientAndServer
default:
return fmt.Errorf("unknown side value: %q", node.Value)
}
return nil
}
func (c Clusters) ReportAttrCount() (count int) {
for _, cluster := range c {
count += cluster.ReportAttrCount()
}
return count
}
func (c Clusters) Servers() (count int) {
for _, cluster := range c {
if cluster.Side()&Server == Server {
count++
}
}
return count
}
func (c Clusters) Clients() (count int) {
for _, cluster := range c {
if cluster.Side()&Client == Client {
count++
}
}
return count
}