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

Adding support for overloaded op_Equality #273

Merged
merged 5 commits into from
May 19, 2019

Conversation

nothrow
Copy link
Contributor

@nothrow nothrow commented May 18, 2019

Issue #272.

When the types mismatch, but support overloaded Equality/Inequality operators, use those.

@codecov
Copy link

codecov bot commented May 18, 2019

Codecov Report

Merging #273 into master will decrease coverage by 9.66%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #273      +/-   ##
==========================================
- Coverage   96.52%   86.85%   -9.67%     
==========================================
  Files          41       41              
  Lines        7244     4221    -3023     
==========================================
- Hits         6992     3666    -3326     
- Misses        252      555     +303
Impacted Files Coverage Δ
...ystem.Linq.Dynamic.Core/Parser/ExpressionParser.cs 85.61% <100%> (-13.04%) ⬇️
...q.Dynamic.Core/Parser/ConstantExpressionWrapper.cs 68% <0%> (-32%) ⬇️
...rkCore.DynamicLinq/EFDynamicQueryableExtensions.cs 72.9% <0%> (-26.94%) ⬇️
...stem.Linq.Dynamic.Core/DefaultQueryableAnalyzer.cs 80% <0%> (-20%) ⬇️
...Providers/AbstractDynamicLinqCustomTypeProvider.cs 59.32% <0%> (-11.92%) ⬇️
....Linq.Dynamic.Core/Parser/PredefinedTypesHelper.cs 90.76% <0%> (-9.24%) ⬇️
src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs 91.47% <0%> (-8.53%) ⬇️
...nq.Dynamic.Core/Util/ParameterExpressionRenamer.cs 92.3% <0%> (-7.7%) ⬇️
src/System.Linq.Dynamic.Core/Validation/Check.cs 47.88% <0%> (-7.67%) ⬇️
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b3e9f7...c929c4d. Read the comment docs.

Copy link
Collaborator

@StefH StefH left a comment

Choose a reason for hiding this comment

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

Please take a look at my comments.

var qry = testList.AsQueryable();

// Act
var expectedX = (ulong) long.MaxValue + 3;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
var expectedX = (ulong) long.MaxValue + 3;
ulong expectedX = (ulong) long.MaxValue + 3;

@codecov-io
Copy link

Codecov Report

Merging #273 into master will decrease coverage by 9.66%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #273      +/-   ##
==========================================
- Coverage   96.52%   86.85%   -9.67%     
==========================================
  Files          41       41              
  Lines        7244     4221    -3023     
==========================================
- Hits         6992     3666    -3326     
- Misses        252      555     +303
Impacted Files Coverage Δ
...ystem.Linq.Dynamic.Core/Parser/ExpressionParser.cs 85.61% <100%> (-13.04%) ⬇️
...q.Dynamic.Core/Parser/ConstantExpressionWrapper.cs 68% <0%> (-32%) ⬇️
...rkCore.DynamicLinq/EFDynamicQueryableExtensions.cs 72.9% <0%> (-26.94%) ⬇️
...stem.Linq.Dynamic.Core/DefaultQueryableAnalyzer.cs 80% <0%> (-20%) ⬇️
...Providers/AbstractDynamicLinqCustomTypeProvider.cs 59.32% <0%> (-11.92%) ⬇️
....Linq.Dynamic.Core/Parser/PredefinedTypesHelper.cs 90.76% <0%> (-9.24%) ⬇️
src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs 91.47% <0%> (-8.53%) ⬇️
...nq.Dynamic.Core/Util/ParameterExpressionRenamer.cs 92.3% <0%> (-7.7%) ⬇️
src/System.Linq.Dynamic.Core/Validation/Check.cs 47.88% <0%> (-7.67%) ⬇️
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b3e9f7...8f6026a. Read the comment docs.

@codecov-io
Copy link

codecov-io commented May 18, 2019

Codecov Report

Merging #273 into master will decrease coverage by 9.67%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #273      +/-   ##
==========================================
- Coverage   96.52%   86.84%   -9.68%     
==========================================
  Files          41       41              
  Lines        7244     4220    -3024     
==========================================
- Hits         6992     3665    -3327     
- Misses        252      555     +303
Impacted Files Coverage Δ
...ystem.Linq.Dynamic.Core/Parser/ExpressionParser.cs 85.6% <100%> (-13.05%) ⬇️
...q.Dynamic.Core/Parser/ConstantExpressionWrapper.cs 68% <0%> (-32%) ⬇️
...rkCore.DynamicLinq/EFDynamicQueryableExtensions.cs 72.9% <0%> (-26.94%) ⬇️
...stem.Linq.Dynamic.Core/DefaultQueryableAnalyzer.cs 80% <0%> (-20%) ⬇️
...Providers/AbstractDynamicLinqCustomTypeProvider.cs 59.32% <0%> (-11.92%) ⬇️
....Linq.Dynamic.Core/Parser/PredefinedTypesHelper.cs 90.76% <0%> (-9.24%) ⬇️
src/System.Linq.Dynamic.Core/Parser/TypeHelper.cs 91.47% <0%> (-8.53%) ⬇️
...nq.Dynamic.Core/Util/ParameterExpressionRenamer.cs 92.3% <0%> (-7.7%) ⬇️
src/System.Linq.Dynamic.Core/Validation/Check.cs 47.88% <0%> (-7.67%) ⬇️
... and 20 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b3e9f7...a044177. Read the comment docs.

@StefH
Copy link
Collaborator

StefH commented May 19, 2019

Thanks a lot for this PR.

I'll create a new NuGet tomorrow.

@StefH StefH merged commit 2df8263 into zzzprojects:master May 19, 2019
@mehdisalehi
Copy link

Equality operator doesnot work for custom class and string. since both are non value type that is why it do not enters the changed code. Same will happen for 2 custom class comparision

@StefH
Copy link
Collaborator

StefH commented Sep 13, 2019

@mehdisalehi Please provide a example or unit-test.
And if possible, create a PR to fix this.

@mehdisalehi
Copy link

mehdisalehi commented Sep 14, 2019

@StefH Below is the example of showing it works for integer but not for string.
I will try and create PR as soon as i can.

public class SpecialInteger
{
    int i;

    public SpecialInteger(int i)
    {
        this.i = i;
    }

    public override bool Equals(object obj)
    {
        return obj is SpecialInteger test &&
               i == test.i;
    }

    public override int GetHashCode()
    {
        var hashCode = -869367308;
        hashCode = hashCode * -1521134295 + i.GetHashCode();
        return hashCode;
    }

    public static bool operator ==(int left, SpecialInteger right)
    {
        return left == right.i;
    }

    public static bool operator !=(int left, SpecialInteger right)
    {
        return !(left == right);
    }

    public static bool operator ==(SpecialInteger left, int right)
    {
        return left.i == right;
    }

    public static bool operator !=(SpecialInteger left, int right)
    {
        return !(left == right);
    }

    public static bool operator ==(SpecialInteger left, SpecialInteger right)
    {
        return left.Equals(right);
    }

    public static bool operator !=(SpecialInteger left, SpecialInteger right)
    {
        return !(left == right);
    }
}
public class SpecialString
{
    String i;

    public SpecialString(String i)
    {
        this.i = i;
    }

    public static bool operator ==(string left, SpecialString right)
    {
        return left == right.i;
    }

    public static bool operator !=(string left, SpecialString right)
    {
        return !(left == right);
    }

    public static bool operator ==(SpecialString left, string right)
    {
        return left.i == right;
    }

    public static bool operator !=(SpecialString left, string right)
    {
        return !(left == right);
    }

    public static bool operator ==(SpecialString left, SpecialString right)
    {
        return EqualityComparer<SpecialString>.Default.Equals(left, right);
    }

    public static bool operator !=(SpecialString left, SpecialString right)
    {
        return !(left == right);
    }

    public override bool Equals(object obj)
    {
        return obj is SpecialString @string &&
               i == @string.i;
    }

    public override int GetHashCode()
    {
        return 165851236 + EqualityComparer<string>.Default.GetHashCode(i);
    }
}


class Program
{
    static void Main(string[] args)
    {
        var specialIntegers = new[] 
        {
            new SpecialInteger(5),
            new SpecialInteger(10),
            new SpecialInteger(15),
        }.AsQueryable();

        var actualSpecialIntegerItems = specialIntegers.Where(it => it == 5);
        var expectedSpecialIntegerItems = specialIntegers.Where("it == 5");


        Console.WriteLine("Result ValueType: {0}", actualSpecialIntegerItems.SequenceEqual(expectedSpecialIntegerItems));


        var specialStrings = new[]
        {
            new SpecialString("5"),
            new SpecialString("ABC"),
            new SpecialString("XYZ"),
        }.AsQueryable();

        var actualSpecialStringItems = specialStrings.Where(it => it == "XYZ");
        var expectedSpecialStringItems = specialStrings.Where("it == \"XYZ\"");


        Console.WriteLine("Result Non ValueType: {0}", actualSpecialStringItems.SequenceEqual(expectedSpecialStringItems));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants