forked from project-chip/alchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.go
272 lines (244 loc) · 6.25 KB
/
cluster.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package matter
import (
"log/slog"
"github.com/project-chip/alchemy/asciidoc"
"github.com/project-chip/alchemy/internal/log"
"github.com/project-chip/alchemy/matter/conformance"
"github.com/project-chip/alchemy/matter/types"
)
type ClusterGroup struct {
entity
Name string `json:"name,omitempty"`
Clusters []*Cluster `json:"clusters"`
AssociatedDataTypes
}
func NewClusterGroup(name string, source asciidoc.Element, clusters []*Cluster) *ClusterGroup {
return &ClusterGroup{
Name: name,
entity: entity{source: source},
Clusters: clusters,
}
}
func (c ClusterGroup) EntityType() types.EntityType {
return types.EntityTypeClusterGroup
}
func (c ClusterGroup) Explode() []*Cluster {
return c.Clusters
}
func (c *ClusterGroup) AddBitmaps(bitmaps ...*Bitmap) {
for _, bm := range bitmaps {
if bm.ParentEntity != nil {
if _, ok := bm.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Bitmap belongs to multiple clusters", slog.String("name", bm.Name), log.Path("source", bm))
}
continue
}
bm.ParentEntity = c
}
c.Bitmaps = append(c.Bitmaps, bitmaps...)
}
func (c *ClusterGroup) AddEnums(enums ...*Enum) {
for _, e := range enums {
if e.ParentEntity != nil {
if _, ok := e.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Enum belongs to multiple clusters", slog.String("name", e.Name), log.Path("source", e))
}
continue
}
e.ParentEntity = c
}
c.Enums = append(c.Enums, enums...)
}
func (c *ClusterGroup) AddStructs(structs ...*Struct) {
for _, s := range structs {
if s.ParentEntity != nil {
if _, ok := s.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Struct belongs to multiple clusters", slog.String("name", s.Name), log.Path("source", s))
}
continue
}
s.ParentEntity = c
}
c.Structs = append(c.Structs, structs...)
}
type Cluster struct {
entity
ID *Number `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Revisions []*Revision `json:"revisions,omitempty"`
Parent *Cluster `json:"-"`
Conformance conformance.Set `json:"conformance,omitempty"`
Hierarchy string `json:"hierarchy,omitempty"`
Role string `json:"role,omitempty"`
Scope string `json:"scope,omitempty"`
PICS string `json:"pics,omitempty"`
Features *Features `json:"features,omitempty"`
AssociatedDataTypes
TypeDefs TypeDefSet `json:"typedefs,omitempty"`
Attributes FieldSet `json:"attributes,omitempty"`
Events EventSet `json:"events,omitempty"`
Commands CommandSet `json:"commands,omitempty"`
}
func NewCluster(source asciidoc.Element) *Cluster {
return &Cluster{
entity: entity{source: source},
}
}
func (c *Cluster) EntityType() types.EntityType {
return types.EntityTypeCluster
}
func (c *Cluster) Inherit(parent *Cluster) (linkedEntities []types.Entity, err error) {
c.Parent = parent
if parent.Features != nil {
if c.Features == nil || len(c.Features.Bits) == 0 {
c.Features = parent.Features.Clone()
} else {
err = c.Features.Inherit(&parent.Features.Bitmap)
if err != nil {
return
}
}
}
if len(c.Description) == 0 {
c.Description = parent.Description
}
c.Attributes = c.Attributes.Inherit(parent.Attributes)
for _, pbm := range parent.Bitmaps {
var matching *Bitmap
for _, b := range c.Bitmaps {
if b.Name == pbm.Name {
matching = b
break
}
}
if matching == nil {
c.Bitmaps = append(c.Bitmaps, pbm)
linkedEntities = append(linkedEntities, pbm)
continue
}
err = matching.Inherit(pbm)
if err != nil {
return
}
}
for _, pe := range parent.Enums {
var matching *Enum
for _, en := range c.Enums {
if en.Name == pe.Name {
matching = en
break
}
}
if matching == nil {
c.Enums = append(c.Enums, pe)
linkedEntities = append(linkedEntities, pe)
continue
}
err = matching.Inherit(pe)
if err != nil {
return
}
}
for _, ps := range parent.Structs {
var matching *Struct
for _, s := range c.Structs {
if s.Name == ps.Name {
matching = s
break
}
}
if matching == nil {
c.Structs = append(c.Structs, ps)
linkedEntities = append(linkedEntities, ps)
continue
}
matching.Inherit(ps)
}
for _, pe := range parent.Events {
var matching *Event
for _, e := range c.Events {
if e.ID.Equals(pe.ID) {
matching = e
break
}
}
if matching == nil {
c.Events = append(c.Events, pe.Clone())
continue
}
matching.Inherit(pe)
}
for _, pc := range parent.Commands {
var matching *Command
for _, c := range c.Commands {
if c.ID.Equals(pc.ID) {
matching = c
break
}
}
if matching == nil {
c.Commands = append(c.Commands, pc.Clone())
continue
}
matching.Inherit(pc)
}
return
}
func (c *Cluster) Identifier(name string) (types.Entity, bool) {
if c == nil {
return nil, false
}
var cr types.Entity
var ok bool
if c.Features != nil {
cr, ok = c.Features.Identifier(name)
if ok {
return cr, ok
}
}
stores := []conformance.IdentifierStore{c.Attributes, c.Commands, c.Events, c.Enums, c.Bitmaps, c.Structs}
for _, s := range stores {
cr, ok = s.Identifier(name)
if ok {
return cr, true
}
}
return nil, false
}
func (c *Cluster) AddBitmaps(bitmaps ...*Bitmap) {
for _, bm := range bitmaps {
if bm.ParentEntity != nil {
if _, ok := bm.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Bitmap belongs to multiple clusters", slog.String("name", bm.Name), log.Path("source", bm), slog.String("cluster", c.Name))
}
continue
}
bm.ParentEntity = c
}
c.Bitmaps = append(c.Bitmaps, bitmaps...)
}
func (c *Cluster) AddEnums(enums ...*Enum) {
for _, e := range enums {
if e.ParentEntity != nil {
if _, ok := e.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Enum belongs to multiple clusters", slog.String("name", e.Name), log.Path("source", e))
}
continue
}
e.ParentEntity = c
}
c.Enums = append(c.Enums, enums...)
}
func (c *Cluster) AddStructs(structs ...*Struct) {
for _, s := range structs {
if s.ParentEntity != nil {
if _, ok := s.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Struct belongs to multiple clusters", slog.String("name", s.Name), log.Path("source", s), slog.String("cluster", c.Name))
}
continue
}
s.ParentEntity = c
}
c.Structs = append(c.Structs, structs...)
}