Line data Source code
1 : #pragma once 2 : 3 : #include "envoy/matcher/matcher.h" 4 : 5 : #include "source/common/matcher/field_matcher.h" 6 : 7 : namespace Envoy { 8 : namespace Matcher { 9 : 10 : /** 11 : * A match tree that iterates over a list of matchers to find the first one that matches. If one 12 : * does, the MatchResult will be the one specified by the individual matcher. 13 : */ 14 : template <class DataType> class ListMatcher : public MatchTree<DataType> { 15 : public: 16 0 : explicit ListMatcher(absl::optional<OnMatch<DataType>> on_no_match) : on_no_match_(on_no_match) {} 17 : 18 0 : typename MatchTree<DataType>::MatchResult match(const DataType& matching_data) override { 19 0 : for (const auto& matcher : matchers_) { 20 0 : const auto maybe_match = matcher.first->match(matching_data); 21 : 22 : // One of the matchers don't have enough information, bail on evaluating the match. 23 0 : if (maybe_match.match_state_ == MatchState::UnableToMatch) { 24 0 : return {MatchState::UnableToMatch, {}}; 25 0 : } 26 : 27 0 : if (maybe_match.result()) { 28 0 : return {MatchState::MatchComplete, matcher.second}; 29 0 : } 30 0 : } 31 : 32 0 : return {MatchState::MatchComplete, on_no_match_}; 33 0 : } 34 : 35 0 : void addMatcher(FieldMatcherPtr<DataType>&& matcher, OnMatch<DataType> action) { 36 0 : matchers_.push_back({std::move(matcher), std::move(action)}); 37 0 : } 38 : 39 : private: 40 : absl::optional<OnMatch<DataType>> on_no_match_; 41 : std::vector<std::pair<FieldMatcherPtr<DataType>, OnMatch<DataType>>> matchers_; 42 : }; 43 : 44 : } // namespace Matcher 45 : } // namespace Envoy