Skip to content

Commit ce453b1

Browse files
committed
Fix uncrustify
Signed-off-by: David Laseca Perez <davidlaseca@eprosima.com>
1 parent b06277a commit ce453b1

3 files changed

Lines changed: 120 additions & 111 deletions

File tree

ddspipe_yaml/include/ddspipe_yaml/YamlValidator.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class DDSPIPE_YAML_DllAPI YamlValidator
6060
const std::string& value);
6161

6262
public:
63-
63+
6464
enum class InputType
6565
{
6666
FROM_STRING,
@@ -98,7 +98,8 @@ class DDSPIPE_YAML_DllAPI YamlValidator
9898
* @brief Move assignment operator. Needed because \c std::unique_ptr needs the full definition
9999
* of \c nlohmann::json_schema::json_validator when move-assigning (the old value is destroyed).
100100
*/
101-
YamlValidator& operator=(YamlValidator&&);
101+
YamlValidator& operator =(
102+
YamlValidator&&);
102103

103104
/**
104105
* @brief Set or replace the root schema of the validator.

ddspipe_yaml/src/cpp/YamlValidator.cpp

Lines changed: 103 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ YamlValidator::YamlValidator()
5858
}
5959

6060
YamlValidator::YamlValidator(
61-
InputType input_type,
62-
const std::string& schema_string)
61+
InputType input_type,
62+
const std::string& schema_string)
6363
: validator(std::make_unique<nlohmann::json_schema::json_validator>(nullptr, format_checker))
6464
{
6565
this->set_schema(input_type, schema_string);
6666
}
6767

6868
YamlValidator::~YamlValidator() = default;
6969

70-
YamlValidator& YamlValidator::operator=(YamlValidator&&) = default;
70+
YamlValidator& YamlValidator::operator =(
71+
YamlValidator&&) = default;
7172

7273
void YamlValidator::set_schema(
7374
InputType input_type,
@@ -77,34 +78,34 @@ void YamlValidator::set_schema(
7778

7879
switch (input_type)
7980
{
80-
case InputType::FROM_FILE: // Treat input string as the path to the file with the schema
81-
try
82-
{
83-
std::ifstream schema_file;
84-
schema_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
85-
schema_file.open(schema_string);
86-
schema = nlohmann::json::parse(schema_file);
87-
}
88-
catch (const std::exception& e)
89-
{
90-
throw eprosima::utils::ConfigurationException(
91-
utils::Formatter() << "Error occured while loading JSON schema from file: "
92-
<< schema_string << " :\n" << e.what());
93-
}
94-
break;
95-
96-
case InputType::FROM_STRING: // Treat input string as the raw schema
97-
try
98-
{
99-
schema = nlohmann::json::parse(schema_string);
100-
}
101-
catch (const std::exception& e)
102-
{
103-
throw eprosima::utils::ConfigurationException(
104-
utils::Formatter() << "Error occured while loading JSON schema from input string:\n"
105-
<< e.what());
106-
}
107-
break;
81+
case InputType::FROM_FILE: // Treat input string as the path to the file with the schema
82+
try
83+
{
84+
std::ifstream schema_file;
85+
schema_file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
86+
schema_file.open(schema_string);
87+
schema = nlohmann::json::parse(schema_file);
88+
}
89+
catch (const std::exception& e)
90+
{
91+
throw eprosima::utils::ConfigurationException(
92+
utils::Formatter() << "Error occured while loading JSON schema from file: "
93+
<< schema_string << " :\n" << e.what());
94+
}
95+
break;
96+
97+
case InputType::FROM_STRING: // Treat input string as the raw schema
98+
try
99+
{
100+
schema = nlohmann::json::parse(schema_string);
101+
}
102+
catch (const std::exception& e)
103+
{
104+
throw eprosima::utils::ConfigurationException(
105+
utils::Formatter() << "Error occured while loading JSON schema from input string:\n"
106+
<< e.what());
107+
}
108+
break;
108109
}
109110

110111
// Set the validator schema
@@ -149,77 +150,78 @@ bool YamlValidator::validate_YAML(
149150

150151
// Create lambda function to convert from YAML to JSON
151152
auto yaml_to_json = [](auto&& self, const Yaml& yml) -> nlohmann::json
152-
{
153-
if (yml.IsNull())
154-
{
155-
return nullptr;
156-
}
157-
158-
if (yml.IsScalar())
159-
{
160-
// The only allowed scalar types are: boolean, integer, number (double) and string
161-
const std::string value = yml.as<std::string>();
162-
163-
if (value == "true")
164153
{
165-
return true;
166-
}
167-
if (value == "false")
168-
{
169-
return false;
170-
}
171-
172-
try
173-
{
174-
return yml.as<int>();
175-
} catch (...)
176-
{
177-
}
178-
try
179-
{
180-
return yml.as<double>();
181-
} catch (...)
182-
{
183-
}
184-
185-
return value;
186-
}
187-
188-
if (yml.IsSequence())
189-
{
190-
nlohmann::json array = nlohmann::json::array();
191-
for (const auto& item : yml)
192-
{
193-
array.push_back(self(self, item));
194-
}
195-
return array;
196-
}
197-
198-
if (yml.IsMap())
199-
{
200-
nlohmann::json object = nlohmann::json::object();
201-
for (const auto& item : yml)
202-
{
203-
std::string key = item.first.as<std::string>();
204-
object[key] = self(self, item.second);
205-
}
206-
return object;
207-
}
208-
209-
std::string yml_as_string;
210-
try
211-
{
212-
yml_as_string = yml.as<std::string>();
213-
}
214-
catch (...)
215-
{
216-
throw eprosima::utils::ConfigurationException("Unsupported YAML file, cannot be converted to JSON.");
217-
}
218-
219-
throw eprosima::utils::ConfigurationException(
220-
utils::Formatter() << "Unsupported YAML file, cannot be converted to JSON.\n"
221-
<< "Error in node: " << yml_as_string);
222-
};
154+
if (yml.IsNull())
155+
{
156+
return nullptr;
157+
}
158+
159+
if (yml.IsScalar())
160+
{
161+
// The only allowed scalar types are: boolean, integer, number (double) and string
162+
const std::string value = yml.as<std::string>();
163+
164+
if (value == "true")
165+
{
166+
return true;
167+
}
168+
if (value == "false")
169+
{
170+
return false;
171+
}
172+
173+
try
174+
{
175+
return yml.as<int>();
176+
} catch (...)
177+
{
178+
}
179+
try
180+
{
181+
return yml.as<double>();
182+
} catch (...)
183+
{
184+
}
185+
186+
return value;
187+
}
188+
189+
if (yml.IsSequence())
190+
{
191+
nlohmann::json array = nlohmann::json::array();
192+
for (const auto& item : yml)
193+
{
194+
array.push_back(self(self, item));
195+
}
196+
return array;
197+
}
198+
199+
if (yml.IsMap())
200+
{
201+
nlohmann::json object = nlohmann::json::object();
202+
for (const auto& item : yml)
203+
{
204+
std::string key = item.first.as<std::string>();
205+
object[key] = self(self, item.second);
206+
}
207+
return object;
208+
}
209+
210+
std::string yml_as_string;
211+
try
212+
{
213+
yml_as_string = yml.as<std::string>();
214+
}
215+
catch (...)
216+
{
217+
throw eprosima::utils::ConfigurationException(
218+
"Unsupported YAML file, cannot be converted to JSON.");
219+
}
220+
221+
throw eprosima::utils::ConfigurationException(
222+
utils::Formatter() << "Unsupported YAML file, cannot be converted to JSON.\n"
223+
<< "Error in node: " << yml_as_string);
224+
};
223225

224226
// Covert YAML to JSON
225227
nlohmann::json instance = yaml_to_json(yaml_to_json, yml);

ddspipe_yaml/test/unittest/yaml_validator/YamlValidatorTest.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ std::string invalid_schema_path_1 = "./test_yaml_files/valid_draft07_schema.json
3434
std::string invalid_schema_path_2 = "./invalid_json_schema.json";
3535
std::string invalid_schema_path_3 = "./invalid_draft07_schema.json";
3636

37-
std::string valid_schema_string= R"(
37+
std::string valid_schema_string =
38+
R"(
3839
{
3940
"$schema":"http://json-schema.org/draft-07/schema#",
4041
"type":"object",
@@ -54,7 +55,8 @@ std::string valid_schema_string= R"(
5455
}
5556
)";
5657

57-
std::string invalid_schema_string= R"(
58+
std::string invalid_schema_string =
59+
R"(
5860
{
5961
"$schema":"http://json-schema.org/draft-07/schema#",
6062
"properties":["not", "an", "object"]
@@ -88,7 +90,7 @@ std::vector<std::string> invalid_files = {
8890
* constructor with path to file: invalid path -> ConfigurationException
8991
* constructor with path to file: valid path and invalid file -> ConfigurationException
9092
* constructor with path to file: valid path and valid file, but not json draft07 -> ConfigurationException
91-
*
93+
*
9294
* constructor with string with file content: valid file -> no exception
9395
* constructor with string with file content: valid file, but not json draft07 -> ConfigurationException
9496
*/
@@ -99,22 +101,25 @@ TEST(YamlValidatorTest, constructors)
99101

100102
// constructor with path to file: valid path and file
101103
{
102-
ASSERT_NO_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::valid_schema_path););
104+
ASSERT_NO_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::valid_schema_path));
103105
}
104106

105107
// constructor with path to file: invalid path
106108
{
107-
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::invalid_schema_path_1), utils::ConfigurationException);
109+
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::invalid_schema_path_1),
110+
utils::ConfigurationException);
108111
}
109112

110113
// constructor with path to file: valid path and invalid file
111114
{
112-
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::invalid_schema_path_2), utils::ConfigurationException);
115+
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::invalid_schema_path_2),
116+
utils::ConfigurationException);
113117
}
114118

115119
// constructor with path to file: valid path and valid file, but not json draft07
116120
{
117-
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::invalid_schema_path_3), utils::ConfigurationException);
121+
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_FILE, test::invalid_schema_path_3),
122+
utils::ConfigurationException);
118123
}
119124

120125
// constructor with string with file content: valid file
@@ -124,7 +129,8 @@ TEST(YamlValidatorTest, constructors)
124129

125130
// constructor with string with file content: valid file, but not json draft07
126131
{
127-
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_STRING, test::invalid_schema_string), utils::ConfigurationException);
132+
ASSERT_THROW(validator = YamlValidator(YamlValidator::InputType::FROM_STRING, test::invalid_schema_string),
133+
utils::ConfigurationException);
128134
}
129135
}
130136

0 commit comments

Comments
 (0)