message: correctly unpack variants of enums
When a variant contains multiple types which can be converted
from a string (such as two enums, or an enum and string), the
previous code tried a conversion from the first type in the
variant. When the first type was an enum, this often threw an
exception. When the first type was a string, this turned into
a plain string containing the dbus enum value.
Add special cases to the variant unpacking to detect types
which are convertible from strings and delegate to a template
specialization of 'convert_from_string' which handles variants.
This specialization will attempt all conversions first and then
revert to a string as a fallback.
Fixes openbmc/sdbusplus#52.
Change-Id: Ide5a0030d595fbaf01122fa8a0ecdaa19ad0078c
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/test/server/message_variant.cpp b/test/server/message_variant.cpp
index 2fa44b7..fef1eff 100644
--- a/test/server/message_variant.cpp
+++ b/test/server/message_variant.cpp
@@ -51,6 +51,9 @@
"EnumTwo does not have convert_from_string!");
static_assert(!sdbusplus::message::details::has_convert_from_string_v<size_t>,
"size_t unexpectedly has a convert_from_string!");
+static_assert(sdbusplus::message::details::has_convert_from_string_v<
+ TestIf::PropertiesVariant>,
+ "TestIf::PropertiesVariant does not convert_from_string!");
TEST_F(Object, PlainEnumOne)
{
@@ -74,7 +77,7 @@
TestIf::EnumTwo::TwoB);
}
-TEST_F(Object, DISABLED_VariantAsString)
+TEST_F(Object, VariantAsString)
{
run_test<variant_t>(std::string("Hello"));
}
@@ -84,7 +87,7 @@
run_test<variant_t>(TestIf::EnumOne::OneA);
}
-TEST_F(Object, DISABLED_VariantAsEnumTwo)
+TEST_F(Object, VariantAsEnumTwo)
{
run_test<variant_t>(TestIf::EnumTwo::TwoB);
}