Skip to content

Commit

Permalink
Apply global variables within Marker styling strings (#2309)
Browse files Browse the repository at this point in the history
* Apply global variables within Marker styling strings

* Update syntax in sceneUpdateTests
  • Loading branch information
matteblair authored Dec 19, 2021
1 parent 0f9d74a commit ff9cfbc
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/src/marker/markerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ bool MarkerManager::buildStyling(Marker& marker) {
std::vector<StyleParam> params;
try {
YAML::Node node = YAML::Load(markerStyling.string);
SceneLoader::applyGlobals(m_scene.config(), node);
params = SceneLoader::parseStyleParams(node, m_stops, m_functions);
} catch (const YAML::Exception& e) {
LOG("Invalid marker styling '%s', %s", markerStyling.string.c_str(), e.what());
Expand Down
2 changes: 1 addition & 1 deletion core/src/scene/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ bool Scene::load() {

Importer::resolveSceneUrls(m_config, m_options.url);

SceneLoader::applyGlobals(m_config);
SceneLoader::applyGlobals(m_config, m_config);
LOGTO("<<< applyGlobals");

m_tileSources = SceneLoader::applySources(m_config, m_options, m_platform);
Expand Down
8 changes: 4 additions & 4 deletions core/src/scene/sceneLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,23 @@ void createGlobalRefs(std::vector<std::pair<YamlPath, YamlPath>>& _globalRefs,
}
}

void SceneLoader::applyGlobals(Node& _config) {
const Node& globals = _config["global"];
void SceneLoader::applyGlobals(const Node& source, Node& destination) {
const Node& globals = source["global"];

// Records the YAML Nodes for which global values have been swapped; keys are
// nodes that referenced globals, values are nodes of globals themselves.
std::vector<std::pair<YamlPath, YamlPath>> globalRefs;

YamlPathBuffer path;
createGlobalRefs(globalRefs, _config, path);
createGlobalRefs(globalRefs, destination, path);
if (!globalRefs.empty() && (!globals || !globals.IsMap())) {
LOGW("Missing global references");
return;
}

for (auto& globalRef : globalRefs) {
Node target, global;
bool targetPathIsValid = globalRef.first.get(_config, target);
bool targetPathIsValid = globalRef.first.get(destination, target);
bool globalPathIsValid = globalRef.second.get(globals, global);
if (targetPathIsValid && globalPathIsValid && target.IsDefined() && global.IsDefined()) {
target = global;
Expand Down
2 changes: 1 addition & 1 deletion core/src/scene/sceneLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct SceneLoader {
static SceneError applyUpdates(Node& config, const std::vector<SceneUpdate>& updates);

/// Global
static void applyGlobals(Node& config);
static void applyGlobals(const Node& source, Node& destination);

/// Scene
static void applyScene(const Node& sceneNode, Color& background, Stops& backgroundStops,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/sceneUpdateTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ TEST_CASE("Apply and propogate repeated global value updates") {
REQUIRE(loadConfig(sceneString, config));
Node& root = config;
// Apply initial globals.
SceneLoader::applyGlobals(config);
SceneLoader::applyGlobals(config, config);
CHECK(root["seq"][1].Scalar() == "global_a_value");
CHECK(root["map"]["b"].Scalar() == "global_b_value");
// Add an update.
Expand Down

0 comments on commit ff9cfbc

Please sign in to comment.