Fix handling of string typed null values (#987)

* Added unit test for handling of missing (e.g. null) values

- When missing item is NOT typed, than it is properly handld as null,
  e.g. it is NOT included in the generated feature
- When missing item is typed to 'string', it gets converted into "null"
  string value hence generated featire has attribute
  non_existent_typed=null

* Fixed handling of missing or null values for attributes with type 'string'

* clean-up: removed unused import
pull/994/head
Peter Hanecak 2024-08-22 12:35:36 +02:00 zatwierdzone przez GitHub
rodzic b0118d9ed8
commit d2efc1a4fe
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
4 zmienionych plików z 34 dodań i 2 usunięć

Wyświetl plik

@ -2,7 +2,6 @@ package com.onthegomap.planetiler.expression;
import com.onthegomap.planetiler.reader.WithTags;
import com.onthegomap.planetiler.util.Parse;
import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
@ -10,7 +9,7 @@ import java.util.function.UnaryOperator;
* Destination data types for an attribute that link the type to functions that can parse the value from an input object
*/
public enum DataType implements BiFunction<WithTags, String, Object> {
GET_STRING("string", WithTags::getString, Objects::toString),
GET_STRING("string", WithTags::getString, Parse::parseStringOrNull),
GET_BOOLEAN("boolean", WithTags::getBoolean, Parse::bool),
GET_DIRECTION("direction", WithTags::getDirection, Parse::direction),
GET_LONG("long", WithTags::getLong, Parse::parseLongOrNull),

Wyświetl plik

@ -31,6 +31,11 @@ public class Parse {
private Parse() {}
/** Returns {@code tag} as a string or null if null. */
public static String parseStringOrNull(Object tag) {
return tag == null ? null : tag.toString();
}
/** Returns {@code tag} as a long or null if invalid. */
public static Long parseLongOrNull(Object tag) {
return tag == null ? null : tag instanceof Number number ? Long.valueOf(number.longValue()) :

Wyświetl plik

@ -267,6 +267,15 @@ class ConfiguredFeatureTest {
}, 1);
}
@Test
void testTagNullValueAttributeTest() {
testPolygon(TEST_RESOURCE, "tag_attribute_null.yml", waterTags, f -> {
var attr = f.getAttrsAtZoom(14);
assertNull(attr.get("non_existent"));
assertNull(attr.get("non_existent_typed"));
}, 1);
}
@Test
void testTagIncludeAttributeTest() {
testPolygon(TEST_RESOURCE, "tag_include.yml", waterTags, f -> {

Wyświetl plik

@ -0,0 +1,19 @@
schema_name: Test Case Schema
schema_description: Test case tile schema
attribution: Test attribution
sources:
osm:
type: osm
url: geofabrik:rhode-island
layers:
- id: testLayer
features:
- source:
- osm
geometry: polygon
include_when:
natural: water
attributes:
- key: non_existent
- key: non_existent_typed
type: string