Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow comparing date/datetimes to ISO8601 date/datetime strings #188

Merged
merged 7 commits into from
Mar 13, 2024

Conversation

fedme
Copy link
Contributor

@fedme fedme commented Mar 12, 2024

This PR adds overloads for the :=, :==, :!=, :<, :<=, :>, :>= boolean operators to allows users to compare Date or DateTime values with ISO8601 string representations of dates and datetimes.

Adding support for this kind of comparisons makes it much easier to create Triggers/Conditionals in Turn that compare a Contact's datetime field to a specific datetime value (expressed as an ISO8601 string).

Copy link

This pull request has been linked to Shortcut Story #30024: Add datetime comparison operators.

@fedme fedme requested a review from santiagocardo March 12, 2024 12:49
@@ -177,7 +177,7 @@ defmodule Expression.Eval do
def op(:==, a, b) when is_struct(a, DateTime) and is_struct(b, DateTime),
do: DateTime.compare(a, b) == :eq

def op(:=, a, b) when is_struct(a, Date) and is_struct(b, Date),
def op(:=, a, b) when is_struct(a, DateTime) and is_struct(b, DateTime),
Copy link
Contributor Author

@fedme fedme Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was a typo, as this function clause was appearing twice.
I think this one was supposed to be for DateTime, not for Date: the one for Date already exists down below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smn could you confirm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, most probably

@fedme
Copy link
Contributor Author

fedme commented Mar 12, 2024

@smn @santiagocardo any ideas on how to fix the failing dialyzer check in CI? It passes locally. It's saying something about an invalid PLT? Maybe an invalid cache?

end

case {operator, comparison_result} do
{:=, :eq} -> true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we intentionally supporting the match operator as an equal operator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this was already the case in the existing code. The FLOIP standard supports both = and == (or at least RapidPro does).

def op(operator, a, b)
when operator in [:=, :==, :!=, :<, :<=, :>, :>=] and
(is_struct(a, Date) or is_struct(a, DateTime)) and
is_binary(b) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fedme are we handling a scenario where a is a binary and b is a struct? Do we need to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this PR I am only handling the case where the 1st argument is a Date/DateTime and the 2nd argument is a String. That's all the Canvas needs at the moment but I am happy to expand it to support all combinations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have pushed a change to support the other args ordering you mentioned @smn 4adbc14

Copy link
Contributor

@smn smn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with this change, just curious about when a is a binary and b is a struct and if we should handle that. I can see how that could be expected from an expression authoring perspective if we're already supporting the scenario we're testing against in this PR.

If we don't, we should document this behaviour well.

@fedme fedme merged commit 45d70a2 into develop Mar 13, 2024
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants