-
Notifications
You must be signed in to change notification settings - Fork 1
FEAT: implement yaml schemas #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
102ecdc
add method validate
cc46387
add iterator for ConfigNodeWithPreset
cf91c82
resolve conversations without tests
cc96bdc
Merge branch 'main' into issue-535
S1mpotyaga 5a4a4fa
check hook
a5e38ee
check hook
6b59ef6
check hooks
9b700b1
Merge branch 'main' into issue-535
S1mpotyaga 203fcc7
Merge branch 'main' into issue-535
S1mpotyaga 40e6cf7
need to correct test
9d1922a
done
9bc87bc
resolve conversations. add SchemaServer
54d46f2
retell messages about errors
d82f004
done
0b0e6be
Merge branch 'main' into issue-535
S1mpotyaga 1d2c05f
fix
ce0f490
Merge branch 'main' into issue-535
S1mpotyaga 7504f48
resolved last conversations
d07bafd
fix
9ac8928
fix
b7919d3
done
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| #include "validator.hpp" | ||
|
|
||
| #include <regex> | ||
| #include <unordered_set> | ||
|
|
||
| namespace sim { | ||
|
|
||
| bool is_meta_field(const std::string& field) { | ||
| return !field.empty() && field[0] == '_'; | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| void validate_field(const YAML::Node& schema_node, | ||
| const ConfigNodeWithPreset& config_node) { | ||
| if (schema_node["_type"]) { | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| std::string type = schema_node["_type"].as<std::string>(); | ||
| if (type == "size_t") { | ||
| config_node.as_or_throw<size_t>(); | ||
| } else if (type == "int") { | ||
| config_node.as_or_throw<int>(); | ||
| } else if (type == "double") { | ||
| config_node.as_or_throw<double>(); | ||
| } else if (type == "bool") { | ||
| config_node.as_or_throw<bool>(); | ||
| } else if (type == "string") { | ||
| config_node.as_or_throw<std::string>(); | ||
| } else if (type == "regex") { | ||
| std::string pattern = config_node.as_or_throw<std::string>(); | ||
| try { | ||
| std::regex r(pattern); | ||
| } catch (const std::regex_error&) { | ||
| throw std::runtime_error("Incorrect type specified std::regex"); | ||
| } | ||
| } else if (type.ends_with(".schema")) { | ||
| YAML::Node sub_schema = YAML::LoadFile(type); | ||
| if (!config_node.IsMap()) { | ||
| throw std::runtime_error(config_node.get_name_or_throw() + | ||
| " must be object."); | ||
| } | ||
| validate(sub_schema, config_node); | ||
| } else { | ||
| throw std::runtime_error("Unknown type: " + type); | ||
| } | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| } else { | ||
| if (!config_node.IsMap()) { | ||
| throw std::runtime_error(config_node.get_name_or_throw() + | ||
| " must be object."); | ||
| } | ||
| validate(schema_node, config_node); | ||
| } | ||
| } | ||
|
|
||
| void validate(const YAML::Node& schema_node, | ||
| const ConfigNodeWithPreset& config_node) { | ||
| // create set of requried fields | ||
| std::unordered_set<std::string> schema_fields; | ||
| for (auto it : schema_node) { | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| std::string field = it.first.as<std::string>(); | ||
| if (!is_meta_field(field)) { | ||
| schema_fields.insert(field); | ||
| } | ||
| } | ||
|
|
||
| // check if config node has unknown fields | ||
| for (const auto& node : config_node) { | ||
| std::string field = node.get_name_or_throw(); | ||
| if (!schema_fields.contains(field)) { | ||
| std::stringstream ss; | ||
| ss << "Config node: " << config_node.get_name_or_throw(); | ||
| ss << "has unknown field: " << field; | ||
| throw std::runtime_error(ss.str()); | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| // check if config node has all required fields | ||
| for (auto it : schema_node) { | ||
| std::string field_schema = it.first.as<std::string>(); | ||
| if (is_meta_field(field_schema)) { | ||
| continue; | ||
| } | ||
| ConfigNodeWithPresetExpected field_config = config_node[field_schema]; | ||
| if (!field_config.has_value()) { | ||
| std::stringstream ss; | ||
| ss << "Config node: " << config_node.get_name_or_throw(); | ||
| ss << "hasn't required field: " << field_schema; | ||
| throw std::runtime_error(ss.str()); | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| for (auto it : schema_node) { | ||
| std::string field = it.first.as<std::string>(); | ||
| if (!is_meta_field(field)) { | ||
| validate_field(it.second, config_node[field].value()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace sim | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| #pragma once | ||
|
|
||
| #include <yaml-cpp/yaml.h> | ||
|
|
||
| #include "parser/config_reader/config_node_with_preset.hpp" | ||
|
|
||
| namespace sim { | ||
|
|
||
| void validate(const YAML::Node& schema_node, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
|
||
| void validate_field(const YAML::Node& schema_node, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
|
|
||
| } // namespace sim | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| node: | ||
| field1: | ||
| _type: size_t | ||
| field2: | ||
| _type: int | ||
| field3: | ||
| _type: double | ||
| field4: | ||
| _type: bool | ||
| field5: | ||
| _type: string | ||
| field6: | ||
| _type: regex |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| node: | ||
| field1: 12389 | ||
| field2: 21039 | ||
| field3: 1.35 | ||
| field4: false | ||
| field5: hello | ||
| field6: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| node: | ||
| field1: hello | ||
| field2: hello | ||
| field3: hello | ||
| field4: hello | ||
| field5: hello | ||
| field6: 1.25 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| #include <gtest/gtest.h> | ||
|
S1mpotyaga marked this conversation as resolved.
S1mpotyaga marked this conversation as resolved.
|
||
| #include <yaml-cpp/yaml.h> | ||
|
|
||
| #include "config_schema/validator.hpp" | ||
|
|
||
| namespace sim { | ||
| namespace test2 { | ||
|
|
||
| TEST(TestBasicTypes, BasicTypes) { | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| std::filesystem::path current_dir = | ||
| std::filesystem::path(__FILE__).parent_path(); | ||
| ConfigNodeWithPreset node = | ||
| load_file_with_presets(current_dir / "basic_types.yml"); | ||
| YAML::Node schema_node = | ||
| YAML::LoadFile(current_dir.append("basic_types.schema")); | ||
|
S1mpotyaga marked this conversation as resolved.
Outdated
|
||
| ASSERT_NO_THROW(validate(schema_node, node)); | ||
| } | ||
|
|
||
| TEST(TestBasicTypes, WrongBasicTypes) { | ||
| std::filesystem::path current_dir = | ||
| std::filesystem::path(__FILE__).parent_path(); | ||
| ConfigNodeWithPreset node = | ||
| load_file_with_presets(current_dir / "basic_types_wrong.yml"); | ||
| YAML::Node schema_node = | ||
| YAML::LoadFile(current_dir.append("basic_types.schema")); | ||
| ASSERT_ANY_THROW(validate(schema_node, node)); | ||
| } | ||
|
|
||
| } // namespace test2 | ||
| } // namespace sim | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.