Introduce struct to reduce number of constructor params

This commit is contained in:
Vegard Kippe
2025-01-15 13:34:05 +01:00
parent 2cd0d63fe4
commit 76667d613b
8 changed files with 86 additions and 76 deletions

View File

@@ -131,19 +131,23 @@ namespace Opm::KeywordValidation {
properties.critical, keyword.location(), 1, std::nullopt, std::nullopt, properties.message});
} else {
// Otherwise, check all its items.
validateKeywordItems(keyword, m_partially_supported_string_items, errors);
validateKeywordItems(keyword, m_partially_supported_int_items, errors);
validateKeywordItems(keyword, m_partially_supported_double_items, errors);
validateKeywordItems(keyword, m_fully_supported_string_items, errors);
validateKeywordItems(keyword, m_fully_supported_int_items, errors);
validateKeywordItems(keyword, m_fully_supported_double_items, errors);
validateKeywordItems(keyword, m_partially_supported_keywords, errors);
validateKeywordItems(keyword, m_fully_supported_keywords, errors);
}
}
void KeywordValidator::validateKeywordItems(const DeckKeyword& keyword,
const SupportedKeywords& keyword_items,
std::vector<ValidationError>& errors) const
{
validateKeywordItems(keyword, keyword_items.string_items, errors);
validateKeywordItems(keyword, keyword_items.int_items, errors);
validateKeywordItems(keyword, keyword_items.double_items, errors);
}
template <typename T>
void KeywordValidator::validateKeywordItems(const DeckKeyword& keyword,
const SupportedKeywords<T>& partially_or_fully_supported_items,
const SupportedKeywordItems<T>& partially_or_fully_supported_items,
std::vector<ValidationError>& errors) const
{
const auto& keyword_properties = partially_or_fully_supported_items.find(keyword.name());

View File

@@ -63,11 +63,11 @@ namespace KeywordValidation
// This is used to list the partially supported items of a keyword:
template <typename T>
using SupportedKeywordItems = std::map<std::size_t, SupportedKeywordProperties<T>>;
using SupportedSingleKeywordItems = std::map<std::size_t, SupportedKeywordProperties<T>>;
// This is used to list the keywords that have partially supported items or items that benefit from early validation:
template <typename T>
using SupportedKeywords = std::map<std::string, SupportedKeywordItems<T>>;
using SupportedKeywordItems = std::map<std::string, SupportedSingleKeywordItems<T>>;
// This contains the information needed to report a single error occurence.
// The validator will construct a vector of these, copying the relevant
@@ -89,24 +89,22 @@ namespace KeywordValidation
const bool include_noncritical,
const bool include_critical);
struct SupportedKeywords {
const SupportedKeywordItems<std::string> string_items;
const SupportedKeywordItems<int> int_items;
const SupportedKeywordItems<double> double_items;
};
class KeywordValidator
{
public:
KeywordValidator(const UnsupportedKeywords& unsupported_keywords,
const SupportedKeywords<std::string>& partially_supported_string_items,
const SupportedKeywords<int>& partially_supported_int_items,
const SupportedKeywords<double>& partially_supported_double_items,
const SupportedKeywords<std::string>& fully_supported_string_items,
const SupportedKeywords<int>& fully_supported_int_items,
const SupportedKeywords<double>& fully_supported_double_items,
const SupportedKeywords& partially_supported_keywords,
const SupportedKeywords& fully_supported_keywords,
const std::unordered_map<std::string, ValidationFunction>& special_validation)
: m_unsupported_keywords(unsupported_keywords)
, m_partially_supported_string_items(partially_supported_string_items)
, m_partially_supported_int_items(partially_supported_int_items)
, m_partially_supported_double_items(partially_supported_double_items)
, m_fully_supported_string_items(fully_supported_string_items)
, m_fully_supported_int_items(fully_supported_int_items)
, m_fully_supported_double_items(fully_supported_double_items)
, m_partially_supported_keywords(partially_supported_keywords)
, m_fully_supported_keywords(fully_supported_keywords)
, m_special_validation(special_validation)
{
}
@@ -135,19 +133,18 @@ namespace KeywordValidation
const T& item_value,
std::vector<ValidationError>& errors) const;
void validateKeywordItems(const DeckKeyword& keyword,
const SupportedKeywords& keyword_items,
std::vector<ValidationError>& errors) const;
template <typename T>
void validateKeywordItems(const DeckKeyword& keyword,
const SupportedKeywords<T>& supported_options,
const SupportedKeywordItems<T>& supported_options,
std::vector<ValidationError>& errors) const;
const UnsupportedKeywords m_unsupported_keywords;
const SupportedKeywords<std::string> m_partially_supported_string_items;
const SupportedKeywords<int> m_partially_supported_int_items;
const SupportedKeywords<double> m_partially_supported_double_items;
const SupportedKeywords<std::string> m_fully_supported_string_items;
const SupportedKeywords<int> m_fully_supported_int_items;
const SupportedKeywords<double> m_fully_supported_double_items;
const SupportedKeywords m_partially_supported_keywords;
const SupportedKeywords m_fully_supported_keywords;
const std::unordered_map<std::string, ValidationFunction> m_special_validation;
};

View File

@@ -28,10 +28,10 @@ namespace Opm::FlowKeywordValidation
{
template <>
const SupportedKeywords<std::string>&
const SupportedKeywordItems<std::string>&
fullySupported()
{
static const SupportedKeywords<std::string> fully_supported_keywords_strings = {
static const SupportedKeywordItems<std::string> fully_supported_keywords_strings = {
{
"NEXTSTEP",
{
@@ -52,20 +52,20 @@ fullySupported()
template <>
const SupportedKeywords<int>&
const SupportedKeywordItems<int>&
fullySupported()
{
static const SupportedKeywords<int> fully_supported_keywords_int = {
static const SupportedKeywordItems<int> fully_supported_keywords_int = {
};
return fully_supported_keywords_int;
}
template <>
const SupportedKeywords<double>&
const SupportedKeywordItems<double>&
fullySupported()
{
static const SupportedKeywords<double> fully_supported_keywords_double = {
static const SupportedKeywordItems<double> fully_supported_keywords_double = {
{
"WPIMULT",
{

View File

@@ -47,7 +47,7 @@ namespace Opm::FlowKeywordValidation
{
template <typename T>
const KeywordValidation::SupportedKeywords<T>& fullySupported();
const KeywordValidation::SupportedKeywordItems<T>& fullySupported();
} // namespace Opm::FlowKeywordValidation

View File

@@ -28,10 +28,10 @@ namespace Opm::FlowKeywordValidation
{
template <>
const SupportedKeywords<std::string>&
const SupportedKeywordItems<std::string>&
partiallySupported()
{
static const SupportedKeywords<std::string> partially_supported_keywords_strings = {
static const SupportedKeywordItems<std::string> partially_supported_keywords_strings = {
{
"BRANPROP",
{
@@ -344,10 +344,10 @@ partiallySupported()
}
template <>
const KeywordValidation::SupportedKeywords<int>&
const KeywordValidation::SupportedKeywordItems<int>&
partiallySupported()
{
static const KeywordValidation::SupportedKeywords<int>partially_supported_keywords_int = {
static const KeywordValidation::SupportedKeywordItems<int>partially_supported_keywords_int = {
{
"EDITNNC",
{
@@ -528,10 +528,10 @@ partiallySupported()
}
template <>
const KeywordValidation::SupportedKeywords<double>&
const KeywordValidation::SupportedKeywordItems<double>&
partiallySupported()
{
static const KeywordValidation::SupportedKeywords<double> partially_supported_keywords_double = {
static const KeywordValidation::SupportedKeywordItems<double> partially_supported_keywords_double = {
{
"AQUCON",
{

View File

@@ -47,7 +47,7 @@ namespace Opm::FlowKeywordValidation
{
template <typename T>
const KeywordValidation::SupportedKeywords<T>& partiallySupported();
const KeywordValidation::SupportedKeywordItems<T>& partiallySupported();
} // namespace Opm::FlowKeywordValidation

View File

@@ -206,14 +206,22 @@ namespace {
{
Opm::Deck deck(parser.parseFile(deckFilename, parseContext, errorGuard));
auto keyword_validator = Opm::KeywordValidation::KeywordValidator {
Opm::FlowKeywordValidation::unsupportedKeywords(),
Opm::KeywordValidation::SupportedKeywords partiallySupported {
Opm::FlowKeywordValidation::partiallySupported<std::string>(),
Opm::FlowKeywordValidation::partiallySupported<int>(),
Opm::FlowKeywordValidation::partiallySupported<double>(),
Opm::FlowKeywordValidation::partiallySupported<double>()
};
Opm::KeywordValidation::SupportedKeywords fullySupported {
Opm::FlowKeywordValidation::fullySupported<std::string>(),
Opm::FlowKeywordValidation::fullySupported<int>(),
Opm::FlowKeywordValidation::fullySupported<double>(),
Opm::FlowKeywordValidation::fullySupported<double>()
};
auto keyword_validator = Opm::KeywordValidation::KeywordValidator {
Opm::FlowKeywordValidation::unsupportedKeywords(),
partiallySupported,
fullySupported,
Opm::KeywordValidation::specialValidation()
};

View File

@@ -40,7 +40,7 @@ const UnsupportedKeywords test_unsupported_keywords = {
};
const SupportedKeywords<std::string> test_string_items = {
const SupportedKeywordItems<std::string> test_string_items = {
{
"PINCH",
{
@@ -58,7 +58,7 @@ const SupportedKeywords<std::string> test_string_items = {
};
const SupportedKeywords<int> test_int_items = {
const SupportedKeywordItems<int> test_int_items = {
{
"ENDSCALE",
{
@@ -76,7 +76,7 @@ const SupportedKeywords<int> test_int_items = {
};
const SupportedKeywords<double> test_double_items = {
const SupportedKeywordItems<double> test_double_items = {
{
"EHYSTR",
{
@@ -86,7 +86,8 @@ const SupportedKeywords<double> test_double_items = {
},
};
const SupportedKeywords partiallySupported { test_string_items, test_int_items, test_double_items };
const SupportedKeywords fullySupported { {}, {}, {} };
BOOST_AUTO_TEST_CASE(non_critical_keyword)
{
@@ -95,7 +96,7 @@ ECHO
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ECHO"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(!errors[0].critical);
@@ -112,7 +113,7 @@ NOECHO
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["NOECHO"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors[0].critical);
@@ -130,7 +131,7 @@ PINCH
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["PINCH"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(!errors[0].critical);
@@ -148,7 +149,7 @@ PINCH
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["PINCH"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors[0].critical);
@@ -166,7 +167,7 @@ ENDSCALE
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(!errors[0].critical);
@@ -184,7 +185,7 @@ ENDSCALE
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors[0].critical);
@@ -201,7 +202,7 @@ EHYSTR
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["EHYSTR"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors.size() == 0);
@@ -215,7 +216,7 @@ EHYSTR
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["EHYSTR"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(!errors[0].critical);
@@ -232,7 +233,7 @@ EHYSTR
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["EHYSTR"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(!errors[0].critical);
@@ -252,7 +253,7 @@ ENDSCALE
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword1 = deck["PINCH"].back();
const auto& test_keyword2 = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword1, errors);
validator.validateDeckKeyword(test_keyword2, errors);
@@ -274,7 +275,7 @@ ECHO
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ECHO"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, true, false);
@@ -293,7 +294,7 @@ ECHO
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ECHO"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, false, true);
@@ -308,7 +309,7 @@ NOECHO
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["NOECHO"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, false, true);
@@ -326,7 +327,7 @@ NOECHO
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["NOECHO"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, true, false);
@@ -342,7 +343,7 @@ PINCH
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["PINCH"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, true, false);
@@ -365,7 +366,7 @@ COMPDAT
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["COMPDAT"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, true, false);
@@ -386,7 +387,7 @@ PINCH
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["PINCH"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, false, true);
@@ -402,7 +403,7 @@ ENDSCALE
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, true, false);
@@ -421,7 +422,7 @@ ENDSCALE
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, false, true);
@@ -437,7 +438,7 @@ ENDSCALE
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, false, true);
@@ -456,7 +457,7 @@ ENDSCALE
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
const auto report = get_error_report(errors, true, false);
@@ -479,7 +480,7 @@ ENDSCALE
const auto& test_keyword2 = deck["NOECHO"].back();
const auto& test_keyword3 = deck["PINCH"].back();
const auto& test_keyword4 = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword1, errors);
validator.validateDeckKeyword(test_keyword2, errors);
@@ -510,7 +511,7 @@ ENDSCALE
const auto& test_keyword2 = deck["NOECHO"].back();
const auto& test_keyword3 = deck["PINCH"].back();
const auto& test_keyword4 = deck["ENDSCALE"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword1, errors);
validator.validateDeckKeyword(test_keyword2, errors);
@@ -535,7 +536,7 @@ EQLOPTS
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["EQLOPTS"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors.size() == 0);
@@ -551,7 +552,7 @@ EQLOPTS
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["EQLOPTS"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors.size() == 1);
@@ -570,7 +571,7 @@ EQLOPTS
)"};
const auto deck = Parser {}.parseString(keywords_string);
const auto& test_keyword = deck["EQLOPTS"].back();
KeywordValidator validator(test_unsupported_keywords, test_string_items, test_int_items, test_double_items, {}, {}, {}, {});
KeywordValidator validator(test_unsupported_keywords, partiallySupported, fullySupported, {});
std::vector<ValidationError> errors;
validator.validateDeckKeyword(test_keyword, errors);
BOOST_CHECK(errors.size() == 0);