Skip to content

Commit 83595cb

Browse files
avoid log errors when type is missing but config is initialized (#35)
* avoid log errors when type is missing but config is initialized * simplify logic and add comments * drop commented code * get required logic correct
1 parent f569658 commit 83595cb

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

config_utilities/include/config_utilities/factory.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ std::vector<std::string> convertArguments() {
6161
};
6262
}
6363

64-
//! @brief Helper function to read the type param from a node.
65-
bool getTypeImpl(const YAML::Node& data, std::string& type, const std::string& param_name);
66-
67-
//! @brief Get type from YAML node directly
68-
bool getType(const YAML::Node& data, std::string& type);
64+
/** @brief Helper function to read the type param from a node.
65+
* @param data YAML node to read type from
66+
* @param type Type value to filll
67+
* @param required Whether or not the type field is required
68+
* @param param_name Field in YAML node to read (empty string defaults to Settings().factory_type_param_name)
69+
*/
70+
bool getType(const YAML::Node& data, std::string& type, bool required = true, const std::string& param_name = "");
6971

7072
//! @brief Struct recording typenames for a module (i.e., the constructor signature). Can be used as a map key
7173
struct ModuleInfo {

config_utilities/include/config_utilities/virtual_config.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,16 @@ template <typename BaseT>
205205
void declare_config(VirtualConfig<BaseT>& config) {
206206
auto data = internal::Visitor::visitVirtualConfig(config.isSet(), config.optional_, config.getType());
207207

208+
// underlying derived type is not required if the config is optional, or if the config has been
209+
// initialized to a derived type already (i.e., config_ is already populated)
210+
const bool type_required = !config.optional_ && !config.config_;
211+
208212
// If setting values create the wrapped config using the string identifier.
209213
if (data) {
210214
std::string type;
211-
const bool success = config.optional_ ? internal::getTypeImpl(*data, type, Settings().factory_type_param_name)
212-
: internal::getType(*data, type);
213-
if (success) {
215+
if (internal::getType(*data, type, type_required)) {
214216
config.config_ = internal::ConfigFactory<BaseT>::create(type);
215-
} else if (!config.optional_) {
217+
} else if (type_required) {
216218
std::stringstream ss;
217219
ss << "Could not get type for '" << internal::ModuleInfo::fromTypes<BaseT>().typeInfo() << "'";
218220
internal::Logger::logError(ss.str());

config_utilities/src/factory.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -213,29 +213,33 @@ ModuleRegistry& ModuleRegistry::instance() {
213213
}
214214

215215
// Helper function to read the type param from a node.
216-
bool getTypeImpl(const YAML::Node& data, std::string& type, const std::string& param_name) {
216+
bool getTypeImpl(const YAML::Node& data, std::string& type, const std::string& key) {
217217
if (!data.IsMap()) {
218218
return false;
219219
}
220-
if (!data[param_name]) {
220+
221+
// Get the type or print an error.
222+
if (!data[key]) {
221223
return false;
222224
}
225+
223226
try {
224-
type = data[param_name].as<std::string>();
227+
type = data[key].as<std::string>();
225228
} catch (const YAML::Exception& e) {
226229
return false;
227230
}
231+
228232
return true;
229233
}
230234

231-
bool getType(const YAML::Node& data, std::string& type) {
232-
// Get the type or print an error.
233-
const std::string param_name = Settings::instance().factory_type_param_name;
234-
if (!getTypeImpl(data, type, param_name)) {
235-
Logger::logError("Could not read the param '" + param_name + "' to deduce the type of the module to create.");
236-
return false;
235+
bool getType(const YAML::Node& data, std::string& type, bool required, const std::string& param_name) {
236+
const std::string key = param_name.empty() ? Settings::instance().factory_type_param_name : param_name;
237+
const auto success = getTypeImpl(data, type, key);
238+
if (!success && required) {
239+
Logger::logError("Could not read the param '" + key + "' to deduce the type of the module to create.");
237240
}
238-
return true;
241+
242+
return success;
239243
}
240244

241245
} // namespace config::internal

0 commit comments

Comments
 (0)