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

Add templated function to YamlWriter for supporting compact format #134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion ddspipe_yaml/include/ddspipe_yaml/YamlWriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,28 @@ void set(
Yaml& yml,
const T& value);

//! Set the \c value in a new yaml in \c yml under \c tag .
/**
* @brief Set a new value in \c yml .
*
* This function is intended to be specialized for different types, defining the method to serialize into YAML
* when two possible formats (compact and extended) are available.
* Depending on the \c is_compact flag, the serialization will be in either compact or extended format.
*
* @param[in,out] yaml base yaml where to write the value
* @param[in] value value to write
* @param[in] is_compact boolean value to set the format of the yaml
*
* @tparam T type of the value to set in the yaml.
*/
template <typename T>
void set(
Yaml& yml,
const T& value,
const bool is_compact);

//! Set the \c value in a new yaml in \c yml under \c tag .
template <typename T>
void set_in_tag(
Yaml& yml,
const TagType& tag,
const T& value);
Expand Down
31 changes: 30 additions & 1 deletion ddspipe_yaml/include/ddspipe_yaml/impl/YamlWriter.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ void set_collection(
Yaml& yml,
const std::vector<T>& collection);

template <typename T>
void set_collection(
Yaml& yml,
const std::vector<T>& collection,
const bool is_compact);

template <typename K, typename T>
void set_map(
Yaml& yml,
Expand Down Expand Up @@ -87,6 +93,15 @@ void set(
set_collection(yml, collection);
}

template <typename T>
void set(
Yaml& yml,
const std::vector<T>& collection,
const bool is_compact)
{
set_collection(yml, collection, is_compact);
}

template <typename T>
void set(
Yaml& yml,
Expand All @@ -108,7 +123,7 @@ void set(
////////////////////////////////////

template <typename T>
void set(
void set_in_tag(
Yaml& yml,
const TagType& tag,
const T& value)
Expand All @@ -130,6 +145,20 @@ void set_collection(
}
}

template <typename T>
void set_collection(
Yaml& yml,
const std::vector<T>& collection,
const bool is_compact)
{
for (const auto& v : collection)
{
Yaml yml_value;
set<T>(yml_value, v, is_compact);
yml.push_back(yml_value);
}
}

template <typename K, typename T>
void set_map(
Yaml& yml,
Expand Down
12 changes: 6 additions & 6 deletions ddspipe_yaml/test/unittest/yaml_writer/YamlWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ void set(
Yaml& yml,
const test::A& a)
{
set(yml, test::A_VALUE_TAG, a.value);
set(yml, test::A_NAME_TAG, a.name);
set_in_tag(yml, test::A_VALUE_TAG, a.value);
set_in_tag(yml, test::A_NAME_TAG, a.name);
}

//! Serialize an object B in a yaml
Expand All @@ -101,8 +101,8 @@ void set(
Yaml& yml,
const test::B& b)
{
set(yml, test::B_ACTIVE_TAG, b.active);
set(yml, test::B_A_TAG, b.a);
set_in_tag(yml, test::B_ACTIVE_TAG, b.active);
set_in_tag(yml, test::B_A_TAG, b.a);
}

//! Deserialize an object A from a yaml
Expand Down Expand Up @@ -393,7 +393,7 @@ TEST(YamlWriterTest, set_specific_type)

// Create yml
Yaml yml;
set(yml, "b", b);
set_in_tag(yml, "b", b);

// Check the b tag exists, and some inside
ASSERT_TRUE(yml["b"]);
Expand All @@ -407,7 +407,7 @@ TEST(YamlWriterTest, set_specific_type)
test::B b2;
b.a.value = 3;
Yaml yml2;
set(yml2, "b", b2);
set_in_tag(yml2, "b", b2);

// Check that ymls are not equal
ASSERT_NE(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ void set(
Yaml& yml,
const test::A& a)
{
set(yml, test::A_VALUE_TAG, a.value);
set(yml, test::A_NAME_TAG, a.name);
set_in_tag(yml, test::A_VALUE_TAG, a.value);
set_in_tag(yml, test::A_NAME_TAG, a.name);
}

template <>
void set(
Yaml& yml,
const test::B& b)
{
set(yml, test::B_ACTIVE_TAG, b.active);
set(yml, test::B_A_TAG, b.a);
set_in_tag(yml, test::B_ACTIVE_TAG, b.active);
set_in_tag(yml, test::B_A_TAG, b.a);
}

template <>
Expand Down
Loading