Skip to content

Commit

Permalink
Fix handling of string typed null values (#987)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
phanecak-maptiler authored Aug 22, 2024
1 parent b0118d9 commit d2efc1a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

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;

/**
* 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ void testTagValueAttributeTest() {
}, 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 -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d2efc1a

Please sign in to comment.