Skip to content

Commit

Permalink
Merge pull request #44 from ZEXSM/feature/add-indexof-function
Browse files Browse the repository at this point in the history
add indexof function
  • Loading branch information
ZEXSM authored Feb 13, 2021
2 parents 7cdcd89 + 068c27f commit 466b0a8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ public interface IODataStringAndCollectionFunction
/// http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_concat
/// </summary>
string Concat(string s1, string s2);

/// <summary>
/// http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_indexof
/// </summary>
int IndexOf(string columnName, string value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ protected override string VisitMethodCallExpression(MethodCallExpression methodC
var toLower0 = VisitExpression(methodCallExpression.Arguments[0]);

return $"{nameof(IODataFunction.ToLower).ToLowerInvariant()}({toLower0})";
case nameof(IODataStringAndCollectionFunction.IndexOf):
var indexOf0 = VisitExpression(methodCallExpression.Arguments[0]);
var indexOf1 = VisitExpression(methodCallExpression.Arguments[1]);

return $"{nameof(IODataFunction.IndexOf).ToLowerInvariant()}({indexOf0},{indexOf1})";
case nameof(IConvertFunction.ConvertEnumToString):
return $"'{_valueExpression.GetValue(methodCallExpression.Arguments[0])}'";
case nameof(IConvertFunction.ConvertDateTimeToString):
Expand Down
16 changes: 14 additions & 2 deletions test/OData.QueryBuilder.Test/ODataQueryOptionListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,9 +1090,21 @@ public void ODataQueryBuilderList_Filter_Method_Not_Supported_NotSupportedExcept
.Invoking(c => c
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Filter((s, f) => @string.IndexOf("t") == 1)
.Filter((s, f) => @string.Trim() == "s")
.ToUri())
.Should().Throw<NotSupportedException>().WithMessage($"Method {nameof(string.IndexOf)} not supported");
.Should().Throw<NotSupportedException>().WithMessage($"Method {nameof(string.Trim)} not supported");
}

[Fact(DisplayName = "IndexOf Test => Success")]
public void ODataQueryBuilderList_Test_IndexOf()
{
var uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Filter((s, f) => f.IndexOf(s.ODataKind.ODataCode.Code, "testCode") == 1)
.ToUri();

uri.OriginalString.Should().Be("http://mock/odata/ODataType?$filter=indexof(ODataKind/ODataCode/Code,'testCode') eq 1");
}
}
}

0 comments on commit 466b0a8

Please sign in to comment.