Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 :
38 : #include <nlohmann/json.hpp>
39 :
40 : #include <ert/tracing/Logger.hpp>
41 :
42 : #include <AdminSchemaData.hpp>
43 :
44 :
45 : namespace h2agent
46 : {
47 : namespace model
48 : {
49 :
50 173 : AdminSchemaData::AdminSchemaData() {
51 173 : schema_schema_.setJson(h2agent::adminSchemas::schema); // won't fail
52 173 : }
53 :
54 6 : std::string AdminSchemaData::asJsonString() const {
55 :
56 6 : nlohmann::json result = nlohmann::json::array();
57 :
58 6 : this->forEach([&](const KeyType& k, const ValueType& value) {
59 3 : result.push_back(value->getJson());
60 3 : });
61 :
62 : // Schema configuration is shown as an array regardless if there is 1 item, N items or none ([]):
63 12 : return (result.dump());
64 6 : }
65 :
66 338 : AdminSchemaData::LoadResult AdminSchemaData::loadSingle(const nlohmann::json &j) {
67 :
68 338 : std::string error{};
69 338 : if (!schema_schema_.validate(j, error)) {
70 2 : return BadSchema;
71 : }
72 :
73 : // Schema object to fill:
74 336 : auto schema = std::make_shared<AdminSchema>();
75 :
76 336 : if (schema->load(j)) {
77 : // Push the key in the map:
78 334 : schema_key_t key = schema->getKey();
79 334 : add(key, schema);
80 :
81 334 : return Success;
82 334 : }
83 :
84 2 : return BadContent;
85 338 : }
86 :
87 336 : AdminSchemaData::LoadResult AdminSchemaData::load(const nlohmann::json &j) {
88 :
89 336 : if (j.is_array()) {
90 8 : for (auto it : j) // "it" is of type json::reference and has no key() member
91 : {
92 4 : LoadResult result = loadSingle(it);
93 4 : if (result != Success)
94 1 : return result;
95 4 : }
96 :
97 1 : return Success;
98 : }
99 :
100 334 : return loadSingle(j);
101 : }
102 :
103 8 : std::shared_ptr<AdminSchema> AdminSchemaData::find(const std::string &id) const {
104 :
105 : bool exists;
106 8 : auto result = get(id, exists);
107 :
108 16 : return (exists ? result:nullptr);
109 8 : }
110 :
111 : }
112 : }
113 :
|