Skip to content

Commit 58a55ea

Browse files
committed
Add RedisGeo
1 parent 4a3b418 commit 58a55ea

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

lib/ServiceStack.Interfaces.dll

1 KB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Globalization;
3+
4+
namespace ServiceStack.Redis
5+
{
6+
public struct RedisGeo
7+
{
8+
public double Longitude;
9+
public double Latitude;
10+
public string Member;
11+
12+
public RedisGeo(double longitude, double latitude, string member)
13+
{
14+
Longitude = longitude;
15+
Latitude = latitude;
16+
Member = member;
17+
}
18+
19+
public RedisGeo(string geoString) : this()
20+
{
21+
if (string.IsNullOrEmpty(geoString))
22+
throw new ArgumentNullException("geoString");
23+
24+
var pos1 = geoString.IndexOf(' ');
25+
if (pos1 == -1)
26+
throw new ArgumentException("Invalid geoString: " + geoString);
27+
Longitude = double.Parse(geoString.Substring(0, pos1));
28+
29+
var pos2 = geoString.IndexOf(' ', pos1 + 1);
30+
if (pos2 == -1)
31+
throw new ArgumentException("Invalid geoString: " + geoString);
32+
Latitude = double.Parse(geoString.Substring(pos1, pos2 - pos1));
33+
34+
Member = geoString.Substring(pos2 + 1);
35+
}
36+
37+
public override string ToString()
38+
{
39+
return Longitude.ToString(CultureInfo.InvariantCulture)
40+
+ " "
41+
+ Latitude.ToString(CultureInfo.InvariantCulture)
42+
+ " "
43+
+ Member;
44+
}
45+
46+
public bool Equals(RedisGeo other)
47+
{
48+
return Longitude.Equals(other.Longitude)
49+
&& Latitude.Equals(other.Latitude)
50+
&& string.Equals(Member, other.Member);
51+
}
52+
53+
public override bool Equals(object obj)
54+
{
55+
if (ReferenceEquals(null, obj)) return false;
56+
return obj is RedisGeo && Equals((RedisGeo) obj);
57+
}
58+
59+
public override int GetHashCode()
60+
{
61+
unchecked
62+
{
63+
var hashCode = Longitude.GetHashCode();
64+
hashCode = (hashCode*397) ^ Latitude.GetHashCode();
65+
hashCode = (hashCode*397) ^ (Member != null ? Member.GetHashCode() : 0);
66+
return hashCode;
67+
}
68+
}
69+
}
70+
}

src/ServiceStack.Interfaces/ServiceStack.Interfaces.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<Compile Include="IEncryptedClient.cs" />
150150
<Compile Include="ISequenceSource.cs" />
151151
<Compile Include="IServiceGateway.cs" />
152+
<Compile Include="Redis\RedisGeo.cs" />
152153
<Compile Include="Web\IServiceGatewayFactory.cs" />
153154
<Compile Include="IUrlFilter.cs" />
154155
<Compile Include="Logging\GenericLogFactory.cs" />
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using NUnit.Framework;
2+
using ServiceStack.Redis;
3+
4+
namespace ServiceStack.Common.Tests
5+
{
6+
[TestFixture]
7+
public class RedisTypeTests
8+
{
9+
[Test]
10+
public void Can_parse_RedisGeo()
11+
{
12+
var palermo = new RedisGeo(13.361389, 38.115556, "Palermo");
13+
var geoString = palermo.ToString();
14+
Assert.That(geoString, Is.EqualTo("13.361389 38.115556 Palermo"));
15+
Assert.That(new RedisGeo(geoString), Is.EqualTo(palermo));
16+
}
17+
}
18+
}

tests/ServiceStack.Common.Tests/ServiceStack.Common.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
<DependentUpon>Settings.settings</DependentUpon>
212212
</Compile>
213213
<Compile Include="QueryStringSerializerTests.cs" />
214+
<Compile Include="RedisTypeTests.cs" />
214215
<Compile Include="ReflectionExtensionsTests.cs" />
215216
<Compile Include="Reflection\PropertyAccessorTests.cs" />
216217
<Compile Include="Expressions\DelegateFactoryTests.cs" />

0 commit comments

Comments
 (0)