alirezanet/Gridify

Bitwise AND custom operator

alirezanet opened this issue · 0 comments

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;
	}
}