Skip to content

Commit

Permalink
Heavy rework of constraint logic to allow for referenced entities
Browse files Browse the repository at this point in the history
  • Loading branch information
hasty committed Jan 27, 2025
1 parent 9cb6c16 commit 585e1dc
Show file tree
Hide file tree
Showing 43 changed files with 3,016 additions and 953 deletions.
30 changes: 15 additions & 15 deletions compare/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/project-chip/alchemy/matter"
"github.com/project-chip/alchemy/matter/conformance"
"github.com/project-chip/alchemy/matter/constraint"
"github.com/project-chip/alchemy/matter/types"
"github.com/project-chip/alchemy/zap"
)
Expand Down Expand Up @@ -61,28 +62,27 @@ func compareField(entityType types.EntityType, specFields matter.FieldSet, specF
diffs = append(diffs, compareAccess(entityType, specField.Access, zapField.Access)...)
defaultValue := zap.GetFallbackValue(&matter.ConstraintContext{Field: specField, Fields: specFields})
if defaultValue.Defined() {
specDefault := defaultValue.ZapString(specField.Type)
if specDefault != zapField.Fallback && !(specField.Fallback == "null" && len(zapField.Fallback) == 0) { // ZAP frequently omits default null
specDefaultVal := matter.ParseNumber(specDefault)
zapDefault := matter.ParseNumber(zapField.Fallback)
if !specDefaultVal.Equals(zapDefault) {
diffs = append(diffs, &StringDiff{Type: DiffTypeMismatch, Property: DiffPropertyDefault, Spec: specDefault, ZAP: zapField.Fallback})
specDefault := constraint.ParseLimit(defaultValue.ZapString(specField.Type))
if !specDefault.Equal(zapField.Fallback) && !(constraint.IsNullLimit(specField.Fallback) && constraint.IsBlankLimit(zapField.Fallback)) { // ZAP frequently omits default null
specFallback := specField.Fallback.Fallback(&matter.ConstraintContext{Fields: specFields, Field: specField})
zapFallback := zapField.Fallback.Fallback(&matter.ConstraintContext{Fields: zapFields, Field: zapField})
if !specFallback.ValueEquals(zapFallback) {
diffs = append(diffs, &StringDiff{Type: DiffTypeMismatch, Property: DiffPropertyDefault, Spec: specFallback.ZapString(specField.Type), ZAP: zapFallback.ZapString(zapField.Type)})
}
}

} else if len(zapField.Fallback) > 0 {
} else if !constraint.IsBlankLimit(zapField.Fallback) {

if len(specField.Fallback) > 0 {
z := matter.ParseNumber(zapField.Fallback)
if specField.Fallback != "null" || !z.Equals(matter.NewNumber(specField.Type.NullValue())) {
s := matter.ParseNumber(specField.Fallback)
if !z.Equals(s) {
diffs = append(diffs, &StringDiff{Type: DiffTypeMismatch, Property: DiffPropertyDefault, Spec: specField.Fallback, ZAP: zapField.Fallback})
if !constraint.IsBlankLimit(specField.Fallback) {
zapFallback := zapField.Fallback.Fallback(&matter.ConstraintContext{Fields: zapFields, Field: zapField})
if !constraint.IsNullLimit(specField.Fallback) || !zapFallback.IsNull() {
specFallback := specField.Fallback.Fallback(&matter.ConstraintContext{Fields: specFields, Field: specField})
if !specFallback.ValueEquals(zapFallback) {
diffs = append(diffs, &StringDiff{Type: DiffTypeMismatch, Property: DiffPropertyDefault, Spec: specFallback.ZapString(specField.Type), ZAP: zapFallback.ZapString(zapField.Type)})
}
}
} else {
z := matter.ParseNumber(zapField.Fallback)
if !z.Valid() || z.Value() != 0 {
if !constraint.IsBlankLimit(zapField.Fallback) {
diffs = append(diffs, newMissingDiff(specField.Name, DiffPropertyDefault, entityType, SourceSpec))
}
}
Expand Down
13 changes: 9 additions & 4 deletions disco/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func (b *Baller) fixConstraintCells(cxt *discoContext, section *spec.Section, ti
if dataType == nil {
continue
}
c, e := constraint.ParseString(vc)
if e != nil {

c := constraint.ParseString(vc)
if constraint.IsGeneric(c) {
continue
}
var quality matter.Quality
Expand Down Expand Up @@ -70,12 +71,16 @@ func (cc *constraintContext) DataType() *types.DataType {
return cc.dataType
}

func (cc *constraintContext) ReferenceConstraint(ref string) constraint.Constraint {
func (cc *constraintContext) IdentifierConstraint(entity types.Entity, field constraint.Limit) constraint.Constraint {
return nil
}

func (cc *constraintContext) ReferenceConstraint(entity types.Entity, field constraint.Limit) constraint.Constraint {

return nil
}

func (cc *constraintContext) Fallback(name string) (def types.DataTypeExtreme) {
func (cc *constraintContext) Fallback(entity types.Entity, field constraint.Limit) (def types.DataTypeExtreme) {

return
}
Expand Down
6 changes: 3 additions & 3 deletions dm/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/beevik/etree"
"github.com/project-chip/alchemy/matter"
"github.com/project-chip/alchemy/matter/conformance"
"github.com/project-chip/alchemy/matter/constraint"
"github.com/project-chip/alchemy/matter/spec"
)

Expand All @@ -29,8 +30,8 @@ func renderAttributes(doc *spec.Doc, cluster *matter.Cluster, c *etree.Element)
ax.CreateAttr("id", a.ID.HexString())
ax.CreateAttr("name", a.Name)
renderDataType(a, ax)
if len(a.Fallback) > 0 {
ax.CreateAttr("default", a.Fallback)
if !constraint.IsBlankLimit(a.Fallback) {
renderConstraintLimit(ax, a.Fallback, a.Type, "default")
}
err = renderAnonymousType(doc, cluster, ax, a)
if err != nil {
Expand All @@ -46,7 +47,6 @@ func renderAttributes(doc *spec.Doc, cluster *matter.Cluster, c *etree.Element)
if err != nil {
return
}
renderFallback(cluster.Attributes, a, ax)
}
return
}
Expand Down
5 changes: 3 additions & 2 deletions dm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/beevik/etree"
"github.com/project-chip/alchemy/matter"
"github.com/project-chip/alchemy/matter/conformance"
"github.com/project-chip/alchemy/matter/constraint"
"github.com/project-chip/alchemy/matter/spec"
)

Expand Down Expand Up @@ -90,8 +91,8 @@ func renderCommands(doc *spec.Doc, cluster *matter.Cluster, c *etree.Element) (e
}
i.CreateAttr("name", f.Name)
renderDataType(f, i)
if len(f.Fallback) > 0 {
i.CreateAttr("default", f.Fallback)
if !constraint.IsBlankLimit(f.Fallback) {
i.CreateAttr("default", f.Fallback.ASCIIDocString(f.Type))
}
err = renderAnonymousType(doc, cluster, i, f)
if err != nil {
Expand Down
24 changes: 17 additions & 7 deletions dm/conformance.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,43 @@ func renderConformanceExpression(doc *spec.Doc, identifierStore conformance.Iden
if e.Not {
parent = parent.CreateElement("notTerm")
}
var el *etree.Element
if doc == nil {
parent.CreateElement("condition").CreateAttr("name", e.Reference)
el = parent.CreateElement("condition")
el.CreateAttr("name", e.Reference)
} else {
entity, ok := doc.Reference(e.Reference)
if !ok {
parent.CreateElement("condition").CreateAttr("name", e.Reference)
el = parent.CreateElement("condition")
el.CreateAttr("name", e.Reference)
} else {
switch entity := entity.(type) {
case *matter.Field:
switch entity.EntityType() {
case types.EntityTypeAttribute:
parent.CreateElement("attribute").CreateAttr("name", entity.Name)
el = parent.CreateElement("attribute")
el.CreateAttr("name", entity.Name)
case types.EntityTypeStructField:
parent.CreateElement("field").CreateAttr("name", entity.Name)
el = parent.CreateElement("field")
el.CreateAttr("name", entity.Name)
}
case *matter.Command:
parent.CreateElement("command").CreateAttr("name", entity.Name)
el = parent.CreateElement("command")
el.CreateAttr("name", entity.Name)
default:
switch entity.EntityType() {
case types.EntityTypeCondition:
parent.CreateElement("attribute").CreateAttr("name", e.Reference)
el = parent.CreateElement("attribute")
default:
parent.CreateElement("condition").CreateAttr("name", e.Reference)
parent.CreateElement("condition")
}
el.CreateAttr("name", e.Reference)
}
}
}
if e.Field != "" && el != nil {
el.CreateAttr("property", e.Field)
}
case *conformance.ComparisonExpression:
switch e.Op {
case conformance.ComparisonOperatorEqual:
Expand Down
Loading

0 comments on commit 585e1dc

Please sign in to comment.