3 #include <kafka/Project.h>
5 #include <kafka/ClientCommon.h>
6 #include <kafka/Error.h>
7 #include <kafka/Interceptors.h>
8 #include <kafka/KafkaException.h>
9 #include <kafka/Types.h>
26 using LogCallback = clients::LogCallback;
27 using ErrorCallback = clients::ErrorCallback;
28 using StatsCallback = clients::StatsCallback;
29 using OauthbearerTokenRefreshCallback = clients::OauthbearerTokenRefreshCallback;
36 virtual ~Object() =
default;
37 virtual std::string toString()
const = 0;
41 static std::string getString(
const T& ) {
return typeid(T).name(); }
43 static std::string getString(
const std::string& value) {
return value; }
45 const ValueType& validate(
const std::string& key)
const
47 static const std::vector<std::string> nonStringValueKeys = {
48 "log_cb",
"error_cb",
"stats_cb",
"oauthbearer_token_refresh_cb",
"interceptors"
51 if ((expectedKey.empty() && std::any_of(nonStringValueKeys.cbegin(), nonStringValueKeys.cend(), [key](
const auto& k) { return k == key; }))
52 || (!expectedKey.empty() && key != expectedKey))
54 throw std::runtime_error(
"Invalid key/value for configuration: " + key);
61 struct ObjWrap:
public Object
63 explicit ObjWrap(T v): value(std::move(v)) {}
64 std::string toString()
const override {
return getString<T>(value); }
69 T& getValue()
const {
return (
dynamic_cast<ObjWrap<T>&
>(*
object)).value; }
71 ValueType() =
default;
73 ValueType(
const std::string& value)
74 {
object = std::make_shared<ObjWrap<std::string>>(value); }
76 ValueType(
const LogCallback& cb)
77 : expectedKey(
"log_cb")
78 {
object = std::make_shared<ObjWrap<LogCallback>>(cb); }
80 ValueType(
const ErrorCallback& cb)
81 : expectedKey(
"error_cb")
82 {
object = std::make_shared<ObjWrap<ErrorCallback>>(cb); }
84 ValueType(
const StatsCallback& cb)
85 : expectedKey(
"stats_cb")
86 {
object = std::make_shared<ObjWrap<StatsCallback>>(cb); }
88 ValueType(
const OauthbearerTokenRefreshCallback& cb)
89 : expectedKey(
"oauthbearer_token_refresh_cb")
90 {
object = std::make_shared<ObjWrap<OauthbearerTokenRefreshCallback>>(cb); }
93 : expectedKey(
"interceptors")
94 {
object = std::make_shared<ObjWrap<Interceptors>>(interceptors); }
96 bool operator==(
const ValueType& rhs)
const {
return toString() == rhs.toString(); }
98 std::string toString()
const {
return object->toString(); }
101 std::string expectedKey;
102 std::shared_ptr<Object> object;
107 using PropertiesMap = std::map<std::string, ValueType>;
111 Properties(PropertiesMap kvMap): _kvMap(std::move(kvMap))
113 for (
const auto& kv: _kvMap)
115 kv.second.validate(kv.first);
129 _kvMap[key] = ValueType(value).validate(key);
146 auto search = _kvMap.find(key);
147 return search != _kvMap.end();
155 T&
get(
const std::string& key)
const
157 auto search = _kvMap.find(key);
158 if (search == _kvMap.end())
160 KAFKA_THROW_ERROR(
Error(RD_KAFKA_RESP_ERR__INVALID_ARG,
"Failed to get \"" + key +
"\" from Properties!"));
163 const ValueType& v = search->second;
164 return v.getValue<T>();
172 if (!
contains(key))
return Optional<std::string>{};
176 return get<std::string>(key);
178 catch (
const std::bad_cast&)
180 return Optional<std::string>{};
192 std::string toString()
const
196 std::for_each(_kvMap.cbegin(), _kvMap.cend(),
197 [&ret](
const auto& kv) {
198 const std::string& key = kv.first;
199 const std::string value = kv.second.toString();
201 static const std::regex reSensitiveKey(R
"(.+\.password|.+\.username|.+secret|.+key|.+pem)");
202 const bool isSensitive = std::regex_match(key, reSensitiveKey);
204 ret.append(ret.empty() ? "" :
"|").append(key).append(
"=").append(isSensitive ?
"*" : value);
212 const PropertiesMap&
map()
const {
return _kvMap; }
215 PropertiesMap _kvMap;
Unified error type.
Definition: Error.h:32
The properties for Kafka clients.
Definition: Properties.h:24
T & get(const std::string &key) const
Get a property reference.
Definition: Properties.h:155
void eraseProperty(const std::string &key)
Remove a property.
Definition: Properties.h:187
void remove(const std::string &key)
Remove the property (if one exists).
Definition: Properties.h:136
Properties & put(const std::string &key, const T &value)
Set a property.
Definition: Properties.h:127
bool contains(const std::string &key) const
Check whether the map contains a property.
Definition: Properties.h:144
const PropertiesMap & map() const
Get all properties with a map.
Definition: Properties.h:212
Optional< std::string > getProperty(const std::string &key) const
Get a property.
Definition: Properties.h:170
Interceptors for Kafka clients.
Definition: Interceptors.h:14