1
#pragma once
2

            
3
#include "envoy/matcher/matcher.h"
4

            
5
#include "absl/status/status.h"
6

            
7
namespace Envoy {
8
namespace Matcher {
9

            
10
/**
11
 * Interface for a validator that is used during match tree construction to validate
12
 * the paths, nodes, subtrees, etc. of the match tree. Currently only node-based (i.e. without
13
 * knowledge of the resulting leaf action) validation of data input is supported.
14
 *
15
 * As the match tree itself can have any structure, this class is used to accumulate a list of
16
 * violations that occur during tree construction. This has the benefit of being able to emit
17
 * an error containing all the violations, not just the first one.
18
 */
19
template <class DataType> class MatchTreeValidationVisitor {
20
public:
21
1713
  virtual ~MatchTreeValidationVisitor() = default;
22

            
23
  // Validates a single DataInput its type_url.
24
2485
  void validateDataInput(const DataInputFactory<DataType>& data_input, absl::string_view type_url) {
25
2485
    auto status = performDataInputValidation(data_input, type_url);
26

            
27
2485
    if (!status.ok()) {
28
9
      errors_.emplace_back(std::move(status));
29
9
    }
30
2485
  }
31

            
32
3214
  const std::vector<absl::Status>& errors() const { return errors_; }
33

            
34
2780
  template <class OnMatchType> void validateOnMatch(const OnMatchType& on_match) {
35
2780
    if (!support_keep_matching_ && on_match.keep_matching()) {
36
2
      errors_.emplace_back(
37
2
          absl::InvalidArgumentError("keep_matching is not supported in this context"));
38
2
    }
39
2780
  }
40

            
41
103
  void setSupportKeepMatching(bool support_keep_matching) {
42
103
    support_keep_matching_ = support_keep_matching;
43
103
  }
44

            
45
protected:
46
  // Implementations would subclass this to specify the validation logic for data inputs,
47
  // returning a helpful error message if validation fails.
48
  virtual absl::Status performDataInputValidation(const DataInputFactory<DataType>& data_input,
49
                                                  absl::string_view type_url) PURE;
50

            
51
private:
52
  std::vector<absl::Status> errors_;
53
  bool support_keep_matching_ = false;
54
};
55
} // namespace Matcher
56
} // namespace Envoy