From fb385e35dee6034d697687c7004bdbc3c89ecaac Mon Sep 17 00:00:00 2001 From: Lucia Echevarria Date: Tue, 1 Oct 2024 11:36:06 +0200 Subject: [PATCH] Add templated function to YamlWriter for supporting compact format Signed-off-by: Lucia Echevarria --- .../include/ddspipe_yaml/YamlWriter.hpp | 21 ++++++++++++- .../include/ddspipe_yaml/impl/YamlWriter.ipp | 31 ++++++++++++++++++- .../unittest/yaml_writer/YamlWriterTest.cpp | 12 +++---- .../YamlWriter_collections_test.cpp | 8 ++--- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/ddspipe_yaml/include/ddspipe_yaml/YamlWriter.hpp b/ddspipe_yaml/include/ddspipe_yaml/YamlWriter.hpp index b88d37b0..cd934599 100644 --- a/ddspipe_yaml/include/ddspipe_yaml/YamlWriter.hpp +++ b/ddspipe_yaml/include/ddspipe_yaml/YamlWriter.hpp @@ -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 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 +void set_in_tag( Yaml& yml, const TagType& tag, const T& value); diff --git a/ddspipe_yaml/include/ddspipe_yaml/impl/YamlWriter.ipp b/ddspipe_yaml/include/ddspipe_yaml/impl/YamlWriter.ipp index 0845b7c0..8eefcb1e 100644 --- a/ddspipe_yaml/include/ddspipe_yaml/impl/YamlWriter.ipp +++ b/ddspipe_yaml/include/ddspipe_yaml/impl/YamlWriter.ipp @@ -48,6 +48,12 @@ void set_collection( Yaml& yml, const std::vector& collection); +template +void set_collection( + Yaml& yml, + const std::vector& collection, + const bool is_compact); + template void set_map( Yaml& yml, @@ -87,6 +93,15 @@ void set( set_collection(yml, collection); } +template +void set( + Yaml& yml, + const std::vector& collection, + const bool is_compact) +{ + set_collection(yml, collection, is_compact); +} + template void set( Yaml& yml, @@ -108,7 +123,7 @@ void set( //////////////////////////////////// template -void set( +void set_in_tag( Yaml& yml, const TagType& tag, const T& value) @@ -130,6 +145,20 @@ void set_collection( } } +template +void set_collection( + Yaml& yml, + const std::vector& collection, + const bool is_compact) +{ + for (const auto& v : collection) + { + Yaml yml_value; + set(yml_value, v, is_compact); + yml.push_back(yml_value); + } +} + template void set_map( Yaml& yml, diff --git a/ddspipe_yaml/test/unittest/yaml_writer/YamlWriterTest.cpp b/ddspipe_yaml/test/unittest/yaml_writer/YamlWriterTest.cpp index 6dbcf73d..8d72a362 100644 --- a/ddspipe_yaml/test/unittest/yaml_writer/YamlWriterTest.cpp +++ b/ddspipe_yaml/test/unittest/yaml_writer/YamlWriterTest.cpp @@ -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 @@ -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 @@ -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"]); @@ -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( diff --git a/ddspipe_yaml/test/unittest/yaml_writer/YamlWriter_collections_test.cpp b/ddspipe_yaml/test/unittest/yaml_writer/YamlWriter_collections_test.cpp index e3dcf230..b92d7679 100644 --- a/ddspipe_yaml/test/unittest/yaml_writer/YamlWriter_collections_test.cpp +++ b/ddspipe_yaml/test/unittest/yaml_writer/YamlWriter_collections_test.cpp @@ -71,8 +71,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); } template <> @@ -80,8 +80,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); } template <>