Skip to content

Commit 3ef88ec

Browse files
authored
Enforced culture invariant compliance (#52)
1 parent 3e0406c commit 3ef88ec

File tree

13 files changed

+256
-117
lines changed

13 files changed

+256
-117
lines changed

src/Redis.OM.Analyzer/Redis.OM.Analyzer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
15-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0"/>
15+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.10.0" />
1616
</ItemGroup>
1717

1818

src/Redis.OM/Aggregation/AggregationPredicates/AggregateSortBy.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Globalization;
23
using Redis.OM.Searching;
34

45
namespace Redis.OM.Aggregation.AggregationPredicates
@@ -44,7 +45,8 @@ public AggregateSortBy(string property, SortDirection direction, int? max = null
4445
/// <inheritdoc/>
4546
public IEnumerable<string> Serialize()
4647
{
47-
var ret = new List<string> { "SORTBY", NumArgs.ToString(), $"@{Property}", Direction == SortDirection.Ascending ? "ASC" : "DESC" };
48+
var ret = new List<string> { "SORTBY", NumArgs.ToString(CultureInfo.InvariantCulture), $"@{Property}", Direction == SortDirection.Ascending ? "ASC" : "DESC" };
49+
4850
if (Max.HasValue)
4951
{
5052
ret.Add("MAX");

src/Redis.OM/Aggregation/AggregationPredicates/ApplyFunctions.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Redis.OM.Aggregation.AggregationPredicates
99
/// </summary>
1010
public static class ApplyFunctions
1111
{
12+
private const double RADIANTTODEGREESCONST = Math.PI / 180.0;
13+
1214
/// <summary>
1315
/// checks if the field exists on the object in redis.
1416
/// </summary>
@@ -265,10 +267,10 @@ public static double GeoDistance(double lon1, double lat1, string loc2Str)
265267
/// <returns>distance in meters.</returns>
266268
public static double GeoDistance(double longitude, double latitude, double otherLongitude, double otherLatitude)
267269
{
268-
var d1 = latitude * (Math.PI / 180.0);
269-
var num1 = longitude * (Math.PI / 180.0);
270-
var d2 = otherLatitude * (Math.PI / 180.0);
271-
var num2 = (otherLongitude * (Math.PI / 180.0)) - num1;
270+
var d1 = latitude * RADIANTTODEGREESCONST;
271+
var num1 = longitude * RADIANTTODEGREESCONST;
272+
var d2 = otherLatitude * RADIANTTODEGREESCONST;
273+
var num2 = (otherLongitude * RADIANTTODEGREESCONST) - num1;
272274
var d3 = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) + (Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0));
273275

274276
return 6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3)));

src/Redis.OM/Common/ExpressionParserUtilities.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Linq.Expressions;
56
using System.Reflection;
@@ -35,7 +36,7 @@ internal static string GetOperandString(Expression exp)
3536
{
3637
return exp switch
3738
{
38-
ConstantExpression constExp => constExp.Value.ToString(),
39+
ConstantExpression constExp => ValueToString(constExp.Value),
3940
MemberExpression member => GetOperandStringForMember(member),
4041
MethodCallExpression method when method.Method.Name == "get_Item" =>
4142
$"@{((ConstantExpression)method.Arguments[0]).Value}",
@@ -314,7 +315,7 @@ private static string GetOperandStringForMember(MemberExpression member)
314315
return sb.ToString();
315316
}
316317

317-
return resolved.ToString();
318+
return ValueToString(resolved);
318319
}
319320

320321
if (searchField != null)
@@ -331,9 +332,7 @@ private static string GetOperandStringStringArgs(Expression exp)
331332
{
332333
return exp switch
333334
{
334-
ConstantExpression constExp => constExp.Type == typeof(string)
335-
? $"\"{constExp.Value}\""
336-
: $"{constExp.Value}",
335+
ConstantExpression constExp => GetConstantStringForArgs(constExp),
337336
MemberExpression member => GetOperandStringForMember(member),
338337
MethodCallExpression method => $"@{((ConstantExpression)method.Arguments[0]).Value}",
339338
UnaryExpression unary => GetOperandString(unary.Operand),
@@ -508,7 +507,7 @@ private static string ParseSplitMethod(MethodCallExpression exp)
508507
{
509508
if (item is ConstantExpression constant)
510509
{
511-
innerArgList.Add(constant.Value.ToString());
510+
innerArgList.Add(GetConstantStringForArgs(constant));
512511
}
513512
}
514513

@@ -664,5 +663,29 @@ private static string TranslateAnyForEmbeddedObjects(MethodCallExpression exp)
664663
var tempQuery = ExpressionTranslator.TranslateBinaryExpression((BinaryExpression)lambda.Body);
665664
return tempQuery.Replace("@", $"{prefix}_");
666665
}
666+
667+
private static string ValueToString(object value)
668+
{
669+
Type valueType = value.GetType();
670+
671+
if (valueType == typeof(double) || Nullable.GetUnderlyingType(valueType) == typeof(double))
672+
{
673+
return ((double)value).ToString(CultureInfo.InvariantCulture);
674+
}
675+
676+
return value.ToString();
677+
}
678+
679+
private static string GetConstantStringForArgs(ConstantExpression constExp)
680+
{
681+
string valueAsString = ValueToString(constExp.Value);
682+
683+
if (constExp.Type == typeof(string))
684+
{
685+
return $"\"{valueAsString}\"";
686+
}
687+
688+
return $"{valueAsString}";
689+
}
667690
}
668691
}

src/Redis.OM/GeoLoc.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23

34
namespace Redis.OM.Modeling
45
{
@@ -39,7 +40,8 @@ public static GeoLoc Parse(string geolocString)
3940
var arr = geolocString.Split(',');
4041
if (arr.Length == 2)
4142
{
42-
if (double.TryParse(arr[0], out var lon) && double.TryParse(arr[1], out var lat))
43+
if (double.TryParse(arr[0], NumberStyles.Number, CultureInfo.InvariantCulture, out var lon) &&
44+
double.TryParse(arr[1], NumberStyles.Number, CultureInfo.InvariantCulture, out var lat))
4345
{
4446
return new GeoLoc(lon, lat);
4547
}

src/Redis.OM/RedisReply.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Linq;
34
using StackExchange.Redis;
45

@@ -100,7 +101,7 @@ public static implicit operator double(RedisReply v)
100101
return (double)v._internalDouble;
101102
}
102103

103-
if (v._internalString != null && double.TryParse(v._internalString, out var ret))
104+
if (v._internalString != null && double.TryParse(v._internalString, NumberStyles.Number, CultureInfo.InvariantCulture, out var ret))
104105
{
105106
return ret;
106107
}

0 commit comments

Comments
 (0)