-
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 all 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,162 @@ | ||
| #include "schema_server.hpp" | ||
|
|
||
| #include <spdlog/fmt/fmt.h> | ||
|
|
||
| #include <regex> | ||
| #include <unordered_set> | ||
|
|
||
| namespace sim { | ||
|
|
||
| SchemaServer::SchemaServer(const std::filesystem::path& a_schemas_dir) | ||
| : m_schemas_dir(a_schemas_dir) {} | ||
|
|
||
| bool SchemaServer::is_meta_field(const std::string& field) { | ||
| return field.starts_with('_'); | ||
| } | ||
|
|
||
| void SchemaServer::validate_untyped(const ConfigSchema& schema_node, | ||
| const ConfigNodeWithPreset& config_node) { | ||
| // create set of requried fields | ||
| std::unordered_set<std::string> schema_fields; | ||
| for (const auto& it : schema_node) { | ||
| const std::string& field = it.get_name_or_throw(); | ||
| if (!is_meta_field(field)) { | ||
| schema_fields.insert(field); | ||
| } | ||
| } | ||
|
|
||
| // check if config node has unknown fields | ||
| for (const auto& subnode : config_node) { | ||
| const std::string& field = subnode.get_name_or_throw(); | ||
| if (!schema_fields.contains(field)) { | ||
| std::stringstream ss; | ||
| ss << "Node has field `'" << field | ||
| << "' that does not described in schema:\n"; | ||
| ss << schema_node; | ||
| throw subnode.create_parsing_error(ss.str()); | ||
| } | ||
| } | ||
|
|
||
| // check if config node has all required fields | ||
| for (const auto& required_field : schema_fields) { | ||
| ConfigNodeWithPresetExpected exp_field_config = | ||
| config_node[required_field]; | ||
| if (!exp_field_config.has_value()) { | ||
| std::stringstream ss; | ||
| ss << "Missing required field '" << required_field | ||
| << "' described in schema: \n"; | ||
| ss << schema_node << "\n"; | ||
| throw config_node.create_parsing_error(ss.str()); | ||
| } | ||
| } | ||
|
|
||
| for (const auto& it : schema_node) { | ||
| std::string field = it.get_name().value(); | ||
| if (!is_meta_field(field)) { | ||
| validate(schema_node[field].value(), config_node[field].value()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [[nodiscard]] bool SchemaServer::try_validate_basic_types( | ||
| const ConfigSchema& schema_node, const ConfigNodeWithPreset& config_node) { | ||
| const ConfigSchema type_node = schema_node["_type"].value(); | ||
| std::string type = type_node.as_or_throw<std::string>(); | ||
| auto unsafe_cast_config_node_to = [&]<typename T>() -> T { | ||
| auto as_result = config_node.as<T>(); | ||
| if (!as_result.has_value()) { | ||
| std::stringstream ss; | ||
| ss << "Node should contain basic type `" << type | ||
| << "' due to schema:\n"; | ||
| ss << type_node << "\n"; | ||
| ss << "But its not:\n"; | ||
| ss << as_result.error() << '\n'; | ||
| throw config_node.create_parsing_error(ss.str()); | ||
| } | ||
| return as_result.value(); | ||
| }; | ||
| if (type == "size_t") { | ||
| unsafe_cast_config_node_to.operator()<size_t>(); | ||
| } else if (type == "int") { | ||
| unsafe_cast_config_node_to.operator()<int>(); | ||
| } else if (type == "double") { | ||
| unsafe_cast_config_node_to.operator()<double>(); | ||
| } else if (type == "bool") { | ||
| unsafe_cast_config_node_to.operator()<bool>(); | ||
| } else if (type == "string") { | ||
| unsafe_cast_config_node_to.operator()<std::string>(); | ||
| } else if (type == "regex") { | ||
| std::string pattern = | ||
| unsafe_cast_config_node_to.operator()<std::string>(); | ||
| try { | ||
| std::regex r(pattern); | ||
| } catch (const std::regex_error&) { | ||
| std::stringstream ss; | ||
| ss << "Field must contain valid regular expression.\n"; | ||
| ss << "Regex pattern: " << pattern << '\n'; | ||
| throw config_node.create_parsing_error(ss.str()); | ||
| } | ||
| } else { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| [[nodiscard]] bool SchemaServer::try_validate_custom_types( | ||
| const ConfigSchema& schema_node, const ConfigNodeWithPreset& config_node) { | ||
| const ConfigSchema type_node = schema_node["_type"].value(); | ||
| std::string type = type_node.as_or_throw<std::string>(); | ||
| if (type.ends_with(".schema")) { | ||
| std::filesystem::path nested_schema_path = std::filesystem::path(type); | ||
| std::filesystem::path sub_schema_path = | ||
| nested_schema_path.is_absolute() | ||
| ? m_schemas_dir / nested_schema_path.relative_path() | ||
| : std::filesystem::path(schema_node.get_config_path().value()) | ||
| .parent_path() / | ||
| nested_schema_path; | ||
| validate(sub_schema_path, config_node); | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| void SchemaServer::validate(const ConfigSchema& schema_node, | ||
| const ConfigNodeWithPreset& config_node) { | ||
| ConfigNodeExpected exp_type_node = schema_node["_type"]; | ||
| if (exp_type_node) { | ||
| std::string type = exp_type_node.value().as_or_throw<std::string>(); | ||
| if (try_validate_basic_types(schema_node, config_node)) { | ||
| return; | ||
| } | ||
| if (try_validate_custom_types(schema_node, config_node)) { | ||
| return; | ||
| } | ||
| std::stringstream ss; | ||
| throw schema_node.create_parsing_error( | ||
| fmt::format("Unknown specified type '{}'", type)); | ||
| } else { | ||
| if (!config_node.IsMap()) { | ||
| std::stringstream ss; | ||
| ss << "Should be map due to schema\n"; | ||
| ss << schema_node << '\n'; | ||
| throw config_node.create_parsing_error(ss.str()); | ||
|
S1mpotyaga marked this conversation as resolved.
|
||
| } | ||
| validate_untyped(schema_node, config_node); | ||
| } | ||
| } | ||
|
|
||
| void SchemaServer::validate(const std::filesystem::path& schema_path, | ||
| const ConfigNodeWithPreset& config_node) { | ||
| std::filesystem::path full_path = | ||
| schema_path.is_absolute() ? schema_path : m_schemas_dir / schema_path; | ||
| auto exp_sub_schema = safe_load_file(full_path); | ||
| if (!exp_sub_schema) { | ||
| throw config_node.create_parsing_error( | ||
| fmt::format("Failed to parse corresponding schema file: {}", | ||
| exp_sub_schema.error())); | ||
| } | ||
|
S1mpotyaga marked this conversation as resolved.
|
||
| validate(exp_sub_schema.value(), config_node); | ||
| } | ||
|
|
||
| } // 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,41 @@ | ||
| #pragma once | ||
|
|
||
| #include "parser/config_reader/config_node_with_preset.hpp" | ||
|
|
||
| namespace sim { | ||
|
|
||
| using ConfigSchema = ConfigNode; | ||
|
|
||
| class SchemaServer { | ||
| public: | ||
| explicit SchemaServer(const std::filesystem::path& a_schemas_dir); | ||
|
|
||
| void validate(const ConfigSchema& schema_node, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
|
||
| void validate(const std::filesystem::path& schema_path, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
|
||
| private: | ||
| // if schema_node correspond to basic type, validate config_node and returns | ||
| // true otherwise, returns false | ||
| [[nodiscard]] bool try_validate_basic_types( | ||
| const ConfigSchema& schema_node, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
|
||
| // if schema_node correspond to custom type, validate config_node and | ||
| // returns true otherwise, returns false | ||
| [[nodiscard]] bool try_validate_custom_types( | ||
| const ConfigSchema& schema_node, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
S1mpotyaga marked this conversation as resolved.
|
||
|
|
||
| void validate_untyped(const ConfigSchema& schema_node, | ||
| const ConfigNodeWithPreset& config_node); | ||
|
|
||
| static bool is_meta_field(const std::string& field); | ||
|
|
||
| private: | ||
| const std::filesystem::path m_schemas_dir; | ||
| }; | ||
|
|
||
| } // 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
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 @@ | ||
| _type: int |
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,2 @@ | ||
| _type: int | ||
| _doc: Custom type |
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,5 @@ | ||
| node: | ||
|
S1mpotyaga marked this conversation as resolved.
|
||
| field1: | ||
| _type: /custom_type.schema | ||
| field2: | ||
| _type: int | ||
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 @@ | ||
| _type: bool |
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,4 @@ | ||
| absolute_path: | ||
| _type: /absolute_path.schema | ||
| relative_path: | ||
| _type: test_relative_path/relative_path.schema |
1 change: 1 addition & 0 deletions
1
test/config_schema/_schemas/test_paths/test_relative_path/relative_path.schema
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 @@ | ||
| _type: bool |
Oops, something went wrong.
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.