Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[20274] Validate YAML tags on parsing #85

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <cpp_utils/logging/BaseLogConfiguration.hpp>
#include <cpp_utils/logging/LogFilter.hpp>
#include <cpp_utils/time/time_utils.hpp>
#include <cpp_utils/types/Fuzzy.hpp>
#include <cpp_utils/utils.hpp>
Expand Down
6 changes: 3 additions & 3 deletions ddspipe_core/src/cpp/configuration/CommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace core {

CommandlineArgs::CommandlineArgs()
{
log_filter[utils::VerbosityKind::Info].set_value("DDSPIPE", utils::FuzzyLevelValues::fuzzy_level_default);
log_filter[utils::VerbosityKind::Warning].set_value("DDSPIPE", utils::FuzzyLevelValues::fuzzy_level_default);
log_filter[utils::VerbosityKind::Error].set_value("", utils::FuzzyLevelValues::fuzzy_level_default);
log_filter.info.set_value("DDSPIPE", utils::FuzzyLevelValues::fuzzy_level_default);
log_filter.warning.set_value("DDSPIPE", utils::FuzzyLevelValues::fuzzy_level_default);
log_filter.error.set_value("", utils::FuzzyLevelValues::fuzzy_level_default);
}

bool CommandlineArgs::is_valid(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ TEST_F(DdsLogConsumerTest, publish_logs)

// Filter out every log except ours
log_configuration.verbosity = utils::VerbosityKind::Info;
log_configuration.filter[utils::VerbosityKind::Info].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Warning].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Error].set_value("DDSPIPE_TEST");
log_configuration.filter.info.set_value("DDSPIPE_TEST");
log_configuration.filter.warning.set_value("DDSPIPE_TEST");
log_configuration.filter.error.set_value("DDSPIPE_TEST");

utils::Log::ClearConsumers();
utils::Log::SetVerbosity(log_configuration.verbosity);
Expand Down Expand Up @@ -205,9 +205,9 @@ TEST_F(DdsLogConsumerTest, dont_publish_logs)

// Filter out every log except ours
log_configuration.verbosity = utils::VerbosityKind::Info;
log_configuration.filter[utils::VerbosityKind::Info].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Warning].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Error].set_value("DDSPIPE_TEST");
log_configuration.filter.info.set_value("DDSPIPE_TEST");
log_configuration.filter.warning.set_value("DDSPIPE_TEST");
log_configuration.filter.error.set_value("DDSPIPE_TEST");

utils::Log::ClearConsumers();
utils::Log::SetVerbosity(log_configuration.verbosity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ TEST(StdLogConsumerTest, print_logs)

// Filter out every log except ours
log_configuration.verbosity = utils::VerbosityKind::Info;
log_configuration.filter[utils::VerbosityKind::Info].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Warning].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Error].set_value("DDSPIPE_TEST");
log_configuration.filter.info.set_value("DDSPIPE_TEST");
log_configuration.filter.warning.set_value("DDSPIPE_TEST");
log_configuration.filter.error.set_value("DDSPIPE_TEST");

utils::Log::ClearConsumers();
utils::Log::SetVerbosity(log_configuration.verbosity);
Expand Down Expand Up @@ -102,9 +102,9 @@ TEST(StdLogConsumerTest, dont_print_logs)

// Filter out every log except ours
log_configuration.verbosity = utils::VerbosityKind::Info;
log_configuration.filter[utils::VerbosityKind::Info].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Warning].set_value("DDSPIPE_TEST");
log_configuration.filter[utils::VerbosityKind::Error].set_value("DDSPIPE_TEST");
log_configuration.filter.info.set_value("DDSPIPE_TEST");
log_configuration.filter.warning.set_value("DDSPIPE_TEST");
log_configuration.filter.error.set_value("DDSPIPE_TEST");

utils::Log::ClearConsumers();
utils::Log::SetVerbosity(log_configuration.verbosity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LogMonitorStatusTest : public testing::Test

utils::BaseLogConfiguration log_conf;
log_conf.verbosity = utils::VerbosityKind::Info;
log_conf.filter[utils::VerbosityKind::Info].set_value("MONITOR_DATA");
log_conf.filter.info.set_value("MONITOR_DATA");

utils::Log::SetVerbosity(log_conf.verbosity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LogMonitorTopicsTest : public testing::Test

utils::BaseLogConfiguration log_conf;
log_conf.verbosity = utils::VerbosityKind::Info;
log_conf.filter[utils::VerbosityKind::Info].set_value("MONITOR_DATA");
log_conf.filter.info.set_value("MONITOR_DATA");

utils::Log::SetVerbosity(log_conf.verbosity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bool XmlHandlerConfiguration::is_valid(
{
if (!utils::is_file_accessible(file.c_str(), utils::FileAccessMode::read))
{
error_msg << "File " << file << " has not exist or does not have read access. ";
error_msg << "File " << file << " does not exist or does not have read access. ";
return false;
}
}
Expand Down
26 changes: 23 additions & 3 deletions ddspipe_yaml/include/ddspipe_yaml/YamlReader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@

#pragma once

#include <cpp_utils/types/Fuzzy.hpp>
#include <functional>

#include <cpp_utils/enum/EnumBuilder.hpp>
#include <cpp_utils/types/Fuzzy.hpp>

#include <ddspipe_yaml/library/library_dll.h>
#include <ddspipe_yaml/Yaml.hpp>
Expand Down Expand Up @@ -167,13 +169,31 @@ class
const Yaml& yml,
const TagType& tag);

//! TODO comment
/**
* @brief Validate \c yml and build the object \c T
*
* This method calls \c get with the default validation function.
*
* @tparam T type of the object to build
* @param yml base yaml
* @param version configuration version
*/
template <typename T>
static T get(
const Yaml& yml,
const YamlReaderVersion version);

//! Get element inside \c tag
/**
* @brief Extracts the sub-yaml from the \c tag and then builds the object \c T
*
* This method calls \c get_value_in_tag to extract the sub-yaml from the \c tag and then it calls \c get to build
* and validate the object \c T.
*
* @tparam T type of the object to build
* @param yml base yaml
* @param tag key to yaml containing the object
* @param version configuration version
*/
template <typename T>
static T get(
const Yaml& yml,
Expand Down
78 changes: 78 additions & 0 deletions ddspipe_yaml/include/ddspipe_yaml/YamlValidator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <set>

#include <ddspipe_yaml/library/library_dll.h>
#include <ddspipe_yaml/Yaml.hpp>
#include <ddspipe_yaml/YamlReader.hpp>

namespace eprosima {
namespace ddspipe {
namespace yaml {

/**
* @brief Yaml Validator
*
* This class is used to validate Yaml objects.
*/
class YamlValidator
LuciaEchevarria99 marked this conversation as resolved.
Show resolved Hide resolved
{
public:

/**
* @brief Validate a Yaml object against a specific version.
*
* For each type, the function should call \c validate_tags with the maximum set of tags \c yml may contain.
*
* @tparam T Type of the object to validate.
* @param yml Yaml object to validate.
* @param version Version to validate against.
*/
DDSPIPE_YAML_DllAPI
template <typename T>
static bool validate(
const Yaml& yml,
const YamlReaderVersion& version = YamlReaderVersion::LATEST);

/**
* @brief Ensure that all the tags in \c yml are unique and inside \c tags.
*
* @param yml Yaml object to validate.
* @param tags Set of tags to validate against.
*/
DDSPIPE_YAML_DllAPI
static bool validate_tags(
const Yaml& yml,
const std::set<TagType>& valid_tags);

protected:

/**
* @brief Get the position of the yaml node in the yaml file
*
* If the position is not available, it will return "unknown position"
*
* @param yml base yaml
* @return "line X, column Y"
*/
static std::string get_position_(
const Yaml& yml);
};

} /* namespace yaml */
} /* namespace ddspipe */
} /* namespace eprosima */
Loading
Loading