Skip to content

Commit

Permalink
fix the wild card logic (#9)
Browse files Browse the repository at this point in the history
* fix the wild card logic

* update test name

---------

Co-authored-by: Bar Nuri <[email protected]>
  • Loading branch information
vitusik and barnuri authored Dec 18, 2024
1 parent 3c12a9b commit 091acb8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
20 changes: 10 additions & 10 deletions LinqToKql.Test/Translator/WhereTranslatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public Task Translate_WhereStringNotEndsWithAsync()
[Theory]
[InlineData("na", "==")]
[InlineData("%na%", "has_cs")]
[InlineData("%na", "startswith_cs")]
[InlineData("na%", "endswith_cs")]
[InlineData("%na", "endswith_cs")]
[InlineData("na%", "startswith_cs")]
public Task Translate_LikeAsync(string pattern, string action)
=> AssertQueryAsync(
_q.Like(y => y.Name, pattern, '%'),
Expand All @@ -166,8 +166,8 @@ public Task Translate_LikeAsync(string pattern, string action)
[Theory]
[InlineData("na", "==")]
[InlineData("%na%", "has_cs")]
[InlineData("%na", "startswith_cs")]
[InlineData("na%", "endswith_cs")]
[InlineData("%na", "endswith_cs")]
[InlineData("na%", "startswith_cs")]
public Task Translate_LikeByPropNameAsync(string pattern, string action)
=> AssertQueryAsync(
_q.Like("Name", pattern, '%'),
Expand All @@ -177,26 +177,26 @@ public Task Translate_LikeByPropNameAsync(string pattern, string action)
[Theory]
[InlineData("na", "==")]
[InlineData("*na*", "has_cs")]
[InlineData("*na", "startswith_cs")]
[InlineData("na*", "endswith_cs")]
[InlineData("*na", "endswith_cs")]
[InlineData("na*", "startswith_cs")]
public Task Translate_LikeByStringMethodAsync(string pattern, string action)
=> AssertQueryAsync(
_q.Where(x => x.Name.KqlLike(pattern, '*')),
[_tableName, $"where Name {action} 'na'"]
);

[Fact]
public Task Translate_WhereStringLikeStartsWithAsync()
public Task Translate_WhereStringLikeStartsWithWildCardAsync()
=> AssertQueryAsync(
_q.Where(x => EF.Functions.Like(x.Name, "%na")).Select(x => new { x.Date, x.Description }),
[_tableName, $"where Name startswith_cs 'na'", "project Date, Description"]
[_tableName, $"where Name endswith_cs 'na'", "project Date, Description"]
);

[Fact]
public Task Translate_WhereStringLikeEndsWithAsync()
public Task Translate_WhereStringLikeEndsWithWithWildCardAsync()
=> AssertQueryAsync(
_q.Where(x => EF.Functions.Like(x.Name, "na%")).Select(x => new { x.Date, x.Description }),
[_tableName, $"where Name endswith_cs 'na'", "project Date, Description"]
[_tableName, $"where Name startswith_cs 'na'", "project Date, Description"]
);

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions LinqToKql/Extensions/IQueryableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public static IQueryable<T> Like<T>(this IQueryable<T> q, Expression<Func<T, str
else if (startsWithWildcard)
{
// Use EndsWith when pattern starts with a wildcard
methodCall = Expression.Call(property, nameof(string.StartsWith), null, Expression.Constant(normalizedPattern));
methodCall = Expression.Call(property, nameof(string.EndsWith), null, Expression.Constant(normalizedPattern));
}
else if (endsWithWildcard)
{
// Use StartsWith when pattern ends with a wildcard
methodCall = Expression.Call(property, nameof(string.EndsWith), null, Expression.Constant(normalizedPattern));
methodCall = Expression.Call(property, nameof(string.StartsWith), null, Expression.Constant(normalizedPattern));
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions LinqToKql/Translator/Builders/LinqToKQLTranslatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ string HandleLike(MethodCallExpression methodCall, Expression leftSide)
}
if (likeValue.StartsWith("%"))
{
return $"{leftSideKql} startswith_cs {filter}";
return $"{leftSideKql} endswith_cs {filter}";
}
if (likeValue.EndsWith("%"))
{
return $"{leftSideKql} endswith_cs {filter}";
return $"{leftSideKql} startswith_cs {filter}";
}
return $"{leftSideKql} == {filter}";
}
Expand All @@ -215,11 +215,11 @@ string HandleKqlLike(MethodCallExpression methodCall)
}
if (likeValue.StartsWith(wildCardSymbol))
{
return $"{leftSideKql} startswith_cs {filter}";
return $"{leftSideKql} endswith_cs {filter}";
}
if (likeValue.EndsWith(wildCardSymbol))
{
return $"{leftSideKql} endswith_cs {filter}";
return $"{leftSideKql} startswith_cs {filter}";
}
return $"{leftSideKql} == {filter}";
}
Expand Down

0 comments on commit 091acb8

Please sign in to comment.