Skip to content

Commit efcab83

Browse files
committed
[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.
1 parent 6d52118 commit efcab83

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

Framework/src/CustomParameters.cxx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <vector>
1717
#include <boost/property_tree/json_parser.hpp>
1818

19+
using namespace std;
20+
1921
namespace o2::quality_control::core
2022
{
2123

@@ -202,7 +204,20 @@ void CustomParameters::populateCustomParameters(const boost::property_tree::ptre
202204
for (const auto& [runtype, subTreeRunType] : tree) {
203205
for (const auto& [beamtype, subTreeBeamType] : subTreeRunType) {
204206
for (const auto& [key, value] : subTreeBeamType) {
205-
set(key, value.get_value<std::string>(), runtype, beamtype);
207+
// Check if value has children (thus it is json)
208+
if (value.empty()) { // just a simple value
209+
set(key, value.get_value<std::string>(), runtype, beamtype);
210+
} else {
211+
// It's some json, serialize it to string
212+
std::stringstream ss;
213+
boost::property_tree::write_json(ss, value, false);
214+
std::string jsonString = ss.str();
215+
// Remove trailing newline if present
216+
if (!jsonString.empty() && jsonString.back() == '\n') {
217+
jsonString.pop_back();
218+
}
219+
set(key, jsonString, runtype, beamtype);
220+
}
206221
}
207222
}
208223
}

Framework/test/testCustomParameters.cxx

Lines changed: 26 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,28 @@ 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+
string json = R"""(
210+
[
211+
{
212+
"name": "mean_of_histogram",
213+
"title": "Mean trend of the example histogram",
214+
"graphAxisLabel": "Mean X:time",
215+
"graphYRange": "0:10000",
216+
"graphs" : [
217+
{
218+
"name": "mean_trend",
219+
"title": "mean trend",
220+
"varexp": "example.mean:time",
221+
"selection": "",
222+
"option": "*L PLC PMC"
223+
}
224+
]
225+
}
226+
]
227+
)""";
228+
auto value = cp.getOptionalPtree("myOwnKey3");
229+
CHECK(value.has_value() == true);
204230
}
205231

206232
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)