mirror of
https://github.com/WarmUpTill/SceneSwitcher.git
synced 2026-11-17 07:03:26 -06:00
Add separate input variable to JSON variable actions
This commit is contained in:
@@ -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}}"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "macro-action-variable-json.hpp"
|
||||
#include "json-helpers.hpp"
|
||||
|
||||
#include <QCheckBox>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include "variable-line-edit.hpp"
|
||||
#include "variable-spinbox.hpp"
|
||||
|
||||
class QCheckBox;
|
||||
|
||||
namespace advss {
|
||||
|
||||
class VariableActionJson : public VariableActionGroup {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ private:
|
||||
VariableSelection *_variables2;
|
||||
FilterComboBox *_actions;
|
||||
VariableTextEdit *_strValue;
|
||||
QHBoxLayout *_entryLayout;
|
||||
QVBoxLayout *_entryLayout;
|
||||
std::vector<VariableActionGroupEdit *> _groupEdits;
|
||||
|
||||
std::shared_ptr<MacroActionVariable> _entryData;
|
||||
|
||||
@@ -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(
|
||||
|
||||
98
tests/test-macro-action-variable-json.cpp
Normal file
98
tests/test-macro-action-variable-json.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "catch.hpp"
|
||||
#include "macro-action-variable-json.hpp"
|
||||
|
||||
#include <obs.hpp>
|
||||
|
||||
using advss::MacroActionVariable;
|
||||
using advss::Variable;
|
||||
using advss::VariableActionJson;
|
||||
using Action = MacroActionVariable::Action;
|
||||
|
||||
static std::shared_ptr<Variable> 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>();
|
||||
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<Variable>(output);
|
||||
action._variable2 = std::weak_ptr<Variable>(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<VariableActionJson>()->_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<VariableActionJson>()->_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<Variable>();
|
||||
action._action = Action::EXTRACT_JSON;
|
||||
action._strValue = "field";
|
||||
action.PerformAction();
|
||||
REQUIRE(output->Value() == "unchanged");
|
||||
}
|
||||
|
||||
advss::GetVariables().clear();
|
||||
}
|
||||
Reference in New Issue
Block a user