improve boolean and integer comparison

This commit is contained in:
Asdine El Hrychy
2025-09-20 20:47:48 +05:30
parent 9cf4dc88d6
commit 11d3afba02
4 changed files with 705 additions and 39 deletions

View File

@@ -91,56 +91,112 @@ func (v BooleanValue) ConvertToIndexedType(t Type) (Value, error) {
}
func (v BooleanValue) EQ(other Value) (bool, error) {
if other.Type() != TypeBoolean {
if other.Type() == TypeNull {
return false, nil
}
if other.Type() == TypeText {
tv, err := other.CastAs(TypeBoolean)
if err != nil {
return false, err
}
other = tv
}
if other.Type() != TypeBoolean {
return false, errors.Errorf("cannot compare boolean with %s", other.Type())
}
return bool(v) == AsBool(other), nil
}
func (v BooleanValue) GT(other Value) (bool, error) {
if other.Type() != TypeBoolean {
if other.Type() == TypeNull {
return false, nil
}
if other.Type() == TypeText {
tv, err := other.CastAs(TypeBoolean)
if err != nil {
return false, err
}
other = tv
}
if other.Type() != TypeBoolean {
return false, errors.Errorf("cannot compare boolean with %s", other.Type())
}
return bool(v) && !AsBool(other), nil
}
func (v BooleanValue) GTE(other Value) (bool, error) {
if other.Type() != TypeBoolean {
if other.Type() == TypeNull {
return false, nil
}
if other.Type() == TypeText {
tv, err := other.CastAs(TypeBoolean)
if err != nil {
return false, err
}
other = tv
}
if other.Type() != TypeBoolean {
return false, errors.Errorf("cannot compare boolean with %s", other.Type())
}
bv := bool(v)
return bv == AsBool(other) || bv, nil
}
func (v BooleanValue) LT(other Value) (bool, error) {
if other.Type() != TypeBoolean {
if other.Type() == TypeNull {
return false, nil
}
if other.Type() == TypeText {
tv, err := other.CastAs(TypeBoolean)
if err != nil {
return false, err
}
other = tv
}
if other.Type() != TypeBoolean {
return false, errors.Errorf("cannot compare boolean with %s", other.Type())
}
return !bool(v) && AsBool(other), nil
}
func (v BooleanValue) LTE(other Value) (bool, error) {
if other.Type() != TypeBoolean {
if other.Type() == TypeNull {
return false, nil
}
if other.Type() == TypeText {
tv, err := other.CastAs(TypeBoolean)
if err != nil {
return false, err
}
other = tv
}
if other.Type() != TypeBoolean {
return false, errors.Errorf("cannot compare boolean with %s", other.Type())
}
bv := bool(v)
return bv == AsBool(other) || !bv, nil
}
func (v BooleanValue) Between(a, b Value) (bool, error) {
if a.Type() != TypeBoolean || b.Type() != TypeBoolean {
return false, nil
}
ok, err := a.LTE(v)
ok, err := v.GTE(a)
if err != nil || !ok {
return false, err
}
return b.GTE(v)
return v.LTE(b)
}