Skip to content

Commit 969ac25

Browse files
authored
[QC-1297] Accept pure json as value instead of a string containing json. (#2617)
* [QC-1297] Accept pure json as value instead of a string containing json. A more elegant way would be to use a variant in the mCustomParameters data structure. However, it implies making quite many changes to check what we are dealing with in every existing method, and some of them would need heavy refactoring (e.g. find()). Given that the overhead of the proposed solution is minimal, I would stick with it. * Update CustomParameters.cxx * expand the test
1 parent 7ec95d3 commit 969ac25

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

Framework/src/CustomParameters.cxx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,20 @@ void CustomParameters::populateCustomParameters(const boost::property_tree::ptre
202202
for (const auto& [runtype, subTreeRunType] : tree) {
203203
for (const auto& [beamtype, subTreeBeamType] : subTreeRunType) {
204204
for (const auto& [key, value] : subTreeBeamType) {
205-
set(key, value.get_value<std::string>(), runtype, beamtype);
205+
// Check if value has children (thus it is json)
206+
if (value.empty()) { // just a simple value
207+
set(key, value.get_value<std::string>(), runtype, beamtype);
208+
} else {
209+
// It's some json, serialize it to string
210+
std::stringstream ss;
211+
boost::property_tree::write_json(ss, value, false);
212+
std::string jsonString = ss.str();
213+
// Remove trailing newline if present
214+
if (!jsonString.empty() && jsonString.back() == '\n') {
215+
jsonString.pop_back();
216+
}
217+
set(key, jsonString, runtype, beamtype);
218+
}
206219
}
207220
}
208221
}

Framework/test/testCustomParameters.cxx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
#include <boost/property_tree/ptree.hpp>
1919
#include <boost/property_tree/json_parser.hpp>
2020
#include "getTestDataDirectory.h"
21+
#include "QualityControl/InfrastructureGenerator.h"
22+
2123
#include <iostream>
2224
#include <catch_amalgamated.hpp>
25+
#include <Configuration/ConfigurationFactory.h>
26+
#include <DataSampling/DataSampling.h>
2327

2428
using namespace o2::quality_control::core;
2529
using namespace std;
@@ -201,6 +205,35 @@ TEST_CASE("test_load_from_ptree")
201205
CHECK(cp.at("myOwnKey") == "myOwnValue");
202206
CHECK(cp.at("myOwnKey1", "PHYSICS") == "myOwnValue1b");
203207
CHECK(cp.atOptional("asdf").has_value() == false);
208+
209+
auto value = cp.getOptionalPtree("myOwnKey3");
210+
CHECK(value.has_value() == true);
211+
212+
// Check that it's an array with 1 element
213+
std::size_t arraySize = std::distance(value->begin(), value->end());
214+
CHECK(arraySize == 1);
215+
216+
// Get the first (and only) element of the array
217+
auto firstElement = value->begin()->second;
218+
219+
// Check the top-level properties
220+
CHECK(firstElement.get<string>("name") == "mean_of_histogram");
221+
CHECK(firstElement.get<string>("title") == "Mean trend of the example histogram");
222+
CHECK(firstElement.get<string>("graphAxisLabel") == "Mean X:time");
223+
CHECK(firstElement.get<string>("graphYRange") == "0:10000");
224+
225+
// Check the graphs array
226+
auto graphs = firstElement.get_child("graphs");
227+
std::size_t graphsSize = std::distance(graphs.begin(), graphs.end());
228+
CHECK(graphsSize == 1);
229+
230+
// Check the first graph properties
231+
auto firstGraph = graphs.begin()->second;
232+
CHECK(firstGraph.get<string>("name") == "mean_trend");
233+
CHECK(firstGraph.get<string>("title") == "mean trend");
234+
CHECK(firstGraph.get<string>("varexp") == "example.mean:time");
235+
CHECK(firstGraph.get<string>("selection") == "");
236+
CHECK(firstGraph.get<string>("option") == "*L PLC PMC");
204237
}
205238

206239
TEST_CASE("test_default_if_not_found_at_optional")

Framework/test/testWorkflow.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,24 @@
2828
"default": {
2929
"default": {
3030
"myOwnKey": "myOwnValue",
31-
"myOwnKey2": "myOwnValue2"
31+
"myOwnKey2": "myOwnValue2",
32+
"myOwnKey3": [
33+
{
34+
"name": "mean_of_histogram",
35+
"title": "Mean trend of the example histogram",
36+
"graphAxisLabel": "Mean X:time",
37+
"graphYRange": "0:10000",
38+
"graphs" : [
39+
{
40+
"name": "mean_trend",
41+
"title": "mean trend",
42+
"varexp": "example.mean:time",
43+
"selection": "",
44+
"option": "*L PLC PMC"
45+
}
46+
]
47+
}
48+
]
3249
}
3350
},
3451
"PHYSICS": {

0 commit comments

Comments
 (0)