Skip to content

Commit 5d419fe

Browse files
committed
Base class for geoshape filter and query converters
1 parent bbc0861 commit 5d419fe

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
lines changed

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,7 @@
676676
<Compile Include="DSL\Filter\LimitFilterDescriptor.cs" />
677677
<Compile Include="DSL\Filter\IdsFilterDescriptor.cs" />
678678
<Compile Include="DSL\Filter\ExistsFilterDescriptor.cs" />
679+
<Compile Include="Resolvers\Converters\GeoShapeConverterBase.cs" />
679680
<Compile Include="Resolvers\Converters\Queries\GeoShapeQueryJsonReader.cs" />
680681
<Compile Include="Resolvers\Converters\SimilarityCollectionConverter.cs" />
681682
<Compile Include="Resolvers\Converters\FuzzinessConverter.cs" />

src/Nest/Resolvers/Converters/Filters/GeoShapeFilterJsonReader.cs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Nest.Resolvers.Converters.Filters
1010
{
11-
public class GeoShapeFilterJsonReader : JsonConverter
11+
public class GeoShapeFilterJsonReader : GeoShapeConverterBase
1212
{
1313
public override bool CanRead { get { return true; } }
1414
public override bool CanWrite { get { return false; } }
@@ -57,82 +57,66 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
5757
{
5858
IGeoShapeCircleFilter f = new GeoShapeCircleFilterDescriptor();
5959
f.Shape = new CircleGeoShape();
60+
f.Shape.Coordinates = GetCoordinates<IEnumerable<double>>(shape);
6061
var radius = shape["radius"];
6162
if (radius != null)
6263
f.Shape.Radius = radius.Value<string>();
63-
var coordinates = shape["coordinates"];
64-
if (coordinates != null)
65-
f.Shape.Coordinates = coordinates.Values<double>();
6664
filter = f;
6765
break;
6866
}
6967
else if (typeName == "envelope")
7068
{
7169
IGeoShapeEnvelopeFilter f = new GeoShapeEnvelopeFilterDescriptor();
7270
f.Shape = new EnvelopeGeoShape();
73-
var coordinates = shape["coordinates"];
74-
if (coordinates != null)
75-
f.Shape.Coordinates = coordinates.Values<double[]>();
71+
f.Shape.Coordinates = GetCoordinates<IEnumerable<IEnumerable<double>>>(shape);
7672
filter = f;
7773
break;
7874
}
7975
else if (typeName == "linestring")
8076
{
8177
IGeoShapeLineStringFilter f = new GeoShapeLineStringFilterDescriptor();
8278
f.Shape = new LineStringGeoShape();
83-
var coordinates = shape["coordinates"];
84-
if (coordinates != null)
85-
f.Shape.Coordinates = coordinates.Values<double[]>();
79+
f.Shape.Coordinates = GetCoordinates<IEnumerable<IEnumerable<double>>>(shape);
8680
filter = f;
8781
break;
8882
}
8983
else if (typeName == "multilinestring")
9084
{
9185
IGeoShapeMultiLineStringFilter f = new GeoShapeMultiLineStringFilterDescriptor();
9286
f.Shape = new MultiLineStringGeoShape();
93-
var coordinates = shape["coordinates"];
94-
if (coordinates != null)
95-
f.Shape.Coordinates = coordinates.Values<double[][]>();
87+
f.Shape.Coordinates = GetCoordinates<IEnumerable<IEnumerable<IEnumerable<double>>>>(shape);
9688
filter = f;
9789
break;
9890
}
9991
else if (typeName == "point")
10092
{
10193
IGeoShapePointFilter f = new GeoShapePointFilterDescriptor();
10294
f.Shape = new PointGeoShape();
103-
var coordinates = shape["coordinates"];
104-
if (coordinates != null)
105-
f.Shape.Coordinates = coordinates.Values<double>();
95+
f.Shape.Coordinates = GetCoordinates<IEnumerable<double>>(shape);
10696
filter = f;
10797
break;
10898
}
10999
else if (typeName == "multipoint")
110100
{
111101
IGeoShapeMultiPointFilter f = new GeoShapeMultiPointFilterDescriptor();
112102
f.Shape = new MultiPointGeoShape();
113-
var coordinates = shape["coordinates"];
114-
if (coordinates != null)
115-
f.Shape.Coordinates = coordinates.Values<double[]>();
103+
f.Shape.Coordinates = GetCoordinates<IEnumerable<IEnumerable<double>>>(shape);
116104
filter = f;
117105
break;
118106
}
119107
else if (typeName == "polygon")
120108
{
121109
IGeoShapePolygonFilter f = new GeoShapePolygonFilterDescriptor();
122110
f.Shape = new PolygonGeoShape();
123-
var coordinates = shape["coordinates"];
124-
if (coordinates != null)
125-
f.Shape.Coordinates = coordinates.Values<double[][]>();
111+
f.Shape.Coordinates = GetCoordinates<IEnumerable<IEnumerable<IEnumerable<double>>>>(shape);
126112
filter = f;
127113
break;
128114
}
129115
else if (typeName == "multipolygon")
130116
{
131117
IGeoShapeMultiPolygonFilter f = new GeoShapeMultiPolygonFilterDescriptor();
132118
f.Shape = new MultiPolygonGeoShape();
133-
var coordinates = shape["coordinates"];
134-
if (coordinates != null)
135-
f.Shape.Coordinates = coordinates.Values<double[][][]>();
119+
f.Shape.Coordinates = GetCoordinates<IEnumerable<IEnumerable<IEnumerable<IEnumerable<double>>>>>(shape);
136120
filter = f;
137121
break;
138122
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace Nest.Resolvers.Converters
9+
{
10+
public abstract class GeoShapeConverterBase : JsonConverter
11+
{
12+
public virtual T GetCoordinates<T>(JToken shape)
13+
{
14+
var coordinates = shape["coordinates"];
15+
if (coordinates != null)
16+
return coordinates.ToObject<T>();
17+
return default(T);
18+
}
19+
}
20+
}

src/Nest/Resolvers/Converters/Queries/GeoShapeQueryJsonReader.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Newtonsoft.Json;
1+
using Nest.Resolvers.Converters;
2+
using Newtonsoft.Json;
23
using Newtonsoft.Json.Linq;
34
using System;
45
using System.Collections.Generic;
@@ -7,7 +8,7 @@
78

89
namespace Nest
910
{
10-
public class GeoShapeQueryJsonReader : JsonConverter
11+
public class GeoShapeQueryJsonReader : GeoShapeConverterBase
1112
{
1213
public override bool CanConvert(Type objectType)
1314
{
@@ -106,14 +107,6 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
106107
return query;
107108
}
108109

109-
private T GetCoordinates<T>(JToken shape)
110-
{
111-
var coordinates = shape["coordinates"];
112-
if (coordinates != null)
113-
return coordinates.ToObject<T>();
114-
return default(T);
115-
}
116-
117110
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
118111
{
119112
}

0 commit comments

Comments
 (0)