Skip to content

Commit 9819e46

Browse files
committed
CSHARP-1661: Refactoring.
1 parent 34f91e6 commit 9819e46

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/MongoDB.Driver/Linq/Translators/AggregateLanguageTranslator.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -977,44 +977,48 @@ private bool TryTranslateStringMethodCall(MethodCallExpression node, out BsonVal
977977
}
978978
break;
979979
case "IndexOf":
980-
var args = new BsonArray { field };
981-
if (node.Arguments[0].Type == typeof(char)
982-
&& node.Arguments[0].NodeType == ExpressionType.Constant)
980+
var indexOfArgs = new BsonArray { field };
981+
982+
if (node.Arguments.Count < 1 || node.Arguments.Count > 3)
983983
{
984-
args.Add(new BsonString(((ConstantExpression)node.Arguments[0]).Value.ToString()));
984+
return false;
985985
}
986-
else
986+
987+
if (node.Arguments[0].Type != typeof(char) && node.Arguments[0].Type != typeof(string))
988+
{
989+
return false;
990+
}
991+
var value = TranslateValue(node.Arguments[0]);
992+
if (value.BsonType == BsonType.Int32)
987993
{
988-
args.Add(TranslateValue(node.Arguments[0]));
994+
value = new BsonString(new string((char)value.AsInt32, 1));
989995
}
996+
indexOfArgs.Add(value);
990997

991998
if (node.Arguments.Count > 1)
992999
{
9931000
if (node.Arguments[1].Type != typeof(int))
9941001
{
995-
throw new NotSupportedException("The StringComparison parameter is not supported in IndexOf.");
1002+
return false;
9961003
}
9971004

998-
args.Add(TranslateValue(node.Arguments[1]));
1005+
var startIndex = TranslateValue(node.Arguments[1]);
1006+
indexOfArgs.Add(startIndex);
9991007
}
10001008

10011009
if (node.Arguments.Count > 2)
10021010
{
10031011
if (node.Arguments[2].Type != typeof(int))
10041012
{
1005-
throw new NotSupportedException("The StringComparison parameter is not supported in IndexOf.");
1013+
return false;
10061014
}
10071015

10081016
var count = TranslateValue(node.Arguments[2]);
1009-
args.Add(new BsonDocument("$add", new BsonArray { args[2], count }));
1010-
}
1011-
1012-
if (node.Arguments.Count > 3)
1013-
{
1014-
throw new NotSupportedException("The StringComparison parameter is not supported in IndexOf.");
1017+
var endIndex = new BsonDocument("$add", new BsonArray { indexOfArgs[2], count });
1018+
indexOfArgs.Add(endIndex);
10151019
}
10161020

1017-
result = new BsonDocument("$indexOfBytes", args);
1021+
result = new BsonDocument("$indexOfBytes", indexOfArgs);
10181022
return true;
10191023
case "Split":
10201024
if (node.Arguments.Count < 1 || node.Arguments.Count > 2)

0 commit comments

Comments
 (0)