Coverage Report

Created: 2023-11-12 09:30

/proc/self/cwd/source/common/matcher/map_matcher.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include <string>
4
5
#include "envoy/matcher/matcher.h"
6
7
namespace Envoy {
8
namespace Matcher {
9
10
/**
11
 * Implementation of a map matcher which performs matches against the data provided by DataType.
12
 * If the match could not be completed, {MatchState::UnableToMatch, {}} will be returned. If the
13
 * match result was determined, {MatchState::MatchComplete, OnMatch} will be returned. If a match
14
 * result was determined to be no match, {MatchState::MatchComplete, {}} will be returned.
15
 */
16
template <class DataType>
17
class MapMatcher : public MatchTree<DataType>, Logger::Loggable<Logger::Id::matcher> {
18
public:
19
  MapMatcher(DataInputPtr<DataType>&& data_input, absl::optional<OnMatch<DataType>> on_no_match)
20
8.37k
      : data_input_(std::move(data_input)), on_no_match_(std::move(on_no_match)) {
21
8.37k
    auto input_type = data_input_->dataInputType();
22
8.37k
    if (input_type != DefaultMatchingDataType) {
23
0
      throwEnvoyExceptionOrPanic(
24
0
          absl::StrCat("Unsupported data input type: ", input_type,
25
0
                       ", currently only string type is supported in map matcher"));
26
0
    }
27
8.37k
  }
Envoy::Matcher::MapMatcher<Envoy::Http::HttpMatchingData>::MapMatcher(std::__1::unique_ptr<Envoy::Matcher::DataInput<Envoy::Http::HttpMatchingData>, std::__1::default_delete<Envoy::Matcher::DataInput<Envoy::Http::HttpMatchingData> > >&&, std::__1::optional<Envoy::Matcher::OnMatch<Envoy::Http::HttpMatchingData> >)
Line
Count
Source
20
8.37k
      : data_input_(std::move(data_input)), on_no_match_(std::move(on_no_match)) {
21
8.37k
    auto input_type = data_input_->dataInputType();
22
8.37k
    if (input_type != DefaultMatchingDataType) {
23
0
      throwEnvoyExceptionOrPanic(
24
0
          absl::StrCat("Unsupported data input type: ", input_type,
25
0
                       ", currently only string type is supported in map matcher"));
26
0
    }
27
8.37k
  }
Unexecuted instantiation: Envoy::Matcher::MapMatcher<Envoy::Network::MatchingData>::MapMatcher(std::__1::unique_ptr<Envoy::Matcher::DataInput<Envoy::Network::MatchingData>, std::__1::default_delete<Envoy::Matcher::DataInput<Envoy::Network::MatchingData> > >&&, std::__1::optional<Envoy::Matcher::OnMatch<Envoy::Network::MatchingData> >)
Unexecuted instantiation: Envoy::Matcher::MapMatcher<Envoy::Network::UdpMatchingData>::MapMatcher(std::__1::unique_ptr<Envoy::Matcher::DataInput<Envoy::Network::UdpMatchingData>, std::__1::default_delete<Envoy::Matcher::DataInput<Envoy::Network::UdpMatchingData> > >&&, std::__1::optional<Envoy::Matcher::OnMatch<Envoy::Network::UdpMatchingData> >)
28
29
  // Adds a child to the map.
30
  virtual void addChild(std::string value, OnMatch<DataType>&& on_match) PURE;
31
32
508
  typename MatchTree<DataType>::MatchResult match(const DataType& data) override {
33
508
    const auto input = data_input_->get(data);
34
508
    ENVOY_LOG(trace, "Attempting to match {}", input);
35
508
    if (input.data_availability_ == DataInputGetResult::DataAvailability::NotAvailable) {
36
0
      return {MatchState::UnableToMatch, absl::nullopt};
37
0
    }
38
39
    // Returns `on_no_match` when input data is empty. (i.e., is absl::monostate).
40
508
    if (absl::holds_alternative<absl::monostate>(input.data_)) {
41
120
      return {MatchState::MatchComplete, on_no_match_};
42
120
    }
43
44
388
    const auto result = doMatch(absl::get<std::string>(input.data_));
45
388
    if (result) {
46
146
      if (result->matcher_) {
47
114
        return result->matcher_->match(data);
48
114
      } else {
49
32
        return {MatchState::MatchComplete, OnMatch<DataType>{result->action_cb_, nullptr}};
50
32
      }
51
242
    } else if (input.data_availability_ ==
52
242
               DataInputGetResult::DataAvailability::MoreDataMightBeAvailable) {
53
      // It's possible that we were attempting a lookup with a partial value, so delay matching
54
      // until we know that we actually failed.
55
0
      return {MatchState::UnableToMatch, absl::nullopt};
56
0
    }
57
58
242
    return {MatchState::MatchComplete, on_no_match_};
59
388
  }
Envoy::Matcher::MapMatcher<Envoy::Http::HttpMatchingData>::match(Envoy::Http::HttpMatchingData const&)
Line
Count
Source
32
508
  typename MatchTree<DataType>::MatchResult match(const DataType& data) override {
33
508
    const auto input = data_input_->get(data);
34
508
    ENVOY_LOG(trace, "Attempting to match {}", input);
35
508
    if (input.data_availability_ == DataInputGetResult::DataAvailability::NotAvailable) {
36
0
      return {MatchState::UnableToMatch, absl::nullopt};
37
0
    }
38
39
    // Returns `on_no_match` when input data is empty. (i.e., is absl::monostate).
40
508
    if (absl::holds_alternative<absl::monostate>(input.data_)) {
41
120
      return {MatchState::MatchComplete, on_no_match_};
42
120
    }
43
44
388
    const auto result = doMatch(absl::get<std::string>(input.data_));
45
388
    if (result) {
46
146
      if (result->matcher_) {
47
114
        return result->matcher_->match(data);
48
114
      } else {
49
32
        return {MatchState::MatchComplete, OnMatch<DataType>{result->action_cb_, nullptr}};
50
32
      }
51
242
    } else if (input.data_availability_ ==
52
242
               DataInputGetResult::DataAvailability::MoreDataMightBeAvailable) {
53
      // It's possible that we were attempting a lookup with a partial value, so delay matching
54
      // until we know that we actually failed.
55
0
      return {MatchState::UnableToMatch, absl::nullopt};
56
0
    }
57
58
242
    return {MatchState::MatchComplete, on_no_match_};
59
388
  }
Unexecuted instantiation: Envoy::Matcher::MapMatcher<Envoy::Network::MatchingData>::match(Envoy::Network::MatchingData const&)
Unexecuted instantiation: Envoy::Matcher::MapMatcher<Envoy::Network::UdpMatchingData>::match(Envoy::Network::UdpMatchingData const&)
60
61
protected:
62
  const DataInputPtr<DataType> data_input_;
63
  const absl::optional<OnMatch<DataType>> on_no_match_;
64
65
  // The inner match method. Attempts to match against the resulting data string. If the match
66
  // result was determined, the OnMatch will be returned. If a match result was determined to be no
67
  // match, {} will be returned.
68
  virtual absl::optional<OnMatch<DataType>> doMatch(const std::string& data) PURE;
69
};
70
71
} // namespace Matcher
72
} // namespace Envoy