diff --git a/Trigger/TrigConfiguration/TrigConfData/CMakeLists.txt b/Trigger/TrigConfiguration/TrigConfData/CMakeLists.txt index 210474b2b93d4360f497189ba30cee29eae7b8c4..0271c076acb1bff1b6b0d0ac039ccce2f2e118f8 100644 --- a/Trigger/TrigConfiguration/TrigConfData/CMakeLists.txt +++ b/Trigger/TrigConfiguration/TrigConfData/CMakeLists.txt @@ -42,3 +42,9 @@ atlas_add_test( TestLogicParser SOURCES test/testLogicParser.cxx INCLUDE_DIRS ${Boost_INCLUDE_DIRS} LINK_LIBRARIES ${Boost_LIBRARIES} TrigConfData POST_EXEC_SCRIPT nopost.sh ) + +atlas_add_test( TestFileRW SOURCES test/testfilerw.cxx + INCLUDE_DIRS ${Boost_INCLUDE_DIRS} + LINK_LIBRARIES ${Boost_LIBRARIES} TrigConfData + ENVIRONMENT "TESTFILEPATH=${CMAKE_CURRENT_SOURCE_DIR}/test/" + POST_EXEC_SCRIPT nopost.sh ) diff --git a/Trigger/TrigConfiguration/TrigConfData/src/DataStructure.cxx b/Trigger/TrigConfiguration/TrigConfData/src/DataStructure.cxx index 49bf930e221665f885a6638c22d88191bb928e11..49b7bbe689727ffd2a5058b4a2f224f37becdca7 100644 --- a/Trigger/TrigConfiguration/TrigConfData/src/DataStructure.cxx +++ b/Trigger/TrigConfiguration/TrigConfData/src/DataStructure.cxx @@ -143,8 +143,18 @@ TrigConf::DataStructure::getList(const std::string & pathToChild, bool ignoreIfM } // check if the pathToChild points to a list + + // this check is not complete, because boost::ptree can not + // distinguish between and empty list and an empty string. In both cases + // the value is empty and there are no children + if ( list.get().empty() ) { - throw std::runtime_error(className() + "#" + name() + ": structure '" + pathToChild + "' is not a list [] but a simple attribute, it needs to be accessed via [\"" + pathToChild + "\"] -> string"); + if ( list.get().get_value<std::string>() != "" ) { + // if the value is not empty, then it is for sure an attribute ("key" : "value") + throw std::runtime_error(className() + "#" + name() + ": structure '" + pathToChild + "' is not a list [] but a simple attribute, it needs to be accessed via [\"" + pathToChild + "\"] -> string"); + } + // else: if the value is empty, we can not say for sure and will not + // give this debugging hint (an empty list will be returned } else if ( ! list.get().front().first.empty() ) { throw std::runtime_error(className() + "#" + name() + ": structure '" + pathToChild + "' is not a list [] but an object {}, it needs to be accessed via getObject(\"" + pathToChild + "\") -> DataStructure"); } diff --git a/Trigger/TrigConfiguration/TrigConfData/test/testfile.json b/Trigger/TrigConfiguration/TrigConfData/test/testfile.json new file mode 100644 index 0000000000000000000000000000000000000000..51e3ec03da19010ef6dfac414516d67de87f333f --- /dev/null +++ b/Trigger/TrigConfiguration/TrigConfData/test/testfile.json @@ -0,0 +1,6 @@ +{ + "fullList" : [ "a", "b", "c" ], + "emptyList" : [], + "nonEmptyAtt" : "abcd", + "emptyAtt" : "" +} diff --git a/Trigger/TrigConfiguration/TrigConfData/test/testfilerw.cxx b/Trigger/TrigConfiguration/TrigConfData/test/testfilerw.cxx new file mode 100644 index 0000000000000000000000000000000000000000..53ca32b84ad9edecbf8e985655c838c40c60d7c8 --- /dev/null +++ b/Trigger/TrigConfiguration/TrigConfData/test/testfilerw.cxx @@ -0,0 +1,39 @@ +#include "TrigConfData/DataStructure.h" + +#include "boost/property_tree/ptree.hpp" +#include "boost/property_tree/json_parser.hpp" + +#include <iostream> + +using namespace std; + +int main() { + + string testfilepath = string(getenv("TESTFILEPATH")) + "/"; + const string file = testfilepath + "testfile.json"; + + boost::property_tree::ptree data; + + try { + boost::property_tree::read_json(file, data); + } + catch (const boost::property_tree::json_parser_error& e) { + cerr << "Could either not locate or parse the file " << file << endl; + return 1; + } + + boost::property_tree::write_json(cout, data); + + TrigConf::DataStructure ds(data); + cout << "emptyAtt: '" << ds.getAttribute("emptyAtt") << "'" << endl; + cout << "nonEmptyAtt: '" << ds.getAttribute("nonEmptyAtt") << "'" << endl; + for( auto & item : ds.getList("fullList") ) { + cout << item.getValue() << endl; + } + for( auto & item : ds.getList("emptyList") ) { + cout << item.getValue() << endl; + } + + + return 0; +}