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

Bitwise AND custom operator #112

Closed
alirezanet opened this issue Aug 18, 2023 Discussed in #111 · 0 comments · Fixed by #113
Closed

Bitwise AND custom operator #112

alirezanet opened this issue Aug 18, 2023 Discussed in #111 · 0 comments · Fixed by #113
Labels
bug Something isn't working close fixed

Comments

@alirezanet
Copy link
Owner

alirezanet commented Aug 18, 2023

Discussed in #111

Originally posted by dalen August 17, 2023
I'm trying to make a custom operator for bitwise and (on top of Pomelo EF Core with MariaDB). But I can't get it working.

I was hoping something like this would work, it compiles, but doesn't work runtime

public class BitwiseAndOperator : IGridifyOperator
{
    public string GetOperator() => "#*";

    public Expression<OperatorParameter> OperatorHandler()
    {
        return (prop, value) => ((uint)prop & (uint)value) != 0;
    }
}
Rewriting child expression from type 'System.Object' to type 'AbilityLib.ComponentOperationFlags' is not allowed, because it would change the meaning of the operation. If this is intentional, override 'VisitUnary' and change it to allow this rewrite.

Gridify.GridifyGlobalConfiguration.CustomOperators.Register(new BitwiseAndOperator());

var lst = new List<AbilityComponents>()
{
	new AbilityComponents() {Prop = ComponentOperationFlags.Opt1 },
	new AbilityComponents() {Prop = ComponentOperationFlags.Opt2 },
	new AbilityComponents() {Prop = ComponentOperationFlags.Opt1 | ComponentOperationFlags.Opt2 }
};

// lst.Select(q=> (uint) q.ComponentFlags).Dump();
// lst.Where(l => ((uint)l.ComponentFlags & 1) != 0).Dump();

lst.AsQueryable().ApplyFiltering("Prop #* 1").Dump();


public class AbilityComponents
{
	public ComponentOperationFlags Prop { get; set; }
}

[Flags]
public enum ComponentOperationFlags: uint
{
	None = 0,
	Opt1 = 1,
	Opt2 = 2,
	Opt3 = 4
}


public class BitwiseAndOperator : IGridifyOperator
{
	public string GetOperator() => "#*";

	public Expression<OperatorParameter> OperatorHandler()
	{
		return (prop, value) => ((uint)prop & (uint)value) != 0;
	}
}
@alirezanet alirezanet added the bug Something isn't working label Aug 18, 2023
alirezanet added a commit that referenced this issue Aug 18, 2023
alirezanet added a commit that referenced this issue Aug 18, 2023
* fix: custom operator support, Unary and bitwise operations

* test: issue #112 test

* chore: add some comments for readability

* test: update test assertion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working close fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant