mirror of
https://github.com/chaisql/chai.git
synced 2025-11-03 09:41:01 +08:00
improve boolean and integer comparison
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user