entity-manager: allow non-numeric substitution for name
Currently EntityManager crashes if it can't parse template substitution
as numeric variable. This prevent to use test string substitutions there.
Tested: use string for template substitution in configuration name:
{
"Name": "$name",
"Probe": [
"com.yadro.HWManager.Fan({'part_number': 'ASMFAN781101A'})"
],
.....
}
Where name is string:
com.yadro.HWManager.Fan interface - - -
.name property s "Sys_Fan1" emits-change writable
.part_number property s "ASMFAN781102A" emits-change writable
Change-Id: I7139a51e9644dba857ff1c542cf127e723182375
Signed-off-by: Igor Kononenko <i.kononenko@yadro.com>
Signed-off-by: Andrei Kartashev <a.kartashev@yadro.com>
diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp
index b617e1d..7cfcc6e 100644
--- a/src/EntityManager.cpp
+++ b/src/EntityManager.cpp
@@ -35,6 +35,7 @@
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
+#include <charconv>
#include <filesystem>
#include <fstream>
#include <iostream>
@@ -1347,10 +1348,15 @@
std::cerr << "Last JSON Illegal\n";
continue;
}
-
- int index = std::stoi(
- nameIt->get<std::string>().substr(indexIdx),
- nullptr, 0);
+ int index = 0;
+ auto str =
+ nameIt->get<std::string>().substr(indexIdx);
+ auto [p, ec] = std::from_chars(
+ str.data(), str.data() + str.size(), index);
+ if (ec != std::errc())
+ {
+ continue; // non-numeric replacement
+ }
usedNames.insert(nameIt.value());
auto usedIt = std::find(indexes.begin(),
indexes.end(), index);