File: example.cc

package info (click to toggle)
golang-github-envoyproxy-protoc-gen-validate 1.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,720 kB
  • sloc: java: 1,768; python: 1,203; xml: 893; cpp: 414; makefile: 154; sh: 9
file content (56 lines) | stat: -rw-r--r-- 1,786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <fcntl.h>
#include <unistd.h>

#include <cstdlib>
#include <ostream>

#include "google/protobuf/io/zero_copy_stream_impl.h"
#include "google/protobuf/text_format.h"
#include "foo/bar.pb.h"
#include "foo/bar.pb.validate.h"

int main(const int nargs, const char** const args) {
  if (nargs <= 1) {
    std::cout << "No inputs provided; exiting" << std::endl;
    return EXIT_SUCCESS;
  }

  int success_count = 0;
  for (int i = 1; i < nargs; ++i) {
    pgv::example::foo::Bars bars;
    const auto filename = args[i];
    const auto fd = ::open(filename, O_RDONLY);
    if (fd < 0) {
      std::cerr << "Failed to open file '" << filename << "'" << std::endl;
      continue;
    }
    google::protobuf::io::FileInputStream input(fd);
    input.SetCloseOnDelete(true);

    if (!google::protobuf::TextFormat::Parse(&input, &bars)) {
      std::cerr << "Failed to parse file '" << filename << "' as a "
                << bars.GetDescriptor()->full_name() << " textproto" << std::endl;
      return EXIT_FAILURE;
    }

    pgv::ValidationMsg error_message;
    if (Validate(bars, &error_message)) {
      std::cout << "Successfully validated file '" << filename << "' as a "
                << bars.GetDescriptor()->full_name() << " textproto"
                << std::endl;
      ++success_count;
    } else {
      std::cerr << "Failed to validate file '" << filename << "' as a "
                << bars.GetDescriptor()->full_name()
                << " textproto: " << error_message << std::endl;
    }
  }

  const int failure_count = nargs - 1 - success_count;
  if (failure_count != 0) {
    std::cerr << "Failed to validate " << failure_count << " file"
              << (failure_count == 1 ? "" : "s") << std::endl;
    return EXIT_FAILURE;
  }
  return EXIT_SUCCESS;
}