Skip to content

Commit 2582b10

Browse files
Apply clang-format to config code
1 parent 4410ed7 commit 2582b10

File tree

14 files changed

+352
-200
lines changed

14 files changed

+352
-200
lines changed

src/core/config/config_registry.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,19 @@ class config_registry {
7474
* @param key Configuration parameter key
7575
* @return Default value for the parameter
7676
*/
77-
template <typename T>
78-
T get_default_value(const std::string &key) const
77+
template <typename T> T get_default_value(const std::string &key) const
7978
{
8079
return get_value_impl<T>(
8180
key, [this](const std::string &k) { return get_default_value_as_any(k); });
8281
}
8382

84-
8583
/**
8684
* @brief Gets configured value. For integer types, also does bounds checking
8785
* @tparam T Value type
8886
* @param key Configuration parameter key
8987
* @return Current value for the parameter
9088
*/
91-
template <typename T>
92-
T get_value(const std::string &key) const
89+
template <typename T> T get_value(const std::string &key) const
9390
{
9491
return get_value_impl<T>(key, [this](const std::string &k) { return get_value_as_any(k); });
9592
}
@@ -99,12 +96,9 @@ class config_registry {
9996
* @param key Configuration parameter key
10097
* @return Current value for the parameter, as any
10198
*/
102-
std::experimental::any get_value_as_any(const std::string &key) const;
99+
std::experimental::any get_value_as_any(const std::string &key) const;
103100

104-
const config_descriptor& get_config_descriptor() const
105-
{
106-
return m_config_descriptor;
107-
}
101+
const config_descriptor &get_config_descriptor() const { return m_config_descriptor; }
108102

109103
private:
110104
std::map<std::string, std::experimental::any> m_config_data;

src/core/config/descriptor_providers/json_descriptor_provider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ std::unique_ptr<parameter_descriptor> json_descriptor_provider::create_descripto
151151

152152
// Create parameter descriptor with default value
153153
auto descriptor = std::make_unique<parameter_descriptor>(*analysis.default_value);
154-
154+
155155
descriptor->set_title(analysis.title);
156156

157157
// Apply constraints if present

src/core/config/descriptor_providers/schema_analyzer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <algorithm>
1414
#include "vlogger/vlogger.h"
1515

16-
1716
static void for_each_oneof_option(json_object *one_of_field,
1817
std::function<void(json_object *)> func)
1918
{
@@ -168,10 +167,16 @@ std::string schema_analyzer::determine_title()
168167
json_object *title_field =
169168
json_utils::try_get_field(m_property_obj, config_strings::schema::JSON_TITLE);
170169
if (!title_field) {
170+
// Enforce title definition only for leafs and arrays. Objects are exempt.
171+
if (determine_value_type() != typeid(json_object *)) {
172+
throw_xlio_exception("Title must be a defined for: " + m_path + " - " +
173+
std::to_string(json_object_get_type(title_field)));
174+
}
171175
return "TITLE-NOT-SET";
172176
}
173177
if (json_object_get_type(title_field) != json_type_string) {
174-
throw_xlio_exception("Title must be a string for: " + m_path + " - " + std::to_string(json_object_get_type(title_field)));
178+
throw_xlio_exception("Title must be a string for: " + m_path + " - " +
179+
std::to_string(json_object_get_type(title_field)));
175180
}
176181
return json_object_get_string(title_field);
177182
}

src/core/config/descriptors/config_descriptor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ std::type_index config_descriptor::get_parent_expected_type(const std::string &k
8080
return typeid(std::map<std::string, std::experimental::any>);
8181
}
8282

83-
const config_descriptor::parameter_map_t& config_descriptor::get_parameter_map() const
83+
const config_descriptor::parameter_map_t &config_descriptor::get_parameter_map() const
8484
{
8585
return parameter_map;
8686
}

src/core/config/descriptors/config_descriptor.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class config_descriptor {
2222
public:
2323
typedef std::map<std::string, parameter_descriptor> parameter_map_t;
24-
24+
2525
/**
2626
* @brief Default constructor
2727
*/
@@ -58,17 +58,17 @@ class config_descriptor {
5858
std::type_index get_parent_expected_type(const std::string &key) const;
5959

6060
/**
61-
* @brief Gets the parameter map, allowing external users to iterate
61+
* @brief Gets the parameter map, allowing external users to iterate
6262
* over all parameters
6363
* @return The parameter map
6464
*/
65-
const parameter_map_t& get_parameter_map() const;
65+
const parameter_map_t &get_parameter_map() const;
6666

6767
private:
6868
/**
6969
* @brief Map from parameter name to its descriptor
7070
*/
71-
parameter_map_t parameter_map;
71+
parameter_map_t parameter_map;
7272

7373
/**
7474
* @brief Set of all parameter keys for efficient prefix-based lookups

src/core/config/descriptors/parameter_descriptor.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
#include <climits>
1313
#include <sstream>
1414
#include <cctype>
15+
#include <numeric>
1516

16-
void parameter_descriptor::set_title(const std::string& title)
17+
void parameter_descriptor::set_title(const std::string &title)
1718
{
1819
m_title = title;
1920
}
2021

21-
const std::string& parameter_descriptor::get_title() const
22+
const std::string &parameter_descriptor::get_title() const
2223
{
2324
return m_title;
2425
}
@@ -207,23 +208,19 @@ std::experimental::any parameter_descriptor::convert_string_to_int64(const std::
207208
return it->second;
208209
}
209210

210-
// If no string mappings are defined, throw an exception which has no further information
211+
// If no string mappings are defined, throw an exception which has no further information
211212
if (m_string_mapping.empty()) {
212213
throw std::experimental::bad_any_cast();
213214
}
214215

215-
// We have string mappings but value is not one of them - make an effort and create a nice error message
216-
std::string valid_values;
216+
// We have string mappings but value is not one of them - make an effort and create a nice error
217+
// message
218+
std::string valid_values = std::accumulate(
219+
next(m_string_mapping.begin()), m_string_mapping.end(), m_string_mapping.begin()->first,
220+
[](const std::string &a, const auto &b) { return a + "," + b.first; });
217221

218-
for (it = m_string_mapping.begin(); it != m_string_mapping.end(); ++it) {
219-
valid_values += it->first;
220-
if (std::next(it) != m_string_mapping.end()) { // avoid trailing delimiter
221-
valid_values += ",";
222-
}
223-
}
224-
225-
//throw std::runtime_error so we can supply a usefull error message
226-
throw std::runtime_error("Invalid value for " + m_title + ": " + val + ", not one of: [" + valid_values + "]");
222+
throw std::runtime_error("Invalid value for " + m_title + ": " + val + ", not one of: [" +
223+
valid_values + "]");
227224
}
228225

229226
std::experimental::any parameter_descriptor::get_value(bool val) const
@@ -258,14 +255,17 @@ std::string parameter_descriptor::convert_int64_to_mapped_string(int64_t val) co
258255
if (m_string_mapping.empty()) {
259256
return std::string();
260257
}
261-
258+
262259
for (const auto &mapping : m_string_mapping) {
263260
if ((mapping.second.type() == typeid(int64_t)) &&
264261
(std::experimental::any_cast<int64_t>(mapping.second) == val)) {
265262
return mapping.first;
266263
}
267264
}
268-
return std::to_string(val) + " (Invalid value)";
265+
266+
//throw std::runtime_error so we can supply a usefull error message
267+
throw std::runtime_error("Invalid value for " + m_title + ": " + std::to_string(val) +
268+
" is not valid");
269269
}
270270

271271
std::experimental::any parameter_descriptor::get_value(int64_t val) const

src/core/config/descriptors/parameter_descriptor.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ class parameter_descriptor {
8989
* @param title Title of the parameter
9090
*/
9191

92-
void set_title(const std::string& title);
92+
void set_title(const std::string &title);
9393
/**
9494
* @brief Gets the title of the parameter
9595
* @return Title of the parameter
9696
*/
97-
const std::string& get_title() const;
97+
const std::string &get_title() const;
9898

9999
/**
100100
* @brief Validates a value against all constraints
@@ -154,8 +154,8 @@ class parameter_descriptor {
154154
* @brief Converts an integer to a string per the string mappings set by set_string_mappings().
155155
* @param val Integer value to convert
156156
* @return Converted value. Empty string means no string mappings were set.
157-
If mappings were set, but the value is not in the mappings, returns the value as a string
158-
followed by "(Invalid value)".
157+
If mappings were set, but the value is not in the mappings, returns the value as a
158+
string followed by "(Invalid value)".
159159
*/
160160
std::string convert_int64_to_mapped_string(int64_t val) const;
161161

@@ -183,7 +183,7 @@ class parameter_descriptor {
183183
std::map<std::string, std::experimental::any> m_string_mapping; /**< String-to-value mappings */
184184
value_transformer_t m_value_transformer; /**< Value transformation function */
185185
std::type_index m_type;
186-
std::string m_title; /**< Title of the parameter as defined in schema */
186+
std::string m_title; /**< Title of the parameter as defined in schema */
187187

188188
/**
189189
* @brief Parses a memory size string with suffixes (KB, MB, GB)

0 commit comments

Comments
 (0)