szaszm commented on a change in pull request #1220:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1220#discussion_r765690424
##########
File path: libminifi/src/core/yaml/CheckRequiredField.cpp
##########
@@ -27,28 +28,48 @@ namespace minifi {
namespace core {
namespace yaml {
-void checkRequiredField(const YAML::Node *yamlNode, const std::string
&fieldName, const std::shared_ptr<logging::Logger>& logger, const std::string
&yamlSection, const std::string &errorMessage) {
- std::string errMsg = errorMessage;
- if (!yamlNode->as<YAML::Node>()[fieldName]) {
- if (errMsg.empty()) {
- const YAML::Node name_node = yamlNode->as<YAML::Node>()["name"];
- // Build a helpful error message for the user so they can fix the
- // invalid YAML config file, using the component name if present
- errMsg =
- name_node ?
- "Unable to parse configuration file for component named '" +
name_node.as<std::string>() + "' as required field '" + fieldName + "' is
missing" :
- "Unable to parse configuration file as required field '" +
fieldName + "' is missing";
- if (!yamlSection.empty()) {
- errMsg += " [in '" + yamlSection + "' section of configuration file]";
- }
- const YAML::Mark mark = yamlNode->Mark();
- if (!mark.is_null()) {
- errMsg += " [line:column, pos at " + std::to_string(mark.line) + ":" +
std::to_string(mark.column) + ", " + std::to_string(mark.pos) + "]";
- }
+bool isFieldPresent(const YAML::Node &yaml_node, std::string_view field_name) {
+ return bool{yaml_node.as<YAML::Node>()[field_name.data()]};
+}
+
+std::string buildErrorMessage(const YAML::Node &yaml_node, const
std::vector<std::string> &alternate_field_names, std::string_view yaml_section)
{
+ const YAML::Node name_node = yaml_node.as<YAML::Node>()["name"];
+ // Build a helpful error message for the user so they can fix the
+ // invalid YAML config file, using the component name if present
+ auto field_list_string = utils::StringUtils::join(", ",
alternate_field_names);
+ std::string err_msg =
+ name_node ?
+ "Unable to parse configuration file for component named '" +
name_node.as<std::string>() + "' as none of the possible required fields [" +
field_list_string + "] is available" :
+ "Unable to parse configuration file as none of the possible required
fields [" + field_list_string + "] is available";
+ if (!yaml_section.empty()) {
+ err_msg += " [in '" + std::string(yaml_section) + "' section of
configuration file]";
+ }
+ const YAML::Mark mark = yaml_node.Mark();
+ if (!mark.is_null()) {
+ err_msg += " [line:column, pos at " + std::to_string(mark.line) + ":" +
std::to_string(mark.column) + ", " + std::to_string(mark.pos) + "]";
+ }
+ return err_msg;
+}
+
+void checkRequiredField(const YAML::Node &yaml_node, std::string_view
field_name, std::string_view yaml_section, std::string error_message) {
+ if (!isFieldPresent(yaml_node, field_name)) {
+ if (error_message.empty()) {
+ error_message = buildErrorMessage(yaml_node,
std::vector<std::string>{std::string(field_name)}, yaml_section);
}
- logger->log_error(errMsg.c_str());
- throw std::invalid_argument(errMsg);
+ throw std::invalid_argument(error_message);
+ }
+}
+
+std::string getRequiredField(const YAML::Node &yaml_node, const
std::vector<std::string> &alternate_names, std::string_view yaml_section,
std::string error_message) {
Review comment:
I think it's fine as a string if it's only ever used as a string, but
I'm fine with returning a node ref as well.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]