Skip to content

Commit

Permalink
Improve style source deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Aug 15, 2023
1 parent 18ae5d9 commit 2114a6f
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,19 @@ public String read(Path path) throws IOException {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,52 @@


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.
*/
Expand Down Expand Up @@ -84,4 +115,204 @@ public StyleSource setUrl(String url) {
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.
*/

package org.apache.baremaps.config;

import static org.junit.jupiter.api.Assertions.assertEquals;

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 ConfigReaderTest {

Check notice

Code scanning / CodeQL

Unused classes and interfaces Note test

Unused class: ConfigReaderTest is not referenced within this codebase. If not used as an external API it should be removed.

@Test
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
26 changes: 26 additions & 0 deletions baremaps-core/src/test/resources/style.js
Original file line number Diff line number Diff line change
@@ -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,
}
},
};

0 comments on commit 2114a6f

Please sign in to comment.