Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions QueryKit.UnitTests/HasConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,115 @@ public void guid_with_has_conversion_compares_a_guid_constant(string queryName)
filter.ToDisplayString().Should().Be($"x => (x.Id == {KnownGuid})");
}

[Fact]
public void can_filter_struct_with_in_operator_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasConversion<string>();
});

// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""Id ^^ ["2","3"]""", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("two");
}

[Fact]
public void can_filter_struct_with_not_in_operator_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasConversion<string>();
});

// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""Id !^^ ["2","3"]""", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Name.Should().Be("one");
}

[Fact]
public void can_filter_struct_with_in_operator_query_name_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<WrappedIdRow>(x => x.Id).HasQueryName("wrappedid").HasConversion<string>();
});

// Act
var result = WrappedIdRows().ApplyQueryKitFilter("""wrappedid ^^ ["1","2"]""", config).ToList();

// Assert
result.Select(x => x.Name).Should().BeEquivalentTo("one", "two");
}

[Fact]
public void can_filter_nullable_struct_with_in_operator_and_has_conversion()
{
// Arrange
var rows = new List<NullableWrappedIdRow>
{
new() { Id = new WrappedId(1) },
new() { Id = new WrappedId(2) },
new() { Id = null }
};
var config = new QueryKitConfiguration(config =>
{
config.Property<NullableWrappedIdRow>(x => x.Id!).HasConversion<string>();
});

// Act
var result = rows.ApplyQueryKitFilter("""Id ^^ ["2","3"]""", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Id.Should().Be(new WrappedId(2));
}

[Fact]
public void can_filter_reference_type_with_in_operator_and_has_conversion()
{
// Arrange
var config = new QueryKitConfiguration(config =>
{
config.Property<EmailRow>(x => x.Email!).HasConversion<string>();
});

// Act
var result = EmailRows().ApplyQueryKitFilter("""Email ^^ ["b@x.com","c@x.com"]""", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Email!.Value.Should().Be("b@x.com");
}

[Fact]
public void can_filter_reference_type_with_in_operator_and_comma_in_quoted_item_and_has_conversion()
{
// Arrange
var rows = EmailRows();
rows.Add(new EmailRow { Email = new EmailAddressRecord("a,b@x.com") });
var config = new QueryKitConfiguration(config =>
{
config.Property<EmailRow>(x => x.Email!).HasConversion<string>();
});

// Act
var result = rows.ApplyQueryKitFilter("""Email ^^ ["a,b@x.com","c@x.com"]""", config).ToList();

// Assert
result.Count.Should().Be(1);
result[0].Email!.Value.Should().Be("a,b@x.com");
}

private static List<WrappedIdRow> WrappedIdRows() => new()
{
new() { Id = new WrappedId(1), Name = "one" },
Expand Down
40 changes: 40 additions & 0 deletions QueryKit/FilterParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,12 @@ private static Expression CreateRightExprForProperty(Expression leftExpr, string
// by constructing it from the string value using a constructor that takes the target type
if (propertyConfig.ConversionTargetType == typeof(string))
{
// An in/not-in list has to stay an array of constants, so each item is converted on its own.
if (IsArrayLiteral(right) && CreateStringConversionArrayExpr(leftExpr.Type, right) is { } arrayExpr)
{
return arrayExpr;
}

// A null literal compares against null instead of being passed to the constructor
var underlyingType = Nullable.GetUnderlyingType(leftExpr.Type);
if (right == "null" && (!leftExpr.Type.IsValueType || underlyingType != null))
Expand All @@ -650,6 +656,40 @@ private static Expression CreateRightExprForProperty(Expression leftExpr, string
return CreateRightExprFromType(targetType, right, rightIsQuotedLiteral, op);
}

private static bool IsArrayLiteral(string right)
=> right.StartsWith("[") && right.EndsWith("]");

// Builds the right side of an in/not-in filter for a property with HasConversion<string>(). The in operator
// reads an array of constants, so every item is constructed from its string with the (string) constructor.
private static Expression? CreateStringConversionArrayExpr(Type leftType, string right)
{
var underlyingType = Nullable.GetUnderlyingType(leftType);
var stringCtor = (underlyingType ?? leftType).GetConstructor(new[] { typeof(string) });
if (stringCtor == null)
{
return null;
}

var items = right.Length == 2 ? new List<string>() : SplitListItems(right);

var constants = items.Select(item =>
{
if (item == "null")
{
return Expression.Constant(null, leftType);
}

if (item.Length >= 2 && item.StartsWith("\"") && item.EndsWith("\""))
{
item = item.Substring(1, item.Length - 2);
}

return Expression.Constant(stringCtor.Invoke(new object[] { item }), leftType);
}).ToArray();

return Expression.NewArrayInit(leftType, constants);
}

// True when CreateRightExprFromType can read a value of the type. For other types it throws.
private static bool CanCreateRightExprFromType(Type type)
{
Expand Down