forked from project-chip/alchemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
463 lines (421 loc) · 12.1 KB
/
build.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package spec
import (
"context"
"fmt"
"log/slog"
"strings"
"github.com/project-chip/alchemy/asciidoc"
"github.com/project-chip/alchemy/internal/log"
"github.com/project-chip/alchemy/internal/pipeline"
"github.com/project-chip/alchemy/matter"
"github.com/project-chip/alchemy/matter/conformance"
"github.com/project-chip/alchemy/matter/types"
)
type Builder struct {
Spec *Specification
IgnoreHierarchy bool
}
func NewBuilder() Builder {
return Builder{}
}
func (sp Builder) Name() string {
return "Building spec"
}
func (sp Builder) Type() pipeline.ProcessorType {
return pipeline.ProcessorTypeCollective
}
func (sp *Builder) Process(cxt context.Context, inputs []*pipeline.Data[*Doc]) (outputs []*pipeline.Data[*Doc], err error) {
docs := make([]*Doc, 0, len(inputs))
for _, i := range inputs {
docs = append(docs, i.Content)
}
sp.Spec, err = sp.buildSpec(docs)
outputs = inputs
return
}
func (sp *Builder) buildSpec(docs []*Doc) (spec *Specification, err error) {
buildTree(docs)
spec = newSpec()
for _, d := range docs {
if len(d.parents) > 0 {
continue
}
dg := NewDocGroup(d.Path.Relative)
setSpec(d, spec, dg)
}
for _, d := range docs {
crossReferences := d.CrossReferences()
for id, xrefs := range crossReferences {
d.group.crossReferences[id] = append(d.group.crossReferences[id], xrefs...)
}
}
for _, d := range docs {
var anchors map[string][]*Anchor
anchors, err = d.Anchors()
if err != nil {
return
}
for id, anchor := range anchors {
d.group.anchors[id] = append(d.group.anchors[id], anchor...)
}
for id, anchor := range d.anchorsByLabel {
d.group.anchorsByLabel[id] = append(d.group.anchorsByLabel[id], anchor...)
}
}
var basicInformationCluster, bridgedBasicInformationCluster *matter.Cluster
for _, d := range docs {
slog.Debug("building spec", "path", d.Path)
dt, dterr := d.DocType()
if dterr == nil {
switch dt {
case matter.DocTypeBaseDeviceType:
spec.BaseDeviceType, err = d.toBaseDeviceType()
if err != nil {
return
}
case matter.DocTypeDataModel:
}
}
var entities []types.Entity
entities, err = d.Entities()
if err != nil {
slog.Warn("error building entities", "doc", d.Path, "error", err)
continue
}
for _, m := range entities {
switch m := m.(type) {
case *matter.ClusterGroup:
for _, c := range m.Clusters {
addClusterToSpec(spec, d, c)
}
case *matter.Cluster:
switch m.Name {
case "Basic Information":
basicInformationCluster = m
case "Bridged Device Basic Information":
bridgedBasicInformationCluster = m
}
addClusterToSpec(spec, d, m)
case *matter.DeviceType:
spec.DeviceTypes = append(spec.DeviceTypes, m)
case *matter.Namespace:
spec.Namespaces = append(spec.Namespaces, m)
default:
slog.Warn("unknown entity type", "path", d.Path, "type", fmt.Sprintf("%T", m))
}
switch m := m.(type) {
case *matter.ClusterGroup:
for _, c := range m.Clusters {
spec.DocRefs[c] = d
}
default:
spec.DocRefs[m] = d
}
}
err = addGlobalEntities(spec, d)
if err != nil {
slog.Warn("error building global objects", "doc", d.Path, "error", err)
continue
}
}
if !sp.IgnoreHierarchy {
resolveHierarchy(spec)
}
sp.resolveDataTypeReferences(spec)
err = updateBridgedBasicInformationCluster(basicInformationCluster, bridgedBasicInformationCluster)
if err != nil {
return
}
for _, c := range spec.ClustersByName {
if c.Features != nil {
spec.ClusterRefs.Add(c, c.Features)
}
for _, en := range c.Bitmaps {
spec.ClusterRefs.Add(c, en)
}
for _, en := range c.Enums {
spec.ClusterRefs.Add(c, en)
}
for _, en := range c.Structs {
spec.ClusterRefs.Add(c, en)
}
}
for _, dt := range spec.DeviceTypes {
for _, cr := range dt.ClusterRequirements {
if c, ok := spec.ClustersByID[cr.ClusterID.Value()]; ok {
cr.Cluster = c
} else {
slog.Warn("unknown cluster ID for cluster requirement on device type", "clusterId", cr.ClusterID.HexString(), "clusterName", cr.ClusterName, "deviceType", dt.Name)
}
}
for _, er := range dt.ElementRequirements {
if c, ok := spec.ClustersByID[er.ClusterID.Value()]; ok {
er.Cluster = c
} else {
slog.Warn("unknown cluster ID for element requirement on device type", "clusterId", er.ClusterID.HexString(), "clusterName", er.ClusterName, "deviceType", dt.Name)
}
}
}
return
}
func addClusterToSpec(spec *Specification, d *Doc, m *matter.Cluster) {
spec.Clusters[m] = struct{}{}
if m.ID.Valid() {
existing, ok := spec.ClustersByID[m.ID.Value()]
if ok {
slog.Warn("Duplicate cluster ID", slog.String("clusterId", m.ID.HexString()), slog.String("clusterName", m.Name), slog.String("existingClusterName", existing.Name))
}
spec.ClustersByID[m.ID.Value()] = m
}
existing, ok := spec.ClustersByName[m.Name]
if ok {
slog.Warn("Duplicate cluster Name", slog.String("clusterId", m.ID.HexString()), slog.String("clusterName", m.Name), slog.String("existingClusterId", existing.ID.HexString()))
}
spec.ClustersByName[m.Name] = m
for _, en := range m.Bitmaps {
_, ok := spec.bitmapIndex[en.Name]
if ok {
slog.Debug("multiple bitmaps with same name", "name", en.Name)
} else {
spec.bitmapIndex[en.Name] = en
}
spec.DocRefs[en] = d
spec.addEntityByName(en.Name, en, m)
}
for _, en := range m.Enums {
_, ok := spec.enumIndex[en.Name]
if ok {
slog.Debug("multiple enums with same name", "name", en.Name)
} else {
spec.enumIndex[en.Name] = en
}
spec.DocRefs[en] = d
spec.addEntityByName(en.Name, en, m)
}
for _, en := range m.Structs {
_, ok := spec.structIndex[en.Name]
if ok {
slog.Debug("multiple structs with same name", "name", en.Name)
} else {
spec.structIndex[en.Name] = en
}
spec.DocRefs[en] = d
spec.addEntityByName(en.Name, en, m)
}
for _, en := range m.TypeDefs {
_, ok := spec.typeDefIndex[en.Name]
if ok {
slog.Debug("multiple structs with same name", "name", en.Name)
} else {
spec.typeDefIndex[en.Name] = en
}
spec.DocRefs[en] = d
specIndex.addEntity(en.Name, en, m)
}
}
func (sp *Builder) resolveDataTypeReferences(spec *Specification) {
for _, s := range spec.structIndex {
for _, f := range s.Fields {
sp.resolveDataType(spec, nil, f, f.Type)
}
}
for _, cluster := range spec.ClustersByName {
for _, a := range cluster.Attributes {
sp.resolveDataType(spec, cluster, a, a.Type)
}
for _, s := range cluster.Structs {
for _, f := range s.Fields {
sp.resolveDataType(spec, cluster, f, f.Type)
}
}
for _, s := range cluster.Events {
for _, f := range s.Fields {
sp.resolveDataType(spec, cluster, f, f.Type)
}
}
for _, s := range cluster.Commands {
for _, f := range s.Fields {
sp.resolveDataType(spec, cluster, f, f.Type)
}
}
}
}
func (sp *Builder) resolveDataType(spec *Specification, cluster *matter.Cluster, field *matter.Field, dataType *types.DataType) {
if dataType == nil {
if !conformance.IsDeprecated(field.Conformance) && (cluster == nil || cluster.Hierarchy == "Base") {
var clusterName string
if cluster != nil {
clusterName = cluster.Name
}
if !sp.IgnoreHierarchy {
slog.Warn("missing type on field", log.Path("path", field), slog.String("id", field.ID.HexString()), slog.String("name", field.Name), slog.String("cluster", clusterName))
}
}
return
}
switch dataType.BaseType {
case types.BaseDataTypeTag:
getTagNamespace(spec, field)
case types.BaseDataTypeList:
sp.resolveDataType(spec, cluster, field, dataType.EntryType)
case types.BaseDataTypeCustom:
if dataType.Entity == nil {
dataType.Entity = getCustomDataType(spec, dataType.Name, cluster, field)
if dataType.Entity == nil {
slog.Error("unknown custom data type", slog.String("cluster", clusterName(cluster)), slog.String("field", field.Name), slog.String("type", dataType.Name), log.Path("source", field))
}
}
if cluster == nil || dataType.Entity == nil {
return
}
spec.ClusterRefs.Add(cluster, dataType.Entity)
s, ok := dataType.Entity.(*matter.Struct)
if !ok {
return
}
for _, f := range s.Fields {
sp.resolveDataType(spec, cluster, f, f.Type)
}
}
}
func getCustomDataType(spec *Specification, dataTypeName string, cluster *matter.Cluster, field *matter.Field) (e types.Entity) {
entities := spec.entities[dataTypeName]
if len(entities) == 0 {
canonicalName := CanonicalName(dataTypeName)
if canonicalName != dataTypeName {
e = getCustomDataType(spec, canonicalName, cluster, field)
} else {
e = getCustomDataTypeFromReference(spec, cluster, field)
}
} else if len(entities) == 1 {
for m := range entities {
e = m
}
} else {
e = disambiguateDataType(entities, cluster, field)
}
return
}
func getCustomDataTypeFromReference(spec *Specification, cluster *matter.Cluster, field *matter.Field) (e types.Entity) {
switch source := field.Type.Source.(type) {
case *asciidoc.CrossReference:
doc, ok := spec.DocRefs[cluster]
if !ok {
return
}
anchor := doc.FindAnchor(source.ID)
if anchor == nil {
return
}
switch el := anchor.Element.(type) {
case *asciidoc.Section:
entities := doc.entitiesBySection[el]
if len(entities) == 1 {
e = entities[0]
return
}
}
return nil
default:
return
}
}
func getTagNamespace(spec *Specification, field *matter.Field) {
for _, ns := range spec.Namespaces {
if strings.EqualFold(ns.Name, field.Type.Name) {
field.Type.Entity = ns
return
}
}
slog.Warn("failed to match tag name space", slog.String("name", field.Name), log.Path("field", field), slog.String("namespace", field.Type.Name))
}
func disambiguateDataType(entities map[types.Entity]*matter.Cluster, cluster *matter.Cluster, field *matter.Field) types.Entity {
// If there are multiple entities with the same name, prefer the one on the current cluster
for m, c := range entities {
if c == cluster {
return m
}
}
// OK, if the data type is defined on the direct parent of this cluster, take that one
if cluster.Parent != nil {
for m, c := range entities {
if c != nil && c == cluster.Parent {
return m
}
}
}
var nakedEntities []types.Entity
for m, c := range entities {
if c == nil {
nakedEntities = append(nakedEntities, m)
}
}
if len(nakedEntities) == 1 {
return nakedEntities[0]
}
// Can't disambiguate out this data model
slog.Warn("ambiguous data type", "cluster", clusterName(cluster), "field", field.Name, log.Path("source", field))
for m, c := range entities {
var clusterName string
if c != nil {
clusterName = c.Name
} else {
clusterName = "naked"
}
slog.Warn("ambiguous data type", "model", m.Source(), "cluster", clusterName)
}
return nil
}
func clusterName(cluster *matter.Cluster) string {
if cluster != nil {
return cluster.Name
}
return "none"
}
func resolveHierarchy(spec *Specification) {
for _, c := range spec.ClustersByID {
if c.Hierarchy == "Base" {
continue
}
base, ok := spec.ClustersByName[c.Hierarchy]
if !ok {
slog.Warn("Failed to find base cluster", "cluster", c.Name, "baseCluster", c.Hierarchy)
continue
}
linkedEntities, err := c.Inherit(base)
if err != nil {
slog.Warn("Failed to inherit from base cluster", "cluster", c.Name, "baseCluster", c.Hierarchy, "error", err)
}
// These entities were inherited from a base cluster, but not modified
for _, linkedEntity := range linkedEntities {
spec.ClusterRefs.Add(c, linkedEntity)
spec.addEntity(linkedEntity, c)
}
assignCustomDataTypes(c)
}
}
func updateBridgedBasicInformationCluster(basicInformationCluster *matter.Cluster, bridgedBasicInformationCluster *matter.Cluster) error {
if basicInformationCluster == nil {
return fmt.Errorf("missing Basic Information Cluster in spec")
}
if bridgedBasicInformationCluster == nil {
return fmt.Errorf("missing Basic Information Cluster in spec")
}
am := make(map[uint64]*matter.Field, len(basicInformationCluster.Attributes))
for _, a := range basicInformationCluster.Attributes {
am[a.ID.Value()] = a
}
for _, ba := range bridgedBasicInformationCluster.Attributes {
id := ba.ID.Value()
a, ok := am[id]
if !ok {
continue
}
ba.Type = a.Type.Clone()
ba.Constraint = a.Constraint.Clone()
ba.Quality = a.Quality
ba.Default = a.Default
ba.Access = a.Access
}
return nil
}