Debian Codegraph @

src/core/lib/json/json_object_loader.cc

raw
1 // Copyright 2020 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <grpc/support/port_platform.h>
16
17 #include "src/core/lib/json/json_object_loader.h"
18
19 #include <utility>
20
21 #include "absl/strings/ascii.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/strip.h"
24
25 namespace grpc_core {
26 namespace json_detail {
27
28 void LoadScalar::LoadInto(const Json& json, const JsonArgs& /*args*/, void* dst,
29 ValidationErrors* errors) const {
30 // We accept either STRING or NUMBER for numeric values, as per
31 // https://developers.google.com/protocol-buffers/docs/proto3#json.
32 if (json.type() != Json::Type::STRING &&
33 (!IsNumber() || json.type() != Json::Type::NUMBER)) {
34 errors->AddError(
35 absl::StrCat("is not a ", IsNumber() ? "number" : "string"));
36 return;
37 }
38 return LoadInto(json.string_value(), dst, errors);
39 }
40
41 bool LoadString::IsNumber() const { return false; }
42
43 void LoadString::LoadInto(const std::string& value, void* dst,
44 ValidationErrors*) const {
45 *static_cast<std::string*>(dst) = value;
46 }
47
48 bool LoadDuration::IsNumber() const { return false; }
49
50 void LoadDuration::LoadInto(const std::string& value, void* dst,
51 ValidationErrors* errors) const {
52 absl::string_view buf(value);
53 if (!absl::ConsumeSuffix(&buf, "s")) {
54 errors->AddError("Not a duration (no s suffix)");
55 return;
56 }
57 buf = absl::StripAsciiWhitespace(buf);
58 auto decimal_point = buf.find('.');
59 int32_t nanos = 0;
60 if (decimal_point != absl::string_view::npos) {
61 absl::string_view after_decimal = buf.substr(decimal_point + 1);
62 buf = buf.substr(0, decimal_point);
63 if (!absl::SimpleAtoi(after_decimal, &nanos)) {
64 errors->AddError("Not a duration (not a number of nanoseconds)");
65 return;
66 }
67 if (after_decimal.length() > 9) {
68 // We don't accept greater precision than nanos.
69 errors->AddError("Not a duration (too many digits after decimal)");
70 return;
71 }
72 for (size_t i = 0; i < (9 - after_decimal.length()); ++i) {
73 nanos *= 10;
74 }
75 }
76 int64_t seconds;
77 if (!absl::SimpleAtoi(buf, &seconds)) {
78 errors->AddError("Not a duration (not a number of seconds)");
79 return;
80 }
81 // Acceptable range for seconds documented at
82 // https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Duration
83 if (seconds < 0 || seconds > 315576000000) {
84 errors->AddError("seconds must be in the range [0, 315576000000]");
85 }
86 *static_cast<Duration*>(dst) =
87 Duration::FromSecondsAndNanoseconds(seconds, nanos);
88 }
89
90 bool LoadNumber::IsNumber() const { return true; }
91
92 void LoadBool::LoadInto(const Json& json, const JsonArgs&, void* dst,
93 ValidationErrors* errors) const {
94 if (json.type() == Json::Type::JSON_TRUE) {
95 *static_cast<bool*>(dst) = true;
96 } else if (json.type() == Json::Type::JSON_FALSE) {
97 *static_cast<bool*>(dst) = false;
98 } else {
99 errors->AddError("is not a boolean");
100 }
101 }
102
103 void LoadUnprocessedJsonObject::LoadInto(const Json& json, const JsonArgs&,
104 void* dst,
105 ValidationErrors* errors) const {
106 if (json.type() != Json::Type::OBJECT) {
107 errors->AddError("is not an object");
108 return;
109 }
110 *static_cast<Json::Object*>(dst) = json.object_value();
111 }
112
113 void LoadVector::LoadInto(const Json& json, const JsonArgs& args, void* dst,
114 ValidationErrors* errors) const {
115 if (json.type() != Json::Type::ARRAY) {
116 errors->AddError("is not an array");
117 return;
118 }
119 const auto& array = json.array_value();
120 const LoaderInterface* element_loader = ElementLoader();
121 for (size_t i = 0; i < array.size(); ++i) {
122 ValidationErrors::ScopedField field(errors, absl::StrCat("[", i, "]"));
123 void* element = EmplaceBack(dst);
124 element_loader->LoadInto(array[i], args, element, errors);
125 }
126 }
127
128 void AutoLoader<std::vector<bool>>::LoadInto(const Json& json,
129 const JsonArgs& args, void* dst,
130 ValidationErrors* errors) const {
131 if (json.type() != Json::Type::ARRAY) {
132 errors->AddError("is not an array");
133 return;
134 }
135 const auto& array = json.array_value();
136 const LoaderInterface* element_loader = LoaderForType<bool>();
137 std::vector<bool>* vec = static_cast<std::vector<bool>*>(dst);
138 for (size_t i = 0; i < array.size(); ++i) {
139 ValidationErrors::ScopedField field(errors, absl::StrCat("[", i, "]"));
140 bool elem = false;
141 element_loader->LoadInto(array[i], args, &elem, errors);
142 vec->push_back(elem);
143 }
144 }
145
146 void LoadMap::LoadInto(const Json& json, const JsonArgs& args, void* dst,
147 ValidationErrors* errors) const {
148 if (json.type() != Json::Type::OBJECT) {
149 errors->AddError("is not an object");
150 return;
151 }
152 const LoaderInterface* element_loader = ElementLoader();
153 for (const auto& pair : json.object_value()) {
154 ValidationErrors::ScopedField field(errors,
155 absl::StrCat("[\"", pair.first, "\"]"));
156 void* element = Insert(pair.first, dst);
157 element_loader->LoadInto(pair.second, args, element, errors);
158 }
159 }
160
161 void LoadOptional::LoadInto(const Json& json, const JsonArgs& args, void* dst,
162 ValidationErrors* errors) const {
163 if (json.type() == Json::Type::JSON_NULL) return;
164 void* element = Emplace(dst);
165 size_t starting_error_size = errors->size();
166 ElementLoader()->LoadInto(json, args, element, errors);
167 if (errors->size() > starting_error_size) Reset(dst);
168 }
169
170 bool LoadObject(const Json& json, const JsonArgs& args, const Element* elements,
171 size_t num_elements, void* dst, ValidationErrors* errors) {
172 if (json.type() != Json::Type::OBJECT) {
173 errors->AddError("is not an object");
174 return false;
175 }
176 for (size_t i = 0; i < num_elements; ++i) {
177 const Element& element = elements[i];
178 if (element.enable_key != nullptr && !args.IsEnabled(element.enable_key)) {
179 continue;
180 }
181 ValidationErrors::ScopedField field(errors,
182 absl::StrCat(".", element.name));
183 const auto& it = json.object_value().find(element.name);
184 if (it == json.object_value().end()) {
185 if (element.optional) continue;
186 errors->AddError("field not present");
187 continue;
188 }
189 char* field_dst = static_cast<char*>(dst) + element.member_offset;
190 element.loader->LoadInto(it->second, args, field_dst, errors);
191 }
192 return true;
193 }
194
195 const Json* GetJsonObjectField(const Json::Object& json,
196 absl::string_view field,
197 ValidationErrors* errors, bool required) {
198 auto it = json.find(std::string(field));
199 if (it == json.end()) {
200 if (required) errors->AddError("field not present");
201 return nullptr;
202 }
203 return &it->second;
204 }
205
206 } // namespace json_detail
207 } // namespace grpc_core