This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git


The following commit(s) were added to refs/heads/main by this push:
     new 30523b02 Improve style source deserialization (#747)
30523b02 is described below

commit 30523b0276a711ec7a3b83401ddfdaf0fb79d014
Author: Bertil Chapuis <[email protected]>
AuthorDate: Tue Aug 15 23:51:24 2023 +0200

    Improve style source deserialization (#747)
---
 .../org/apache/baremaps/config/ConfigReader.java   |  13 +-
 .../baremaps/vectortile/style/StyleSource.java     | 233 ++++++++++++++++++++-
 .../ConfigReaderTest.java}                         |  22 +-
 .../apache/baremaps/stream/StreamUtilsTest.java    |   2 +-
 .../org/apache/baremaps/testing/TestFiles.java     |   4 +-
 baremaps-core/src/test/resources/style.js          |  26 +++
 6 files changed, 283 insertions(+), 17 deletions(-)

diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/config/ConfigReader.java 
b/baremaps-core/src/main/java/org/apache/baremaps/config/ConfigReader.java
index 8c08a256..d19e4316 100644
--- a/baremaps-core/src/main/java/org/apache/baremaps/config/ConfigReader.java
+++ b/baremaps-core/src/main/java/org/apache/baremaps/config/ConfigReader.java
@@ -39,14 +39,19 @@ public class ConfigReader {
   }
 
   private String eval(Path path) throws IOException {
-    try (var context = 
Context.newBuilder("js").option("js.esm-eval-returns-exports", "true")
-        .option("js.scripting", 
"true").allowExperimentalOptions(true).allowIO(true).build()) {
+    try (var context = Context.newBuilder("js")
+        .option("js.esm-eval-returns-exports", "true")
+        .option("js.scripting", "true")
+        .allowExperimentalOptions(true)
+        .allowIO(true)
+        .build()) {
       var script = String.format("""
           import config from '%s';
           export default JSON.stringify(config);
-          """, path.toAbsolutePath());
+          """, path.toAbsolutePath().toUri());
       var source = Source.newBuilder("js", new StringReader(script), 
"script.js")
-          .mimeType("application/javascript+module").build();
+          .mimeType("application/javascript+module")
+          .build();
       var value = context.eval(source);
       return value.getMember("default").toString();
     } catch (Exception e) {
diff --git 
a/baremaps-core/src/main/java/org/apache/baremaps/vectortile/style/StyleSource.java
 
b/baremaps-core/src/main/java/org/apache/baremaps/vectortile/style/StyleSource.java
index 8ac5cfd6..09fae5c9 100644
--- 
a/baremaps-core/src/main/java/org/apache/baremaps/vectortile/style/StyleSource.java
+++ 
b/baremaps-core/src/main/java/org/apache/baremaps/vectortile/style/StyleSource.java
@@ -15,21 +15,52 @@ package org.apache.baremaps.vectortile.style;
 
 
 import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.List;
 
 /**
  * A source is a collection of data that is used as a single input to style 
layers.
  *
  * @see <a href=
- *      
"https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/";>https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/</a>
+ *      
"https://maplibre.org/maplibre-style-spec/sources/";>https://maplibre.org/maplibre-style-spec/sources/</a>
  */
 public class StyleSource {
 
   @JsonProperty("type")
   private String type;
 
+  @JsonProperty("attribution")
+  private String attribution;
+
+  @JsonProperty("bounds")
+  private List<Double> bounds;
+
+  @JsonProperty("minzoom")
+  private Integer minzoom;
+
+  @JsonProperty("maxzoom")
+  private Integer maxzoom;
+
+  @JsonProperty("promoteId")
+  private String promoteId;
+
+  @JsonProperty("scheme")
+  private String scheme;
+
+  @JsonProperty("tiles")
+  private List<String> tiles;
+
   @JsonProperty("url")
   private String url;
 
+  @JsonProperty("volatile")
+  private Boolean isVolatile;
+
+  @JsonProperty("tileSize")
+  private Integer tileSize;
+
+  @JsonProperty("encoding")
+  private String encoding;
+
   /**
    * Constructs a style source.
    */
@@ -84,4 +115,204 @@ public class StyleSource {
     this.url = url;
     return this;
   }
+
+  /**
+   * Returns the tiles of the source.
+   *
+   * @return the tiles of the source
+   */
+  public String getAttribution() {
+    return attribution;
+  }
+
+  /**
+   * Sets the attribution of the source.
+   *
+   * @param attribution the attribution of the source
+   * @return the source
+   */
+  public StyleSource setAttribution(String attribution) {
+    this.attribution = attribution;
+    return this;
+  }
+
+  /**
+   * Returns the bounds of the source.
+   *
+   * @return the bounds of the source
+   */
+  public List<Double> getBounds() {
+    return bounds;
+  }
+
+  /**
+   * Sets the bounds of the source.
+   *
+   * @param bounds the bounds of the source
+   * @return the source
+   */
+  public StyleSource setBounds(List<Double> bounds) {
+    this.bounds = bounds;
+    return this;
+  }
+
+  /**
+   * Returns the minzoom of the source.
+   *
+   * @return the minzoom of the source
+   */
+  public Integer getMinzoom() {
+    return minzoom;
+  }
+
+  /**
+   * Sets the minzoom of the source.
+   *
+   * @param minzoom the minzoom of the source
+   * @return the source
+   */
+  public StyleSource setMinzoom(Integer minzoom) {
+    this.minzoom = minzoom;
+    return this;
+  }
+
+  /**
+   * Returns the maxzoom of the source.
+   *
+   * @return the maxzoom of the source
+   */
+  public Integer getMaxzoom() {
+    return maxzoom;
+  }
+
+  /**
+   * Sets the maxzoom of the source.
+   *
+   * @param maxzoom the maxzoom of the source
+   * @return the source
+   */
+  public StyleSource setMaxzoom(Integer maxzoom) {
+    this.maxzoom = maxzoom;
+    return this;
+  }
+
+  /**
+   * Returns the promoteId of the source.
+   *
+   * @return the promoteId of the source
+   */
+  public String getPromoteId() {
+    return promoteId;
+  }
+
+  /**
+   * Sets the promoteId of the source.
+   *
+   * @param promoteId the promoteId of the source
+   * @return the source
+   */
+  public StyleSource setPromoteId(String promoteId) {
+    this.promoteId = promoteId;
+    return this;
+  }
+
+  /**
+   * Returns the scheme of the source.
+   *
+   * @return the scheme of the source
+   */
+  public String getScheme() {
+    return scheme;
+  }
+
+  /**
+   * Sets the scheme of the source.
+   *
+   * @param scheme the scheme of the source
+   * @return the source
+   */
+  public StyleSource setScheme(String scheme) {
+    this.scheme = scheme;
+    return this;
+  }
+
+  /**
+   * Returns the tiles of the source.
+   *
+   * @return the tiles of the source
+   */
+  public List<String> getTiles() {
+    return tiles;
+  }
+
+  /**
+   * Sets the tiles of the source.
+   *
+   * @param tiles the tiles of the source
+   * @return the source
+   */
+  public StyleSource setTiles(List<String> tiles) {
+    this.tiles = tiles;
+    return this;
+  }
+
+  /**
+   * Returns the volatile of the source.
+   *
+   * @return the volatile of the source
+   */
+  public Boolean getVolatile() {
+    return isVolatile;
+  }
+
+  /**
+   * Sets the volatile of the source.
+   *
+   * @param isVolatile the volatile of the source
+   * @return the source
+   */
+  public StyleSource setVolatile(Boolean isVolatile) {
+    this.isVolatile = isVolatile;
+    return this;
+  }
+
+  /**
+   * Returns the tileSize of the source.
+   *
+   * @return the tileSize of the source
+   */
+  public Integer getTileSize() {
+    return tileSize;
+  }
+
+  /**
+   * Sets the tileSize of the source.
+   *
+   * @param tileSize the tileSize of the source
+   * @return the source
+   */
+  public StyleSource setTileSize(Integer tileSize) {
+    this.tileSize = tileSize;
+    return this;
+  }
+
+  /**
+   * Returns the encoding of the source.
+   *
+   * @return the encoding of the source
+   */
+  public String getEncoding() {
+    return encoding;
+  }
+
+  /**
+   * Sets the encoding of the source.
+   *
+   * @param encoding the encoding of the source
+   * @return the source
+   */
+  public StyleSource setEncoding(String encoding) {
+    this.encoding = encoding;
+    return this;
+  }
 }
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java 
b/baremaps-core/src/test/java/org/apache/baremaps/config/ConfigReaderTest.java
similarity index 52%
copy from 
baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java
copy to 
baremaps-core/src/test/java/org/apache/baremaps/config/ConfigReaderTest.java
index fedb1c6a..d4a39abb 100644
--- 
a/baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java
+++ 
b/baremaps-core/src/test/java/org/apache/baremaps/config/ConfigReaderTest.java
@@ -10,22 +10,24 @@
  * the License.
  */
 
-package org.apache.baremaps.stream;
+package org.apache.baremaps.config;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.IntStream;
+import java.io.IOException;
+import org.apache.baremaps.testing.TestFiles;
+import org.apache.baremaps.vectortile.style.Style;
 import org.junit.jupiter.api.Test;
+import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
 
-class StreamUtilsTest {
+class ConfigReaderTest {
 
   @Test
-  void partition() {
-    List<Integer> list = IntStream.range(0, 100).mapToObj(i -> i).toList();
-    List<List<Integer>> partitions = StreamUtils.partition(list.stream(), 10)
-        .map(stream -> stream.collect(Collectors.toList())).toList();
-    assertEquals(partitions.size(), 10);
+  void readStyle() throws IOException {
+    var config = new ConfigReader().read(TestFiles.STYLE_JS);
+    var style = new ObjectMapper().readValue(config, Style.class);
+    var source = style.getSources().get("mymap");
+    assertEquals("http://my.server.com/{z}/{y}/{x}.mvt";, 
source.getTiles().get(0));
+    assertEquals(14, source.getMaxzoom());
   }
 }
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java 
b/baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java
index fedb1c6a..734feba5 100644
--- 
a/baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java
+++ 
b/baremaps-core/src/test/java/org/apache/baremaps/stream/StreamUtilsTest.java
@@ -23,7 +23,7 @@ class StreamUtilsTest {
 
   @Test
   void partition() {
-    List<Integer> list = IntStream.range(0, 100).mapToObj(i -> i).toList();
+    List<Integer> list = IntStream.range(0, 100).boxed().toList();
     List<List<Integer>> partitions = StreamUtils.partition(list.stream(), 10)
         .map(stream -> stream.collect(Collectors.toList())).toList();
     assertEquals(partitions.size(), 10);
diff --git 
a/baremaps-core/src/test/java/org/apache/baremaps/testing/TestFiles.java 
b/baremaps-core/src/test/java/org/apache/baremaps/testing/TestFiles.java
index e1c82db9..450c0ace 100644
--- a/baremaps-core/src/test/java/org/apache/baremaps/testing/TestFiles.java
+++ b/baremaps-core/src/test/java/org/apache/baremaps/testing/TestFiles.java
@@ -48,9 +48,11 @@ public class TestFiles {
 
   public static final Path MONACO_STATE_TXT = 
resolve("monaco/monaco-state.txt");
 
+  public static final Path STYLE_JS = resolve("style.js");
+
   public static Path resolve(String resource) {
     Path cwd = Path.of("").toAbsolutePath();
     Path pathFromRoot = Path.of("baremaps-core", "src", "test", "resources", 
resource);
-    return cwd.resolveSibling(pathFromRoot);
+    return cwd.resolveSibling(pathFromRoot).toAbsolutePath();
   }
 }
diff --git a/baremaps-core/src/test/resources/style.js 
b/baremaps-core/src/test/resources/style.js
new file mode 100644
index 00000000..1d22a158
--- /dev/null
+++ b/baremaps-core/src/test/resources/style.js
@@ -0,0 +1,26 @@
+/**
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not 
use this file except
+ in compliance with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software 
distributed under the License
+ is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express
+ or implied. See the License for the specific language governing permissions 
and limitations under
+ the License.
+ **/
+export default {
+    "version": 8,
+    "name": "MyMap",
+    "center": [0, 0],
+    "zoom": 10,
+    "sources": {
+        "mymap": {
+            "type": "vector",
+            "tiles": [
+                "http://my.server.com/{z}/{y}/{x}.mvt";
+            ],
+            "maxzoom": 14,
+        }
+    },
+};

Reply via email to