From 4a38516ede13eff2f20e2ce5dfcb495666fc3dbd Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Thu, 5 Nov 2026 11:22:07 +0100 Subject: [PATCH] Add separate input variable to JSON variable actions --- data/locale/en-US.ini | 5 +- lib/macro/macro-action-variable-json.cpp | 36 +++++++-- lib/macro/macro-action-variable-json.hpp | 2 + lib/macro/macro-action-variable.cpp | 25 +++--- lib/macro/macro-action-variable.hpp | 2 +- tests/CMakeLists.txt | 9 +++ tests/test-macro-action-variable-json.cpp | 98 +++++++++++++++++++++++ 7 files changed, 157 insertions(+), 20 deletions(-) create mode 100644 tests/test-macro-action-variable-json.cpp diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 672f44d3..6234ead6 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -1448,7 +1448,10 @@ AdvSceneSwitcher.action.variable.layout.variable2="{{actions}}{{variables}}{{var AdvSceneSwitcher.action.variable.layout.changeCase="{{actions}}{{variables}}{{caseType}}" AdvSceneSwitcher.action.variable.layout.queryJson="{{actions}}{{variables}}{{jsonQuery}}{{jsonQueryHelp}}" AdvSceneSwitcher.action.variable.layout.accessJsonArray="{{actions}}{{variables}}{{jsonIndex}}" -AdvSceneSwitcher.action.variable.layout.jsonArrayLength="{{actions}}{{variables2}}and store it in{{variables}}" +AdvSceneSwitcher.action.variable.layout.jsonArrayLength="{{actions}}\nRead from{{variables2}}and store the result in{{variables}}" +AdvSceneSwitcher.action.variable.layout.jsonField="{{actions}}{{strValue}}\nRead from{{variables2}}and store the result in{{variables}}" +AdvSceneSwitcher.action.variable.layout.jsonQuery="{{actions}}{{jsonQuery}}{{jsonQueryHelp}}\nRead from{{variables2}}and store the result in{{variables}}" +AdvSceneSwitcher.action.variable.layout.jsonIndex="{{actions}}{{jsonIndex}}\nRead from{{variables2}}and store the result in{{variables}}" AdvSceneSwitcher.action.variable.layout.setToTempvar="{{actions}}{{variables}}{{tempVars}}{{tempVarsHelp}}" AdvSceneSwitcher.action.variable.layout.segmentValue="{{actions}}{{variables}}{{segmentIndex}}" AdvSceneSwitcher.action.variable.layout.mathExpression="{{actions}}{{variables}}{{mathExpression}}" diff --git a/lib/macro/macro-action-variable-json.cpp b/lib/macro/macro-action-variable-json.cpp index bf57b234..869eae9b 100644 --- a/lib/macro/macro-action-variable-json.cpp +++ b/lib/macro/macro-action-variable-json.cpp @@ -1,6 +1,8 @@ #include "macro-action-variable-json.hpp" #include "json-helpers.hpp" +#include + namespace advss { using Action = MacroActionVariable::Action; @@ -10,13 +12,13 @@ static bool registered = RegisterVariableActionGroup( { {Action::EXTRACT_JSON, "AdvSceneSwitcher.action.variable.type.extractJsonField", - "AdvSceneSwitcher.action.variable.layout.strValue", true}, + "AdvSceneSwitcher.action.variable.layout.jsonField", true}, {Action::QUERY_JSON, "AdvSceneSwitcher.action.variable.type.queryJson", - "AdvSceneSwitcher.action.variable.layout.queryJson", true}, + "AdvSceneSwitcher.action.variable.layout.jsonQuery", true}, {Action::ARRAY_JSON, "AdvSceneSwitcher.action.variable.type.accessJsonArray", - "AdvSceneSwitcher.action.variable.layout.accessJsonArray"}, + "AdvSceneSwitcher.action.variable.layout.jsonIndex"}, {Action::JSON_ARRAY_LENGTH, "AdvSceneSwitcher.action.variable.type.jsonArrayLength", "AdvSceneSwitcher.action.variable.layout.jsonArrayLength"}, @@ -42,7 +44,7 @@ void VariableActionJson::Save(obs_data_t *obj, _jsonValue.Save(obj, "jsonValue"); } -void VariableActionJson::Load(obs_data_t *obj, MacroActionVariable &) +void VariableActionJson::Load(obs_data_t *obj, MacroActionVariable &action) { _jsonQuery.Load(obj, "jsonQuery"); _jsonIndex.Load(obj, "jsonIndex"); @@ -51,6 +53,14 @@ void VariableActionJson::Load(obs_data_t *obj, MacroActionVariable &) if (obs_data_has_user_value(obj, "jsonValue")) { _jsonValue.Load(obj, "jsonValue"); } + + // Old settings modified _variable in place + const bool modifiedInPlace = action._action == Action::EXTRACT_JSON || + action._action == Action::QUERY_JSON || + action._action == Action::ARRAY_JSON; + if (modifiedInPlace && obs_data_get_int(obj, "version") < 2) { + action._variable2 = action._variable; + } } void VariableActionJson::ResolveVariables() @@ -66,7 +76,11 @@ bool VariableActionJson::Perform(MacroActionVariable &action, { switch (type) { case Action::EXTRACT_JSON: { - auto value = GetJsonField(var.Value(), action._strValue); + auto input = action._variable2.lock(); + if (!input) { + return true; + } + auto value = GetJsonField(input->Value(), action._strValue); if (!value.has_value()) { return true; } @@ -74,7 +88,11 @@ bool VariableActionJson::Perform(MacroActionVariable &action, return true; } case Action::QUERY_JSON: { - auto value = QueryJson(var.Value(), _jsonQuery); + auto input = action._variable2.lock(); + if (!input) { + return true; + } + auto value = QueryJson(input->Value(), _jsonQuery); if (!value.has_value()) { return true; } @@ -93,7 +111,11 @@ bool VariableActionJson::Perform(MacroActionVariable &action, return true; } case Action::ARRAY_JSON: { - auto value = AccessJsonArrayIndex(var.Value(), _jsonIndex); + auto input = action._variable2.lock(); + if (!input) { + return true; + } + auto value = AccessJsonArrayIndex(input->Value(), _jsonIndex); if (!value.has_value()) { return true; } diff --git a/lib/macro/macro-action-variable-json.hpp b/lib/macro/macro-action-variable-json.hpp index 344dedc3..86ac96b2 100644 --- a/lib/macro/macro-action-variable-json.hpp +++ b/lib/macro/macro-action-variable-json.hpp @@ -4,6 +4,8 @@ #include "variable-line-edit.hpp" #include "variable-spinbox.hpp" +class QCheckBox; + namespace advss { class VariableActionJson : public VariableActionGroup { diff --git a/lib/macro/macro-action-variable.cpp b/lib/macro/macro-action-variable.cpp index 197eec8d..ca9a1d5a 100644 --- a/lib/macro/macro-action-variable.cpp +++ b/lib/macro/macro-action-variable.cpp @@ -67,7 +67,7 @@ bool MacroActionVariable::Save(obs_data_t *obj) const for (const auto &group : _groups) { group->Save(obj, *this); } - obs_data_set_int(obj, "version", 1); + obs_data_set_int(obj, "version", 2); return true; } @@ -136,7 +136,7 @@ MacroActionVariableEdit::MacroActionVariableEdit( _variables2(new VariableSelection(this)), _actions(new FilterComboBox(this)), _strValue(new VariableTextEdit(this, 5, 1, 1)), - _entryLayout(new QHBoxLayout()) + _entryLayout(new QVBoxLayout()) { populateActionSelection(_actions); @@ -242,25 +242,28 @@ void MacroActionVariableEdit::SetWidgetVisibility() } const auto widgets = GetInlineWidgets(); - for (const auto &[_, widget] : widgets) { - _entryLayout->removeWidget(widget); + for (int i = 0; i < _entryLayout->count(); i++) { + auto row = _entryLayout->itemAt(i)->layout(); + for (const auto &[_, widget] : widgets) { + row->removeWidget(widget); + } } ClearLayout(_entryLayout); const std::string layoutString = obs_module_text(option->layout); - PlaceWidgets(layoutString, _entryLayout, widgets); + const auto rows = QString::fromStdString(layoutString).split('\n'); + for (int i = 0; i < rows.size(); i++) { + auto row = new QHBoxLayout(); + _entryLayout->addLayout(row); + const bool expandRow = option->expandInlineRow && i == 0; + PlaceWidgets(rows[i].toStdString(), row, widgets, !expandRow); + } for (const auto &[placeholder, widget] : widgets) { widget->setVisible(layoutString.find(placeholder) != std::string::npos); } - if (option->expandInlineRow) { - RemoveStretchIfPresent(_entryLayout); - } else { - AddStretchIfNecessary(_entryLayout); - } - for (const auto &groupEdit : _groupEdits) { groupEdit->SetWidgetVisibility(_entryData->_action); } diff --git a/lib/macro/macro-action-variable.hpp b/lib/macro/macro-action-variable.hpp index 0e3a7a24..dce756c2 100644 --- a/lib/macro/macro-action-variable.hpp +++ b/lib/macro/macro-action-variable.hpp @@ -126,7 +126,7 @@ private: VariableSelection *_variables2; FilterComboBox *_actions; VariableTextEdit *_strValue; - QHBoxLayout *_entryLayout; + QVBoxLayout *_entryLayout; std::vector _groupEdits; std::shared_ptr _entryData; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7e18167..840a52c1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -183,6 +183,15 @@ target_sources( ${ADVSS_SOURCE_DIR}/lib/variables/variable-spinbox.cpp ${ADVSS_SOURCE_DIR}/lib/variables/variable-string.cpp) +# --- macro-action-variable --- # + +target_sources( + ${PROJECT_NAME} + PRIVATE test-macro-action-variable-json.cpp + ${ADVSS_SOURCE_DIR}/lib/macro/macro-action-variable.cpp + ${ADVSS_SOURCE_DIR}/lib/macro/macro-action-variable-group.cpp + ${ADVSS_SOURCE_DIR}/lib/macro/macro-action-variable-json.cpp) + # --- macro-tree --- # target_sources( diff --git a/tests/test-macro-action-variable-json.cpp b/tests/test-macro-action-variable-json.cpp new file mode 100644 index 00000000..6a342dee --- /dev/null +++ b/tests/test-macro-action-variable-json.cpp @@ -0,0 +1,98 @@ +#include "catch.hpp" +#include "macro-action-variable-json.hpp" + +#include + +using advss::MacroActionVariable; +using advss::Variable; +using advss::VariableActionJson; +using Action = MacroActionVariable::Action; + +static std::shared_ptr addVariable(const std::string &name) +{ + OBSDataAutoRelease data = obs_data_create(); + obs_data_set_string(data, "name", name.c_str()); + auto variable = std::make_shared(); + variable->Load(data); + advss::GetVariables().emplace_back(variable); + return variable; +} + +TEST_CASE("JSON variable actions read input and write output", + "[macro-action-variable]") +{ + auto output = addVariable("output"); + auto input = addVariable("input"); + MacroActionVariable action(nullptr); + action._variable = std::weak_ptr(output); + action._variable2 = std::weak_ptr(input); + + SECTION("Extract field") + { + const std::string json = R"({"field":"value"})"; + input->SetValue(json); + action._action = Action::EXTRACT_JSON; + action._strValue = "field"; + action.PerformAction(); + REQUIRE(output->Value() == "value"); + REQUIRE(input->Value() == json); + } + SECTION("Access array") + { + const std::string json = R"(["a","b"])"; + input->SetValue(json); + action._action = Action::ARRAY_JSON; + action.GetGroup()->_jsonIndex = 1; + action.PerformAction(); + REQUIRE(output->Value() == "b"); + REQUIRE(input->Value() == json); + } +#ifdef JSONPATH_SUPPORT + SECTION("Query") + { + const std::string json = R"({"field":"value"})"; + input->SetValue(json); + action._action = Action::QUERY_JSON; + action.GetGroup()->_jsonQuery = "$.field"; + action.PerformAction(); + REQUIRE(output->Value() == "value"); + REQUIRE(input->Value() == json); + } +#endif + SECTION("Output only changes when the result changes") + { + input->SetValue(R"({"field":"value","other":1})"); + action._action = Action::EXTRACT_JSON; + action._strValue = "field"; + action.PerformAction(); + const int changeCount = output->GetValueChangeCount(); + + input->SetValue(R"({"field":"value","other":2})"); + action.PerformAction(); + REQUIRE(output->GetValueChangeCount() == changeCount); + + input->SetValue(R"({"field":"new","other":2})"); + action.PerformAction(); + REQUIRE(output->GetValueChangeCount() == changeCount + 1); + } + SECTION("Invalid input leaves output unchanged") + { + output->SetValue("unchanged"); + input->SetValue("not json"); + action._action = Action::EXTRACT_JSON; + action._strValue = "field"; + action.PerformAction(); + REQUIRE(output->Value() == "unchanged"); + } + SECTION("No input selected leaves output unchanged") + { + output->SetValue("unchanged"); + action._variable2 = std::weak_ptr(); + action._action = Action::EXTRACT_JSON; + action._strValue = "field"; + action.PerformAction(); + REQUIRE(output->Value() == "unchanged"); + } + + advss::GetVariables().clear(); +}