Skip to content

Commit

Permalink
Some DRYing up of code base
Browse files Browse the repository at this point in the history
  • Loading branch information
hasty committed Oct 1, 2024
1 parent 2cff917 commit 2113e6f
Show file tree
Hide file tree
Showing 12 changed files with 878 additions and 790 deletions.
2 changes: 1 addition & 1 deletion disco/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (b *Ball) fixConstraintCells(section *spec.Section, ti *spec.TableInfo) (er
continue
}

dataType, e := b.doc.ReadRowDataType(row, ti.ColumnMap, matter.TableColumnType)
dataType, e := ti.ReadDataType(row, matter.TableColumnType)
if e != nil {
slog.Debug("error reading data type for constraint", slog.String("path", b.doc.Path.String()), slog.Any("error", e))
continue
Expand Down
90 changes: 6 additions & 84 deletions matter/cluster.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
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"
)
Expand All @@ -17,11 +14,13 @@ type ClusterGroup struct {
}

func NewClusterGroup(name string, source asciidoc.Element, clusters []*Cluster) *ClusterGroup {
return &ClusterGroup{
cg := &ClusterGroup{
Name: name,
entity: entity{source: source},
Clusters: clusters,
}
cg.AssociatedDataTypes.parentEntity = cg
return cg
}

func (c ClusterGroup) EntityType() types.EntityType {
Expand All @@ -32,45 +31,6 @@ 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"`
Expand All @@ -87,16 +47,17 @@ type Cluster struct {

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{
c := &Cluster{
entity: entity{source: source},
}
c.AssociatedDataTypes.parentEntity = c
return c
}

func (c *Cluster) EntityType() types.EntityType {
Expand Down Expand Up @@ -231,42 +192,3 @@ func (c *Cluster) Identifier(name string) (types.Entity, bool) {
}
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...)
}
65 changes: 62 additions & 3 deletions matter/datatypes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package matter

import (
"log/slog"
"strings"

"github.com/project-chip/alchemy/internal/log"
"github.com/project-chip/alchemy/matter/types"
)

type DataTypeCategory uint8
Expand Down Expand Up @@ -54,7 +58,62 @@ func StripTypeSuffixes(dataType string) string {
}

type AssociatedDataTypes struct {
Bitmaps BitmapSet `json:"bitmaps,omitempty"`
Enums EnumSet `json:"enums,omitempty"`
Structs StructSet `json:"structs,omitempty"`
parentEntity types.Entity

Bitmaps BitmapSet `json:"bitmaps,omitempty"`
Enums EnumSet `json:"enums,omitempty"`
Structs StructSet `json:"structs,omitempty"`
TypeDefs TypeDefSet `json:"typedefs,omitempty"`
}

func (adt *AssociatedDataTypes) AddBitmaps(bitmaps ...*Bitmap) {
for _, bm := range bitmaps {
if bm.ParentEntity != nil {
if _, ok := bm.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Bitmap belongs to multiple parents", slog.String("name", bm.Name), log.Path("source", bm), LogEntity(adt.parentEntity))
}
continue
}
bm.ParentEntity = adt.parentEntity
}
adt.Bitmaps = append(adt.Bitmaps, bitmaps...)
}

func (adt *AssociatedDataTypes) AddEnums(enums ...*Enum) {
for _, e := range enums {
if e.ParentEntity != nil {
if _, ok := e.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Enum belongs to multiple parents", slog.String("name", e.Name), log.Path("source", e), LogEntity(adt.parentEntity))
}
continue
}
e.ParentEntity = adt.parentEntity
}
adt.Enums = append(adt.Enums, enums...)
}

func (adt *AssociatedDataTypes) AddStructs(structs ...*Struct) {
for _, s := range structs {
if s.ParentEntity != nil {
if _, ok := s.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("Struct belongs to multiple parents", slog.String("name", s.Name), log.Path("source", s), LogEntity(adt.parentEntity))
}
continue
}
s.ParentEntity = adt.parentEntity
}
adt.Structs = append(adt.Structs, structs...)
}

func (adt *AssociatedDataTypes) AddTypeDefs(typeDefs ...*TypeDef) {
for _, td := range typeDefs {
if td.ParentEntity != nil {
if _, ok := td.ParentEntity.(*ClusterGroup); !ok {
slog.Warn("TypeDef belongs to multiple parents", slog.String("name", td.Name), log.Path("source", td), LogEntity(adt.parentEntity))
}
continue
}
td.ParentEntity = adt.parentEntity
}
adt.TypeDefs = append(adt.TypeDefs, typeDefs...)
}
2 changes: 1 addition & 1 deletion matter/spec/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (s *Section) toAttributes(d *Doc, cluster *matter.Cluster, entityMap map[as
}
attr.Name = matter.StripTypeSuffixes(attr.Name)
attr.Conformance = ti.ReadConformance(row, matter.TableColumnConformance)
attr.Type, err = d.ReadRowDataType(row, ti.ColumnMap, matter.TableColumnType)
attr.Type, err = ti.ReadDataType(row, matter.TableColumnType)
if err != nil {
if cluster.Hierarchy == "Base" && !conformance.IsDeprecated(attr.Conformance) && !conformance.IsDisallowed(attr.Conformance) {
// Clusters inheriting from other clusters don't supply type information, nor do attributes that are deprecated or disallowed
Expand Down
Loading

0 comments on commit 2113e6f

Please sign in to comment.