Skip to content

Commit

Permalink
Add support for null comparisons to conformance
Browse files Browse the repository at this point in the history
  • Loading branch information
hasty committed Nov 1, 2024
1 parent 5b402dd commit a46422a
Show file tree
Hide file tree
Showing 4 changed files with 542 additions and 453 deletions.
2 changes: 1 addition & 1 deletion matter/conformance/grammar/math.peg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


EquationTerm = (FeatureValue / ReferenceValue / IdentifierValue / NumberValue / '(' _ eq:EquationValue _ ')' { return eq, nil})
EquationTerm = (FeatureValue / ReferenceValue / IdentifierValue / NumberValue / NullValue / '(' _ eq:EquationValue _ ')' { return eq, nil})

EquationValue <- _ left:EquationMultiply rest:( AddOp )* _ {
if rest == nil {
Expand Down
3 changes: 3 additions & 0 deletions matter/conformance/grammar/number.peg
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ Digits <- [0-9]+ {
return strconv.ParseInt(string(c.text), 10, 64)
}

NullValue <- "null" {
return NewNullValue(string(c.text)), nil
}
58 changes: 58 additions & 0 deletions matter/conformance/null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package conformance

import "fmt"

type NullValue struct {
raw string
}

func NewNullValue(raw string) *NullValue {
return &NullValue{raw: raw}
}

func (fv NullValue) ASCIIDocString() string {
return fv.raw
}

func (fv NullValue) Description() string {
return fv.raw
}

func (fv *NullValue) Compare(context Context, other ComparisonValue, op ComparisonOperator) (bool, error) {
switch op {
case ComparisonOperatorEqual:
switch other.(type) {
case nil, *NullValue:
return true, nil
default:
return false, nil
}
case ComparisonOperatorNotEqual:
switch other.(type) {
case nil, *NullValue:
return false, nil
default:
return true, nil
}
default:
return false, fmt.Errorf("invalid operator with null value: %s", op.String())
}
}

func (fv NullValue) Value(context Context) (any, error) {
return nil, nil
}

func (fv *NullValue) Equal(ofv ComparisonValue) bool {
if ofv == nil {
return fv == nil
} else if fv == nil {
return false
}
_, ok := ofv.(*NullValue)
return !ok
}

func (fv NullValue) Clone() ComparisonValue {
return &NullValue{raw: fv.raw}
}
Loading

0 comments on commit a46422a

Please sign in to comment.