add ensure_compatibility_from_left

This commit is contained in:
simon lehericey 2022-06-09 13:45:56 +02:00
parent 5ac3049033
commit 9f49e8c2ea
2 changed files with 79 additions and 0 deletions

View file

@ -12,6 +12,43 @@ module Logic
.find { |c| c.name == name }
end
def self.ensure_compatibility_from_left(condition)
left = condition.left
right = condition.right
operator_class = condition.class
case [left.type, condition]
in [:boolean, _]
operator_class = Eq
in [:empty, _]
operator_class = EmptyOperator
in [:enum, _]
operator_class = Eq
in [:number, EmptyOperator]
operator_class = Eq
in [:number, _]
in [:string, _]
operator_class = Eq
end
if !compatible_type?(left, right)
right = case left.type
when :boolean
Constant.new(true)
when :empty
Empty.new
when :enum
Constant.new(left.options.first)
when :number
Constant.new(0)
when :string
Constant.new('')
end
end
operator_class.new(left, right)
end
def self.compatible_type?(left, right)
case [left.type, right.type]
in [a, ^a] # syntax for same type