Skip to content

Commit

Permalink
Merge pull request #16 from ZEXSM/bugfix/static-values-and-strings
Browse files Browse the repository at this point in the history
* fix use string values
* fix use static values
  • Loading branch information
ZEXSM authored Aug 17, 2019
2 parents 3af0066 + 77276f9 commit 8ef5079
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
28 changes: 16 additions & 12 deletions src/OData.QueryBuilder/Extensions/ExpressionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ public static string ToODataQuery(this ConstantExpression constantExpression)
{
switch (constantExpression.Value)
{
case true:
return "true";
case false:
return "false";
case null:
return "null";
case bool boolVal:
return boolVal.ToString().ToLower();
case string stringVal:
return $"'{stringVal}'";
case int intVal:
return intVal.ToString();
default:
return constantExpression.Value.ToString();
return "null";
}
}

Expand All @@ -102,9 +102,6 @@ public static string ToODataQuery(this LambdaExpression lambdaExpression)
return $"{tag}:{tag}/{filter}";
}

public static string ToODataQuery(this ParameterExpression parameterExpression) =>
parameterExpression.Name;

public static string ToODataQuery(this NewExpression newExpression)
{
var names = new string[newExpression.Members.Count];
Expand Down Expand Up @@ -209,9 +206,16 @@ public static string ToODataQuery(this Expression expression, string queryString
return $"{leftQueryString} {binaryExpression.NodeType.ToODataOperator()} {rightQueryString}";

case MemberExpression memberExpression:
if (memberExpression.Expression is ConstantExpression)
var memberExpressionValue = memberExpression.GetMemberExpressionValue();

if (memberExpressionValue != default(object))
{
return memberExpression.GetMemberExpressionValue().ToString();
if (memberExpressionValue is string)
{
return $"'{memberExpressionValue}'";
}

return memberExpressionValue.ToString();
}

var parentMemberExpressionQuery = memberExpression.Expression.ToODataQuery(queryString);
Expand Down
24 changes: 16 additions & 8 deletions src/OData.QueryBuilder/Extensions/ReflectionExtension.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
using System.Reflection;
using System;
using System.Reflection;

namespace OData.QueryBuilder.Extensions
{
internal static class ReflectionExtension
{
public static object GetValue(this MemberInfo memberInfo, object obj = default(object))
{
switch (memberInfo)
try
{
case FieldInfo fieldInfo:
return fieldInfo.GetValue(obj);
case PropertyInfo propertyInfo:
return propertyInfo.GetValue(obj, default(object[]));
default:
return default(object);
switch (memberInfo)
{
case FieldInfo fieldInfo:
return fieldInfo.GetValue(obj);
case PropertyInfo propertyInfo:
return propertyInfo.GetValue(obj, default(object[]));
default:
return default(object);
}
}
catch (Exception)
{
return default(object);
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions test/OData.QueryBuilder.Test/ODataQueryBuilderByListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class ODataQueryBuilderByListTest : IClassFixture<CommonFixture>
{
private readonly ODataQueryBuilder<ODataInfoContainer> _odataQueryBuilder;

public static string IdCodeStatic => "testCode";

public ODataQueryBuilderByListTest(CommonFixture commonFixture) =>
_odataQueryBuilder = commonFixture.ODataQueryBuilder;

Expand Down Expand Up @@ -109,8 +111,8 @@ public void ODataQueryBuilderList_Skip_Top_Simple_Success()
uri.OriginalString.Should().Be("http://mock/odata/ODataType?$skip=1&$top=1");
}

[Fact(DisplayName = "(ODataQueryBuilderList) Filter simple => Success")]
public void ODataQueryBuilderList_Filter_Simple_Success()
[Fact(DisplayName = "(ODataQueryBuilderList) Filter simple const int=> Success")]
public void ODataQueryBuilderList_Filter_Simple_Const_Int_Success()
{
var uri = _odataQueryBuilder
.For<ODataTypeEntity>(s => s.ODataType)
Expand All @@ -121,6 +123,21 @@ public void ODataQueryBuilderList_Filter_Simple_Success()
uri.OriginalString.Should().Be("http://mock/odata/ODataType?$filter=ODataKind/ODataCode/IdCode ge 3 or IdType eq 5");
}

[Fact(DisplayName = "(ODataQueryBuilderList) Filter simple const string => Success")]
public void ODataQueryBuilderList_Filter_Simple_Const_String_Success()
{
var constValue = "3";
var uri = _odataQueryBuilder
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Filter(s =>
s.ODataKind.ODataCode.Code == constValue || s.ODataKind.ODataCode.Code == "5"
&& s.ODataKind.ODataCode.Code == IdCodeStatic)
.ToUri();

uri.OriginalString.Should().Be("http://mock/odata/ODataType?$filter=ODataKind/ODataCode/Code eq '3' or ODataKind/ODataCode/Code eq '5' and ODataKind/ODataCode/Code eq 'testCode'");
}

[Fact(DisplayName = "(ODataQueryBuilderList) Filter All/Any => Success")]
public void ODataQueryBuilderList_Filter_All_Any_Success()
{
Expand Down

0 comments on commit 8ef5079

Please sign in to comment.