Skip to content

Commit 9bdd82b

Browse files
committed
Support serialization of geo shapes on documents (#3208)
* Support serialization of geo shapes on documents Fixes #3100 Fixes #3096 * GeometryCollection implements IGeoShape This commit changes GeometryCollection to implement IGeoShape, to allow a document to have an IGeoShape member and accept a GeometryCollection. IGeoShape.Type is implemented explicitly so that the existing Type member continues to be the implicit implementation of IGeometryCollection. The ideal scenario would have been for IGeometryCollection to implement IGeoShape however this would break backwards binary compatibility, since both interfaces have a Type member. (cherry picked from commit 96c0d4d) This commit includes changes from PR #3214, which were required to address incorrect serialization introduced in 6.x
1 parent 4663659 commit 9bdd82b

File tree

17 files changed

+529
-197
lines changed

17 files changed

+529
-197
lines changed

.paket/Paket.Restore.targets

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@
4848

4949
<!-- Because ReadAllText is slow on osx/linux, try to find shasum and awk -->
5050
<PropertyGroup>
51-
<PaketRestoreCachedHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreCachedHasher)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum $(PaketRestoreCacheFile) | /usr/bin/awk '{ print $1 }'</PaketRestoreCachedHasher>
52-
<PaketRestoreLockFileHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreLockFileHash)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum $(PaketLockFilePath) | /usr/bin/awk '{ print $1 }'</PaketRestoreLockFileHasher>
51+
<PaketRestoreCachedHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreCachedHasher)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum $(PaketRestoreCacheFile) | /usr/bin/awk '{ print $1 }'</PaketRestoreCachedHasher>
52+
<PaketRestoreLockFileHasher Condition="'$(OS)' != 'Windows_NT' And '$(PaketRestoreLockFileHash)' == '' And Exists('/usr/bin/shasum') And Exists('/usr/bin/awk')">/usr/bin/shasum $(PaketLockFilePath) | /usr/bin/awk '{ print $1 }'</PaketRestoreLockFileHasher>
5353
</PropertyGroup>
5454

5555
<!-- If shasum and awk exist get the hashes -->
5656
<Exec StandardOutputImportance="Low" Condition=" '$(PaketRestoreCachedHasher)' != '' " Command="$(PaketRestoreCachedHasher)" ConsoleToMSBuild='true'>
57-
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreCachedHash" />
57+
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreCachedHash" />
5858
</Exec>
5959
<Exec StandardOutputImportance="Low" Condition=" '$(PaketRestoreLockFileHasher)' != '' " Command="$(PaketRestoreLockFileHasher)" ConsoleToMSBuild='true'>
60-
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreLockFileHash" />
60+
<Output TaskParameter="ConsoleOutput" PropertyName="PaketRestoreLockFileHash" />
6161
</Exec>
6262

6363
<PropertyGroup Condition="Exists('$(PaketRestoreCacheFile)') ">
@@ -127,6 +127,7 @@
127127
<PackageReference Include="%(PaketReferencesFileLinesInfo.PackageName)">
128128
<Version>%(PaketReferencesFileLinesInfo.PackageVersion)</Version>
129129
<PrivateAssets Condition="%(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'true'">All</PrivateAssets>
130+
<ExcludeAssets Condition="%(PaketReferencesFileLinesInfo.AllPrivateAssets) == 'exclude'">runtime</ExcludeAssets>
130131
</PackageReference>
131132
</ItemGroup>
132133

@@ -183,8 +184,8 @@
183184

184185
<ConvertToAbsolutePath Condition="@(_NuspecFiles) != ''" Paths="@(_NuspecFiles)">
185186
<Output TaskParameter="AbsolutePaths" PropertyName="NuspecFileAbsolutePath" />
186-
</ConvertToAbsolutePath>
187-
187+
</ConvertToAbsolutePath>
188+
188189

189190
<!-- Call Pack -->
190191
<PackTask Condition="$(UseNewPack)"

build/scripts/Commandline.fsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Targets:
3232
* diff <github|nuget|directories|assemblies> <version|path 1> <version|path 2> [format]
3333
3434
NOTE: both the `test` and `integrate` targets can be suffixed with `-all` to force the tests against all suported TFM's
35+
36+
Execution hints can be provided anywhere on the command line
37+
- skiptests : skip running tests as part of the target chain
38+
- skipdocs : skip generating documentation
3539
"""
3640

3741
module Commandline =
@@ -41,7 +45,7 @@ module Commandline =
4145
let skipTests = args |> List.exists (fun x -> x = "skiptests")
4246
let skipDocs = args |> List.exists (fun x -> x = "skipdocs") || isMono
4347
let private filteredArgs = args |> List.filter (fun x -> x <> "skiptests" && x <> "skipdocs")
44-
48+
4549
let multiTarget =
4650
match (filteredArgs |> List.tryHead) with
4751
| Some t when t.EndsWith("-all") -> MultiTarget.All
@@ -69,7 +73,7 @@ module Commandline =
6973
match Uri.TryCreate(candidate, UriKind.RelativeOrAbsolute) with
7074
| true, _ -> Some candidate
7175
| _ -> None
72-
76+
7377
let private (|IsDiff|_|) (candidate:string) =
7478
let c = candidate |> toLower
7579
match c with
@@ -150,7 +154,7 @@ module Commandline =
150154
setBuildParam "esversions" esVersions
151155
setBuildParam "clusterfilter" "ConnectionReuse"
152156
setBuildParam "numberOfConnections" numberOfConnections
153-
157+
154158
| ["diff"; IsDiff diffType; IsProject project; firstVersionOrPath; secondVersionOrPath; IsFormat format] ->
155159
setBuildParam "diffType" diffType
156160
setBuildParam "project" project

src/Nest/CommonAbstractions/SerializationBehavior/GenericJsonConverters/ReserializeJsonConverter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ protected virtual object DeserializeJson(JsonReader reader, Type objectType, obj
3131

3232
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
3333
{
34-
var v = value as TInterface;
35-
if (v == null) return;
34+
if (!(value is TInterface v)) return;
3635
this.SerializeJson(writer, value, v, serializer);
3736
}
3837

@@ -41,6 +40,8 @@ protected virtual void SerializeJson(JsonWriter writer, object value, TInterface
4140
this.Reserialize(writer, value, serializer);
4241
}
4342

43+
protected virtual bool SkipWriteProperty(string propertyName) => false;
44+
4445
protected void Reserialize(JsonWriter writer, object value, JsonSerializer serializer, Action<JsonWriter> inlineWriter = null)
4546
{
4647
var properties = value.GetType().GetCachedObjectProperties();
@@ -49,7 +50,7 @@ protected void Reserialize(JsonWriter writer, object value, JsonSerializer seria
4950
inlineWriter?.Invoke(writer);
5051
foreach (var p in properties)
5152
{
52-
if (p.Ignored) continue;
53+
if (p.Ignored || SkipWriteProperty(p.PropertyName)) continue;
5354
var vv = p.ValueProvider.GetValue(value);
5455
if (vv == null) continue;
5556
writer.WritePropertyName(p.PropertyName);

src/Nest/QueryDsl/Geo/GeoLocation.cs

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Nest
1010
{
11-
1211
/// <summary>
1312
/// Represents a Latitude/Longitude as a 2 dimensional point that gets serialized as { lat, lon }
1413
/// </summary>
@@ -18,18 +17,16 @@ public class GeoLocation : IEquatable<GeoLocation>, IFormattable
1817
/// Latitude
1918
/// </summary>
2019
[JsonProperty("lat")]
21-
public double Latitude => _latitude;
22-
private readonly double _latitude;
20+
public double Latitude { get; }
2321

2422
/// <summary>
2523
/// Longitude
2624
/// </summary>
2725
[JsonProperty("lon")]
28-
public double Longitude => _longitude;
29-
private readonly double _longitude;
26+
public double Longitude { get; }
3027

3128
/// <summary>
32-
/// Represents a Latitude/Longitude as a 2 dimensional point.
29+
/// Represents a Latitude/Longitude as a 2 dimensional point.
3330
/// </summary>
3431
/// <param name="latitude">Value between -90 and 90</param>
3532
/// <param name="longitude">Value between -180 and 180</param>
@@ -42,32 +39,26 @@ public GeoLocation(double latitude, double longitude)
4239
if (!IsValidLongitude(longitude))
4340
throw new ArgumentOutOfRangeException(string.Format(CultureInfo.InvariantCulture,
4441
"Invalid longitude '{0}'. Valid values are between -180 and 180", longitude));
45-
_latitude = latitude;
46-
_longitude = longitude;
42+
Latitude = latitude;
43+
Longitude = longitude;
4744
}
4845

4946
/// <summary>
5047
/// True if <paramref name="latitude"/> is a valid latitude. Otherwise false.
5148
/// </summary>
5249
/// <param name="latitude"></param>
5350
/// <returns></returns>
54-
public static bool IsValidLatitude(double latitude)
55-
{
56-
return latitude >= -90 && latitude <= 90;
57-
}
51+
public static bool IsValidLatitude(double latitude) => latitude >= -90 && latitude <= 90;
5852

5953
/// <summary>
6054
/// True if <paramref name="longitude"/> is a valid longitude. Otherwise false.
6155
/// </summary>
6256
/// <param name="longitude"></param>
6357
/// <returns></returns>
64-
public static bool IsValidLongitude(double longitude)
65-
{
66-
return longitude >= -180 && longitude <= 180;
67-
}
58+
public static bool IsValidLongitude(double longitude) => longitude >= -180 && longitude <= 180;
6859

6960
/// <summary>
70-
/// Try to create a <see cref="GeoLocation"/>.
61+
/// Try to create a <see cref="GeoLocation"/>.
7162
/// Return <value>null</value> if either <paramref name="latitude"/> or <paramref name="longitude"/> are invalid.
7263
/// </summary>
7364
/// <param name="latitude">Value between -90 and 90</param>
@@ -80,18 +71,17 @@ public static GeoLocation TryCreate(double latitude, double longitude)
8071
return null;
8172
}
8273

83-
public override string ToString()
84-
{
85-
return _latitude.ToString("#0.0#######", CultureInfo.InvariantCulture) + "," + _longitude.ToString("#0.0#######", CultureInfo.InvariantCulture);
86-
}
74+
public override string ToString() =>
75+
Latitude.ToString("#0.0#######", CultureInfo.InvariantCulture) + "," +
76+
Longitude.ToString("#0.0#######", CultureInfo.InvariantCulture);
8777

8878
public bool Equals(GeoLocation other)
8979
{
9080
if (ReferenceEquals(null, other))
9181
return false;
9282
if (ReferenceEquals(this, other))
9383
return true;
94-
return _latitude.Equals(other._latitude) && _longitude.Equals(other._longitude);
84+
return Latitude.Equals(other.Latitude) && Longitude.Equals(other.Longitude);
9585
}
9686

9787
public override bool Equals(object obj)
@@ -106,7 +96,7 @@ public override bool Equals(object obj)
10696
}
10797

10898
public override int GetHashCode() =>
109-
unchecked((_latitude.GetHashCode() * 397) ^ _longitude.GetHashCode());
99+
unchecked((Latitude.GetHashCode() * 397) ^ Longitude.GetHashCode());
110100

111101
public string ToString(string format, IFormatProvider formatProvider) => ToString();
112102

@@ -117,21 +107,18 @@ public static implicit operator GeoLocation(string latLon)
117107

118108
var parts = latLon.Split(',');
119109
if (parts.Length != 2) throw new ArgumentException("Invalid format: string must be in the form of lat,lon");
120-
double lat;
121-
if (!double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out lat))
110+
if (!double.TryParse(parts[0], NumberStyles.Any, CultureInfo.InvariantCulture, out var lat))
122111
throw new ArgumentException("Invalid latitude value");
123-
double lon;
124-
if (!double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out lon))
112+
if (!double.TryParse(parts[1], NumberStyles.Any, CultureInfo.InvariantCulture, out var lon))
125113
throw new ArgumentException("Invalid longitude value");
126114
return new GeoLocation(lat, lon);
127115
}
128116

129117
public static implicit operator GeoLocation(double[] lonLat)
130118
{
131-
if (lonLat.Length != 2)
132-
return null;
133-
134-
return new GeoLocation(lonLat[1], lonLat[0]);
119+
return lonLat.Length != 2
120+
? null
121+
: new GeoLocation(lonLat[1], lonLat[0]);
135122
}
136123
}
137124

@@ -141,14 +128,21 @@ public static implicit operator GeoLocation(double[] lonLat)
141128
[JsonConverter(typeof(GeoCoordinateJsonConverter))]
142129
public class GeoCoordinate : GeoLocation
143130
{
144-
public GeoCoordinate(double latitude, double longitude) : base(latitude, longitude)
145-
{
146-
}
131+
/// <summary>
132+
/// Creates a new instance of <see cref="GeoCoordinate"/>
133+
/// </summary>
134+
public GeoCoordinate(double latitude, double longitude) : base(latitude, longitude) { }
147135

136+
/// <summary>
137+
/// Creates a new instance of <see cref="GeoCoordinate"/> from a pair of coordinates
138+
/// in the order Latitude then Longitude.
139+
/// </summary>
148140
public static implicit operator GeoCoordinate(double[] coordinates)
149141
{
150142
if (coordinates == null || coordinates.Length != 2)
151-
throw new ArgumentOutOfRangeException(nameof(coordinates), "Can not create a GeoCoordinate from an array that does not have two doubles");
143+
throw new ArgumentOutOfRangeException(
144+
nameof(coordinates),
145+
$"Can not create a {nameof(GeoCoordinate)} from an array that does not have two doubles");
152146

153147
return new GeoCoordinate(coordinates[0], coordinates[1]);
154148
}

src/Nest/QueryDsl/Geo/Shape/Envelope/EnvelopeGeoShape.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ public class EnvelopeGeoShape : GeoShapeBase, IEnvelopeGeoShape
1313
{
1414
public EnvelopeGeoShape() : this(null) { }
1515

16-
public EnvelopeGeoShape(IEnumerable<GeoCoordinate> coordinates)
17-
: base("envelope")
18-
{
16+
public EnvelopeGeoShape(IEnumerable<GeoCoordinate> coordinates) : base("envelope") =>
1917
this.Coordinates = coordinates;
20-
}
2118

2219
public IEnumerable<GeoCoordinate> Coordinates { get; set; }
2320
}

0 commit comments

Comments
 (0)