Skip to content

Commit 2b26041

Browse files
authored
Improving parsing for GeoLocs (redis#19)
* Improving parsing for GeoLocs
1 parent b0da3fa commit 2b26041

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
---
1616

17+
[![NuGet](http://img.shields.io/nuget/v/Redis.OM.svg?style=flat-square)](https://www.nuget.org/packages/Redis.OM/)
1718
[![License][license-image]][license-url]
1819
[![Build Status][ci-svg]][ci-url]
1920

@@ -81,11 +82,11 @@ docker run -p 6379:6379 redislabs/redismod:preview
8182
With Redis OM, you can model your data and declare indexes with minimal code. For example, here's how we might model a customer object:
8283

8384
```csharp
84-
[Document(StorageType = StorageType.Json)]
85+
[Document]
8586
public class Customer
8687
{
87-
[Indexed] public string FirstName { get; set; }
88-
[Indexed] public string LastName { get; set; }
88+
[Indexed(Sortable = true)] public string FirstName { get; set; }
89+
[Indexed(Sortable = true)] public string LastName { get; set; }
8990
[Indexed] public string Email { get; set; }
9091
[Indexed(Sortable = true)] public int Age { get; set; }
9192
}

src/Redis.OM/GeoLoc.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Redis.OM.Modeling
55
/// <summary>
66
/// A structure representing a point on the globe by it's longitude and latitude.
77
/// </summary>
8-
public readonly struct GeoLoc
8+
public struct GeoLoc
99
{
1010
/// <summary>
1111
/// Initializes a new instance of the <see cref="GeoLoc"/> struct.
@@ -19,14 +19,14 @@ public GeoLoc(double longitude, double latitude)
1919
}
2020

2121
/// <summary>
22-
/// Gets the longitude.
22+
/// Gets or sets the longitude.
2323
/// </summary>
24-
public double Longitude { get; }
24+
public double Longitude { get; set; }
2525

2626
/// <summary>
27-
/// Gets the latitude.
27+
/// Gets or sets the latitude.
2828
/// </summary>
29-
public double Latitude { get; }
29+
public double Latitude { get; set; }
3030

3131
/// <summary>
3232
/// Parses a Geolocation from a string.

src/Redis.OM/Redis.OM.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
<RootNamespace>Redis.OM</RootNamespace>
77
<Nullable>enable</Nullable>
88
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
9-
<PackageVersion>0.1.0</PackageVersion>
9+
<PackageVersion>0.1.1</PackageVersion>
10+
<Version>0.1.1</Version>
11+
<Description>Object Mapping and More for Redis</Description>
1012
<Title>Redis OM</Title>
1113
<Authors>Steve Lorello</Authors>
1214
<Copyright>Redis Inc</Copyright>

src/Redis.OM/RedisObjectHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private static string SendToJson(IDictionary<string, string> hash, Type t)
253253
{
254254
ret += $"\"{propertyName}\":{hash[propertyName]},";
255255
}
256-
else if (type == typeof(string))
256+
else if (type == typeof(string) || type == typeof(GeoLoc))
257257
{
258258
ret += $"\"{propertyName}\":\"{hash[propertyName]}\",";
259259
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Redis.OM.Modeling;
2+
3+
namespace Redis.OM.Unit.Tests
4+
{
5+
public class BasicTypeWithGeoLoc
6+
{
7+
public string Name { get; set; }
8+
public GeoLoc Location { get; set; }
9+
}
10+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json;
3+
using Xunit;
4+
5+
namespace Redis.OM.Unit.Tests
6+
{
7+
public class GeoLocTests
8+
{
9+
[Fact]
10+
public void TestParsingFromJson()
11+
{
12+
var str = "{\"Name\":\"Foo\", \"Location\":{\"Longitude\":32.5,\"Latitude\":22.4}}";
13+
var basicType = JsonSerializer.Deserialize<BasicTypeWithGeoLoc>(str);
14+
Assert.Equal("Foo",basicType.Name);
15+
Assert.Equal(32.5, basicType.Location.Longitude);
16+
Assert.Equal(22.4, basicType.Location.Latitude);
17+
}
18+
19+
[Fact]
20+
public void TestParsingFromFormattedHash()
21+
{
22+
var hash = new Dictionary<string, string>
23+
{
24+
{"Name", "Foo"},
25+
{"Location", "32.5,22.4"}
26+
};
27+
28+
var basicType = RedisObjectHandler.FromHashSet<BasicTypeWithGeoLoc>(hash);
29+
Assert.Equal("Foo",basicType.Name);
30+
Assert.Equal(32.5, basicType.Location.Longitude);
31+
Assert.Equal(22.4, basicType.Location.Latitude);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)