| 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 |
#ifndef GRPC_CORE_LIB_GPRPP_VALIDATION_ERRORS_H |
| 16 |
#define GRPC_CORE_LIB_GPRPP_VALIDATION_ERRORS_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <stddef.h> |
| 21 |
|
| 22 |
#include <map> |
| 23 |
#include <string> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "absl/status/status.h" |
| 28 |
#include "absl/strings/string_view.h" |
| 29 |
|
| 30 |
namespace grpc_core { |
| 31 |
|
| 32 |
// Tracks errors that occur during validation of a data structure (e.g., |
| 33 |
// a JSON object or protobuf message). Errors are tracked based on |
| 34 |
// which field they are associated with. If at least one error occurs |
| 35 |
// during validation, the validation failed. |
| 36 |
// |
| 37 |
// Example usage: |
| 38 |
// |
| 39 |
// absl::StatusOr<std::string> GetFooBar(const Json::Object& json) { |
| 40 |
// ValidationErrors errors; |
| 41 |
// { |
| 42 |
// ValidationErrors::ScopedField field("foo"); |
| 43 |
// auto it = json.object_value().find("foo"); |
| 44 |
// if (it == json.object_value().end()) { |
| 45 |
// errors.AddError("field not present"); |
| 46 |
// } else if (it->second.type() != Json::Type::OBJECT) { |
| 47 |
// errors.AddError("must be a JSON object"); |
| 48 |
// } else { |
| 49 |
// const Json& foo = it->second; |
| 50 |
// ValidationErrors::ScopedField field(".bar"); |
| 51 |
// auto it = foo.object_value().find("bar"); |
| 52 |
// if (it == json.object_value().end()) { |
| 53 |
// errors.AddError("field not present"); |
| 54 |
// } else if (it->second.type() != Json::Type::STRING) { |
| 55 |
// errors.AddError("must be a JSON string"); |
| 56 |
// } else { |
| 57 |
// return it->second.string_value(); |
| 58 |
// } |
| 59 |
// } |
| 60 |
// } |
| 61 |
// return errors.status("errors validating foo.bar"); |
| 62 |
// } |
| 63 |
class ValidationErrors { |
| 64 |
public: |
| 65 |
// Pushes a field name onto the stack at construction and pops it off |
| 66 |
// of the stack at destruction. |
| 67 |
class ScopedField { |
| 68 |
public: |
| 69 |
ScopedField(ValidationErrors* errors, absl::string_view field_name) |
| 70 |
: errors_(errors) { |
| 71 |
errors_->PushField(field_name); |
| 72 |
} |
| 73 |
|
| 74 |
// Not copyable. |
| 75 |
ScopedField(const ScopedField& other) = delete; |
| 76 |
ScopedField& operator=(const ScopedField& other) = delete; |
| 77 |
|
| 78 |
// Movable. |
| 79 |
ScopedField(ScopedField&& other) noexcept |
| 80 |
: errors_(std::exchange(other.errors_, nullptr)) {} |
| 81 |
ScopedField& operator=(ScopedField&& other) noexcept { |
| 82 |
if (errors_ != nullptr) errors_->PopField(); |
| 83 |
errors_ = std::exchange(other.errors_, nullptr); |
| 84 |
return *this; |
| 85 |
} |
| 86 |
|
| 87 |
~ScopedField() { |
| 88 |
if (errors_ != nullptr) errors_->PopField(); |
| 89 |
} |
| 90 |
|
| 91 |
private: |
| 92 |
ValidationErrors* errors_; |
| 93 |
}; |
| 94 |
|
| 95 |
// Records that we've encountered an error associated with the current |
| 96 |
// field. |
| 97 |
void AddError(absl::string_view error) GPR_ATTRIBUTE_NOINLINE; |
| 98 |
|
| 99 |
// Returns true if the current field has errors. |
| 100 |
bool FieldHasErrors() const GPR_ATTRIBUTE_NOINLINE; |
| 101 |
|
| 102 |
// Returns the resulting status of parsing. |
| 103 |
absl::Status status(absl::string_view prefix) const; |
| 104 |
|
| 105 |
// Returns true if there are no errors. |
| 106 |
bool ok() const { return field_errors_.empty(); } |
| 107 |
|
| 108 |
size_t size() const { return field_errors_.size(); } |
| 109 |
|
| 110 |
private: |
| 111 |
// Pushes a field name onto the stack. |
| 112 |
void PushField(absl::string_view ext) GPR_ATTRIBUTE_NOINLINE; |
| 113 |
// Pops a field name off of the stack. |
| 114 |
void PopField() GPR_ATTRIBUTE_NOINLINE; |
| 115 |
|
| 116 |
// Errors that we have encountered so far, keyed by field name. |
| 117 |
// TODO(roth): If we don't actually have any fields for which we |
| 118 |
// report more than one error, simplify this data structure. |
| 119 |
std::map<std::string /*field_name*/, std::vector<std::string>> field_errors_; |
| 120 |
// Stack of field names indicating the field that we are currently |
| 121 |
// validating. |
| 122 |
std::vector<std::string> fields_; |
| 123 |
}; |
| 124 |
|
| 125 |
} // namespace grpc_core |
| 126 |
|
| 127 |
#endif // GRPC_CORE_LIB_GPRPP_VALIDATION_ERRORS_H |