1
#pragma once
2

            
3
#include "envoy/config/core/v3/base.pb.h"
4

            
5
#include "source/common/protobuf/utility.h"
6

            
7
namespace Envoy {
8
namespace Upstream {
9

            
10
// We use a tuple representation for hashing/equality/comparison, since this
11
// ensures we are not subject to proto nuances like unknown fields (e.g. from
12
// original type information annotations).
13
using LocalityTuple = std::tuple<const std::string&, const std::string&, const std::string&>;
14

            
15
struct LocalityHash {
16
2040
  size_t operator()(const envoy::config::core::v3::Locality& locality) const {
17
2040
    return absl::Hash<LocalityTuple>()({locality.region(), locality.zone(), locality.sub_zone()});
18
2040
  }
19
};
20

            
21
struct LocalityEqualTo {
22
  bool operator()(const envoy::config::core::v3::Locality& lhs,
23
9156
                  const envoy::config::core::v3::Locality& rhs) const {
24
9156
    const LocalityTuple lhs_tuple = LocalityTuple(lhs.region(), lhs.zone(), lhs.sub_zone());
25
9156
    const LocalityTuple rhs_tuple = LocalityTuple(rhs.region(), rhs.zone(), rhs.sub_zone());
26
9156
    return lhs_tuple == rhs_tuple;
27
9156
  }
28
};
29

            
30
struct LocalityLess {
31
  bool operator()(const envoy::config::core::v3::Locality& lhs,
32
19677
                  const envoy::config::core::v3::Locality& rhs) const {
33
19677
    const LocalityTuple lhs_tuple = LocalityTuple(lhs.region(), lhs.zone(), lhs.sub_zone());
34
19677
    const LocalityTuple rhs_tuple = LocalityTuple(rhs.region(), rhs.zone(), rhs.sub_zone());
35
19677
    return lhs_tuple < rhs_tuple;
36
19677
  }
37
};
38

            
39
// For tests etc. where this is convenient.
40
static inline envoy::config::core::v3::Locality
41
19
Locality(const std::string& region, const std::string& zone, const std::string sub_zone) {
42
19
  envoy::config::core::v3::Locality locality;
43
19
  locality.set_region(region);
44
19
  locality.set_zone(zone);
45
19
  locality.set_sub_zone(sub_zone);
46
19
  return locality;
47
19
}
48

            
49
} // namespace Upstream
50
} // namespace Envoy
51

            
52
// Something heinous this way comes. Required to allow == for LocalityWeightsMap.h in eds.h.
53
namespace envoy {
54
namespace config {
55
namespace core {
56
namespace v3 {
57

            
58
inline bool operator==(const envoy::config::core::v3::Locality& x,
59
10
                       const envoy::config::core::v3::Locality& y) {
60
10
  return Envoy::Upstream::LocalityEqualTo()(x, y);
61
10
}
62

            
63
} // namespace v3
64
} // namespace core
65
} // namespace config
66
} // namespace envoy