Skip to content

Commit

Permalink
Fix UseIndex's handling of single item arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
borigas committed Apr 25, 2019
1 parent 74db836 commit 9fea9b9
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/CouchDB.Driver/Translators/MethodCallExpressionTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using CouchDB.Driver.Extensions;
using CouchDB.Driver.Types;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

Expand Down Expand Up @@ -315,7 +317,29 @@ private Expression VisitUseIndexMethod(MethodCallExpression m)
{
Visit(m.Arguments[0]);
_sb.Append("\"use_index\":");
Visit(m.Arguments[1]);
if (!(m.Arguments[1] is ConstantExpression indexArgsExpression))
{
throw new ArgumentException("UseIndex requires an IList<string> argument");
}

if (!(indexArgsExpression.Value is IList<string> indexArgs))
{
throw new ArgumentException("UseIndex requires an IList<string> argument");
}
else if (indexArgs.Count == 1)
{
// use_index expects the value with [ or ] when it's a single item array
Visit(Expression.Constant(indexArgs[0]));
}
else if (indexArgs.Count == 2)
{
Visit(indexArgsExpression);
}
else
{
throw new ArgumentException("UseIndex requires 1 or 2 strings");
}

_sb.Append(",");
return m;
}
Expand Down

0 comments on commit 9fea9b9

Please sign in to comment.