Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Support double value in string format for GeoPoint
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Huo <penghuo@gmail.com>
  • Loading branch information
penghuo committed Mar 5, 2021
1 parent 4afff39 commit 4e196f5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,29 @@ public Object objectValue() {

@Override
public Pair<Double, Double> geoValue() {
return Pair.of(value().get("lat").doubleValue(), value().get("lon").doubleValue());
final JsonNode value = value();
if (value.has("lat") && value.has("lon")) {
Double lat = 0d;
Double lon = 0d;
try {
lat = extractDoubleValue(value.get("lat"));
} catch (Exception exception) {
throw new IllegalStateException(
"latitude must be number value, but got value: " + value.get(
"lat"));
}
try {
lon = extractDoubleValue(value.get("lon"));
} catch (Exception exception) {
throw new IllegalStateException(
"longitude must be number value, but got value: " + value.get(
"lon"));
}
return Pair.of(lat, lon);
} else {
throw new IllegalStateException("geo point must in format of {\"lat\": number, \"lon\": "
+ "number}");
}
}

/**
Expand All @@ -120,4 +142,18 @@ public Pair<Double, Double> geoValue() {
private JsonNode value() {
return value.isArray() ? value.get(0) : value;
}

/**
* Get doubleValue from JsonNode if possible.
*/
private Double extractDoubleValue(JsonNode node) {
if (node.isTextual()) {
return Double.valueOf(node.textValue());
}
if (node.isNumber()) {
return node.doubleValue();
} else {
throw new IllegalStateException("node must be a number");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,43 @@ public void constructIP() {
public void constructGeoPoint() {
assertEquals(new ElasticsearchExprGeoPointValue(42.60355556, -97.25263889),
tupleValue("{\"geoV\":{\"lat\":42.60355556,\"lon\":-97.25263889}}").get("geoV"));
assertEquals(new ElasticsearchExprGeoPointValue(42.60355556, -97.25263889),
tupleValue("{\"geoV\":{\"lat\":\"42.60355556\",\"lon\":\"-97.25263889\"}}").get("geoV"));
assertEquals(new ElasticsearchExprGeoPointValue(42.60355556, -97.25263889),
constructFromObject("geoV", "42.60355556,-97.25263889"));
}

@Test
public void constructGeoPointFromUnsupportedFormatShouldThrowException() {
IllegalStateException exception =
assertThrows(IllegalStateException.class,
() -> tupleValue("{\"geoV\":[42.60355556,-97.25263889]}").get("geoV"));
assertEquals("geo point must in format of {\"lat\": number, \"lon\": number}",
exception.getMessage());

exception =
assertThrows(IllegalStateException.class,
() -> tupleValue("{\"geoV\":{\"lon\":-97.25263889}}").get("geoV"));
assertEquals("geo point must in format of {\"lat\": number, \"lon\": number}",
exception.getMessage());

exception =
assertThrows(IllegalStateException.class,
() -> tupleValue("{\"geoV\":{\"lat\":-97.25263889}}").get("geoV"));
assertEquals("geo point must in format of {\"lat\": number, \"lon\": number}",
exception.getMessage());

exception =
assertThrows(IllegalStateException.class,
() -> tupleValue("{\"geoV\":{\"lat\":true,\"lon\":-97.25263889}}").get("geoV"));
assertEquals("latitude must be number value, but got value: true", exception.getMessage());

exception =
assertThrows(IllegalStateException.class,
() -> tupleValue("{\"geoV\":{\"lat\":42.60355556,\"lon\":false}}").get("geoV"));
assertEquals("longitude must be number value, but got value: false", exception.getMessage());
}

@Test
public void constructBinary() {
assertEquals(new ElasticsearchExprBinaryValue("U29tZSBiaW5hcnkgYmxvYg=="),
Expand All @@ -298,7 +331,7 @@ public void constructBinary() {
* https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html.
*/
@Test
public void constructFromElasticsearcyArrayReturnFirstElement() {
public void constructFromElasticsearchArrayReturnFirstElement() {
assertEquals(integerValue(1), tupleValue("{\"intV\":[1, 2, 3]}").get("intV"));
assertEquals(new ExprTupleValue(
new LinkedHashMap<String, ExprValue>() {
Expand Down

0 comments on commit 4e196f5

Please sign in to comment.