OR-Tools  9.6
parse_proto.cc
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include <string>
17 #include <vector>
18 
19 #include "absl/strings/str_cat.h"
20 #include "absl/strings/str_split.h"
21 #include "google/protobuf/io/tokenizer.h"
22 #include "google/protobuf/text_format.h"
23 
24 namespace operations_research {
25 namespace {
26 
27 class TextFormatErrorCollector : public google::protobuf::io::ErrorCollector {
28  public:
29  struct CollectedError {
30  bool warning;
31  int line;
32  int column;
33  std::string message;
34  };
35 
36  TextFormatErrorCollector() = default;
37  ~TextFormatErrorCollector() override = default;
38 
39  void AddError(int line, int column, const std::string& message) override {
40  collected_errors_.push_back({false, line, column, message});
41  }
42  void AddWarning(int line, int column, const std::string& message) override {
43  collected_errors_.push_back({true, line, column, message});
44  }
45 
46  // Returns a string listing each collected error. When an error is associated
47  // with a line number and column number that can be found in `value`, that
48  // error message is shown in context using a caret (^), like:
49  // { good_field: 1 error_field: "bad" }
50  // ^
51  std::string RenderErrorMessage(const absl::string_view value) {
52  std::string message;
53  std::vector<std::string> value_lines = absl::StrSplit(value, '\n');
54  for (const auto& error : collected_errors_) {
55  if (error.warning) {
56  absl::StrAppend(&message, "warning: ");
57  }
58  absl::StrAppend(&message, error.message, "\n");
59  if (error.line >= 0 && error.line < value_lines.size()) {
60  const std::string& error_line = value_lines[error.line];
61  if (error.column >= 0 && error.column < error_line.size()) {
62  // If possible, use ^ to point at error.column in error_line.
63  absl::StrAppend(&message, error_line, "\n");
64  absl::StrAppend(&message, std::string(error.column, ' '), "^\n");
65  }
66  }
67  }
68  return message;
69  }
70 
71  private:
72  std::vector<CollectedError> collected_errors_;
73 };
74 
75 } // namespace
76 
77 bool ParseTextProtoForFlag(const absl::string_view text,
78  google::protobuf::Message* const message_out,
79  std::string* const error_out) {
80  TextFormatErrorCollector errors;
81  google::protobuf::TextFormat::Parser parser;
82  parser.RecordErrorsTo(&errors);
83  const bool success = parser.ParseFromString(std::string(text), message_out);
84  *error_out = errors.RenderErrorMessage(text);
85  return success;
86 }
87 
88 } // namespace operations_research
int64_t value
Collection of objects used to extend the Constraint Solver library.
bool ParseTextProtoForFlag(const absl::string_view text, google::protobuf::Message *const message_out, std::string *const error_out)
Definition: parse_proto.cc:77
bool warning
Definition: parse_proto.cc:30
std::string message
Definition: parse_proto.cc:33
int line
Definition: parse_proto.cc:31
int column
Definition: parse_proto.cc:32