Skip to content

Commit

Permalink
Add templated function to YamlWriter for supporting compact format (#134
Browse files Browse the repository at this point in the history
)

* Add templated function to YamlWriter for supporting compact format

Signed-off-by: Lucia Echevarria <[email protected]>

* Remove const when passing argument by value

Signed-off-by: Raul Sanchez-Mateos <[email protected]>

---------

Signed-off-by: Lucia Echevarria <[email protected]>
Signed-off-by: Raul Sanchez-Mateos <[email protected]>
Co-authored-by: Raul Sanchez-Mateos <[email protected]>
  • Loading branch information
LuciaEchevarria99 and rsanchez15 authored Oct 11, 2024
1 parent 4b19122 commit 97256ff
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
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,
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,
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,
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,
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

0 comments on commit 97256ff

Please sign in to comment.