Data type support for Expression / MultiExpression (#190)

This commit is contained in:
Brian Sperlongano
2022-04-28 07:08:00 -04:00
committed by GitHub
parent 3500ee0240
commit b790ad8cd6
3 changed files with 14 additions and 7 deletions

View File

@@ -431,7 +431,7 @@ public class Generate {
/** Imposm3 "mapping" to filter OSM elements that should appear in this "table". */ /** Imposm3 "mapping" to filter OSM elements that should appear in this "table". */
public static final Expression MAPPING = %s; public static final Expression MAPPING = %s;
""".formatted( """.formatted(
mappingExpression mappingExpression.generateJavaCode()
); );
String tableName = "osm_" + key; String tableName = "osm_" + key;
String className = lowerUnderscoreToUpperCamel(tableName); String className = lowerUnderscoreToUpperCamel(tableName);
@@ -652,7 +652,7 @@ public class Generate {
/** Returns java code that will recreate an {@link MultiExpression} identical to {@code mapping}. */ /** Returns java code that will recreate an {@link MultiExpression} identical to {@code mapping}. */
private static String generateJavaCode(MultiExpression<String> mapping) { private static String generateJavaCode(MultiExpression<String> mapping) {
return "MultiExpression.of(List.of(" + mapping.expressions().stream() return "MultiExpression.of(List.of(" + mapping.expressions().stream()
.map(s -> "MultiExpression.entry(%s, %s)".formatted(Format.quote(s.result()), s.expression())) .map(s -> "MultiExpression.entry(%s, %s)".formatted(Format.quote(s.result()), s.expression().generateJavaCode()))
.collect(joining(", ")) + "))"; .collect(joining(", ")) + "))";
} }

View File

@@ -5,6 +5,7 @@ import static com.onthegomap.planetiler.TestUtils.newLineString;
import static com.onthegomap.planetiler.TestUtils.newPoint; import static com.onthegomap.planetiler.TestUtils.newPoint;
import static com.onthegomap.planetiler.TestUtils.rectangle; import static com.onthegomap.planetiler.TestUtils.rectangle;
import static com.onthegomap.planetiler.basemap.BasemapProfile.OSM_SOURCE; import static com.onthegomap.planetiler.basemap.BasemapProfile.OSM_SOURCE;
import static com.onthegomap.planetiler.basemap.util.Utils.coalesce;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@@ -23,6 +24,7 @@ import com.onthegomap.planetiler.stats.Stats;
import com.onthegomap.planetiler.util.Translations; import com.onthegomap.planetiler.util.Translations;
import com.onthegomap.planetiler.util.Wikidata; import com.onthegomap.planetiler.util.Wikidata;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -41,10 +43,15 @@ public abstract class AbstractLayerTest {
final FeatureCollector.Factory featureCollectorFactory = new FeatureCollector.Factory(params, stats); final FeatureCollector.Factory featureCollectorFactory = new FeatureCollector.Factory(params, stats);
static void assertFeatures(int zoom, List<Map<String, Object>> expected, Iterable<FeatureCollector.Feature> actual) { static void assertFeatures(int zoom, List<Map<String, Object>> expected, Iterable<FeatureCollector.Feature> actual) {
List<FeatureCollector.Feature> actualList = StreamSupport.stream(actual.spliterator(), false).toList(); // ensure both are sorted by layer
assertEquals(expected.size(), actualList.size(), () -> "size: " + actualList); var expectedList =
for (int i = 0; i < expected.size(); i++) { expected.stream().sorted(Comparator.comparing(d -> coalesce(d.get("_layer"), "").toString())).toList();
assertSubmap(expected.get(i), TestUtils.toMap(actualList.get(i), zoom)); var actualList = StreamSupport.stream(actual.spliterator(), false)
.sorted(Comparator.comparing(FeatureCollector.Feature::getLayer))
.toList();
assertEquals(expectedList.size(), actualList.size(), () -> "size: " + actualList);
for (int i = 0; i < expectedList.size(); i++) {
assertSubmap(expectedList.get(i), TestUtils.toMap(actualList.get(i), zoom));
} }
} }

View File

@@ -164,7 +164,7 @@ class WaterTest extends AbstractLayerTest {
} }
@Test @Test
void testRiverk() { void testRiver() {
assertFeatures(11, List.of(Map.of( assertFeatures(11, List.of(Map.of(
"class", "river", "class", "river",
"_layer", "water", "_layer", "water",