From ff9cfbce890fc3e025bc78bc6d57f9857751888f Mon Sep 17 00:00:00 2001 From: Matt Blair Date: Sun, 19 Dec 2021 00:06:03 -0500 Subject: [PATCH] Apply global variables within Marker styling strings (#2309) * Apply global variables within Marker styling strings * Update syntax in sceneUpdateTests --- core/src/marker/markerManager.cpp | 1 + core/src/scene/scene.cpp | 2 +- core/src/scene/sceneLoader.cpp | 8 ++++---- core/src/scene/sceneLoader.h | 2 +- tests/unit/sceneUpdateTests.cpp | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/src/marker/markerManager.cpp b/core/src/marker/markerManager.cpp index cde5d531df..b299db8ac9 100644 --- a/core/src/marker/markerManager.cpp +++ b/core/src/marker/markerManager.cpp @@ -364,6 +364,7 @@ bool MarkerManager::buildStyling(Marker& marker) { std::vector 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()); diff --git a/core/src/scene/scene.cpp b/core/src/scene/scene.cpp index c91959d877..c4115cd98b 100644 --- a/core/src/scene/scene.cpp +++ b/core/src/scene/scene.cpp @@ -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); diff --git a/core/src/scene/sceneLoader.cpp b/core/src/scene/sceneLoader.cpp index 0f271971f9..048bfe9f9e 100644 --- a/core/src/scene/sceneLoader.cpp +++ b/core/src/scene/sceneLoader.cpp @@ -113,15 +113,15 @@ void createGlobalRefs(std::vector>& _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> globalRefs; YamlPathBuffer path; - createGlobalRefs(globalRefs, _config, path); + createGlobalRefs(globalRefs, destination, path); if (!globalRefs.empty() && (!globals || !globals.IsMap())) { LOGW("Missing global references"); return; @@ -129,7 +129,7 @@ void SceneLoader::applyGlobals(Node& _config) { 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; diff --git a/core/src/scene/sceneLoader.h b/core/src/scene/sceneLoader.h index a367dfac9b..d115f42fa4 100644 --- a/core/src/scene/sceneLoader.h +++ b/core/src/scene/sceneLoader.h @@ -34,7 +34,7 @@ struct SceneLoader { static SceneError applyUpdates(Node& config, const std::vector& 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, diff --git a/tests/unit/sceneUpdateTests.cpp b/tests/unit/sceneUpdateTests.cpp index 11032175f4..1cafdd5ff2 100644 --- a/tests/unit/sceneUpdateTests.cpp +++ b/tests/unit/sceneUpdateTests.cpp @@ -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.