-
Notifications
You must be signed in to change notification settings - Fork 409
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
Migrate to PEG parser. Introduce boolean operators and constants. #2182
base: main
Are you sure you want to change the base?
Conversation
src/CommunityToolkit.Maui.UnitTests/Converters/MathExpressionConverterTests.cs
Outdated
Show resolved
Hide resolved
src/CommunityToolkit.Maui.UnitTests/Converters/MathExpressionConverterTests.cs
Outdated
Show resolved
Hide resolved
@dotnet-policy-service agree company="Esri" |
Hi @brminnick since your review, I have:
The fork is ready for another round of review |
src/CommunityToolkit.Maui/Converters/MathExpressionConverter/MathExpression.shared.cs
Outdated
Show resolved
Hide resolved
src/CommunityToolkit.Maui/Converters/MathExpressionConverter/MathExpression.shared.cs
Outdated
Show resolved
Hide resolved
@stephenquan here's the build log:
|
Thanks @pictos, I have reviewed the CS8063 possible null reference return issues and have resolved it by making nullability updates to both the MathExpression.shared.cs and MathExpressionConverter.shared.cs. The nullability updates also required additional Asserts added to the MathExpressionConverterTests.cs. With these changes, the build checks are now passing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we're getting really close to merge this!
src/CommunityToolkit.Maui/Converters/MathExpressionConverter/MathExpression.shared.cs
Outdated
Show resolved
Hide resolved
src/CommunityToolkit.Maui/Converters/MathExpressionConverter/MathExpression.shared.cs
Outdated
Show resolved
Hide resolved
…stead of raising exceptions.
@pictos @bijington I have pushed updates to the fork address both the C# property name pattern and logging invalid math expressions with TraceWarning. Because we're no longer raising invalid math expressions I had to change the relevant unit test from an exception test to a null check. The fork is ready for review again. |
...munityToolkit.Maui/Converters/MathExpressionConverter/MultiMathExpressionConverter.shared.cs
Outdated
Show resolved
Hide resolved
…ssionConverter and corresponding unit tests.
@pictos @brminnick as requested, I have removed the Null Forgiving Operator from the MultiMathExpressionConverter. I have corrected the null checks in the MultiMathExpressionConverter and that resulted in a necessary minor update to the unit tests. This fork is ready for review again. |
Hi all, we are using snapshot copies of these classes in our projects. I am eager to help out and remove any blockers on this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I have some new developers working with Boolean operators in XAML, but they struggled with the syntax. To simplify I things added new commits which adds XAML-friendly alternatives. Additionally, I added more coverage to the logical AND/OR unit tests. Unfortunately, in doing so, 3 of the 6 automated test are failing and I am not sure why. Also, the PR will now require a re-review.
Here are some examples of the alternate syntax:
|
…es in unit tests.
The azure pipelines https://dev.azure.com/dotnet/CommunityToolkit/_build/results?buildId=112270&view=logs&j=67da6a11-18ef-5f70-9b43-fae7e6c83e18&t=54ba8226-26d8-53c8-39e8-392f99f22929&l=538 is showing 11 new errors that I do not know how to resolve. These 11 new errors do not relate the commits done, so, I'm hoping they will automatically resolve when the fork catches up with the main branch: D:\a\1\s\samples\CommunityToolkit.Maui.Sample\MauiProgram.cs(38,20): error CS0234: The type or namespace name 'Composition' does not exist in the namespace 'Microsoft.UI' (are you missing an assembly reference?) [D:\a\1\s\samples\CommunityToolkit.Maui.Sample\CommunityToolkit.Maui.Sample.csproj::TargetFramework=net8.0-windows10.0.19041.0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 5 out of 6 changed files in this pull request and generated no suggestions.
Files not reviewed (1)
- samples/CommunityToolkit.Maui.Sample/Pages/Converters/MultiMathExpressionConverterPage.xaml: Language not supported
Comments skipped due to low confidence (3)
src/CommunityToolkit.Maui.UnitTests/Converters/MathExpressionConverterTests.cs:66
- [nitpick] The variable name 'variables' should be renamed to 'values' for consistency with the method parameter name in MultiMathExpressionConverter.shared.cs.
public void MathExpressionConverter_WithMultipleVariable_ReturnsCorrectResult(string expression, object[] variables, double expectedResult)
src/CommunityToolkit.Maui/Converters/MathExpressionConverter/MathExpressionConverter.shared.cs:20
- [nitpick] The variable name 'value' is less descriptive in the context of mathematical expressions. Consider renaming it to 'inputValue' for clarity.
public override object? ConvertFrom(object? value, string parameter, CultureInfo? culture = null)
src/CommunityToolkit.Maui/Converters/MathExpressionConverter/MathExpressionConverter.shared.cs:20
- The removal of the '[return: NotNullIfNotNull(nameof(values))]' attribute might lead to unclear error messages when null values are encountered. Consider adding a clear error message for null values.
public override object? ConvertFrom(object? value, string parameter, CultureInfo? culture = null)
Hi @stephenquan! Could you update this PR to resolve the merge conflicts? We've finally merged the massive .NET 9 PR today and it introduced a few merge conflicts across our open PRs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.
Files not reviewed (1)
- samples/CommunityToolkit.Maui.Sample/Pages/Converters/MultiMathExpressionConverterPage.xaml: Language not supported
[InlineData("Cat", false, false, "Cat")] | ||
[InlineData("", false, "", false)] | ||
[InlineData(false, "", false, "")] | ||
[InlineData(null, "Cat", null, "Cat")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @stephenquan! There's one thing I'm having trouble wrapping my head around in this PR: How/why we are evaluating null
using boolean operators, as demonstrated here in this test?
If I'm understanding everything correctly, the below line of code is equivalent to evaluating the boolean math equation of null OR null
, right?
mathExpressionConverter.Convert(variables, typeof(object), "x || x1")
I haven't studied boolean algebra since college, but, my understanding is that boolean algebra does not include the concept of null
. Is my understanding correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My gut tells me that we should be throwing an Exception
in the scenarios where null
is being used in a Boolean Algebraic equation.
In other words, MathExpression.Calculate()
should not return null
in these scenarios and instead throw an exception alerting the developer that they've input an invalid equation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brminnick thanks for your question. The inspiration for logical-AND and logical-OR operators is derived from both Javascript and Python with respect to how they handle truthy and falsy concepts. For reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND
If a value can be converted to true, the value is so-called truthy. If a value can be converted to false, the value is so-called falsy.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR
If x can be converted to true, returns x; else, returns y.
And the unit test mirror the results from the Javascript documentation examples.
The Python documentation also describes this behavior: https://docs.python.org/3/reference/expressions.html#boolean-operations
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
If I wanted to implement the following case statement
// states of water
if (x0 < 0) then x1 // "ice";
else if (x0 < 100) then x2 // "water"
else x3 // "steam"
I could write it as:
// using the conditional operator
x0 < 0 ? x1
: x0 < 100
? x2
: x3
Or, using the truthy and falsy states, I could rewrite it as
// using the truthy and falsy application of logical-AND and logical-OR
(x0 < 0) and x1
or (x0 < 100) and x2
or x3
Generalizing the pattern, it would be:
conditional-1 and result-1
or conditional-2 and result-2
or ...
or conditional-n and result-n
or result-else
There are limitations to this approach since the things fail if any of the intermediate results are "falsy" then it doesn't work. In the above example, "ice", "water" and "steam" are truthy values since they're non-empty strings. It is also because of this behavior, that we do not require any of the inputs to be strictly boolean. They can be of any object including null and it would still behave.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Javascript
console.log(null || null); // output: null
console.log(null && null); // output: null
Python
>>> print(None or None) // output: None
None
>>> print(None and None) // output: None
None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the equivalent of a Nullable<bool>
expression so I think it's fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.
Files not reviewed (1)
- samples/CommunityToolkit.Maui.Sample/Pages/Converters/MultiMathExpressionConverterPage.xaml: Language not supported
Description of Change
double
withobject
+
,-
,*
,/
and^
as well as math functionsand
,or
,?
,:
,>=
,>
,<=
,<
,==
,!=
as well astrue
andfalse
constantsdouble
withobject
General comments:
Linked Issues
#2181
PR Checklist
approved
(bug) orChampioned
(feature/proposal)main
at time of PRAdditional information