mirror of
				https://github.com/Telecominfraproject/wlan-cloud-lib-cppkafka.git
				synced 2025-11-03 20:18:06 +00:00 
			
		
		
		
	Allow dumping all options in a configuration
This commit is contained in:
		@@ -33,8 +33,6 @@
 | 
				
			|||||||
#include <memory>
 | 
					#include <memory>
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <functional>
 | 
					#include <functional>
 | 
				
			||||||
#include <unordered_map>
 | 
					 | 
				
			||||||
#include <unordered_set>
 | 
					 | 
				
			||||||
#include <chrono>
 | 
					#include <chrono>
 | 
				
			||||||
#include <boost/optional.hpp>
 | 
					#include <boost/optional.hpp>
 | 
				
			||||||
#include <librdkafka/rdkafka.h>
 | 
					#include <librdkafka/rdkafka.h>
 | 
				
			||||||
@@ -149,6 +147,11 @@ public:
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    std::string get(const std::string& name) const;
 | 
					    std::string get(const std::string& name) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Gets all options, including default values which are set by rdkafka
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    std::map<std::string, std::string> get_all() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Gets the delivery report callback
 | 
					     * Gets the delivery report callback
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,6 +31,7 @@
 | 
				
			|||||||
#define CPPKAFKA_CONFIGURATION_BASE_H
 | 
					#define CPPKAFKA_CONFIGURATION_BASE_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
 | 
					#include <map>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace cppkafka {
 | 
					namespace cppkafka {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -59,6 +60,14 @@ public:
 | 
				
			|||||||
    void set(const std::string& name, const char* value) {
 | 
					    void set(const std::string& name, const char* value) {
 | 
				
			||||||
        proxy_set(name, value);
 | 
					        proxy_set(name, value);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					protected:
 | 
				
			||||||
 | 
					    static std::map<std::string, std::string> parse_dump(const char** values, size_t count) {
 | 
				
			||||||
 | 
					        std::map<std::string, std::string> output;
 | 
				
			||||||
 | 
					        for (size_t i = 0; i < count; i += 2) {
 | 
				
			||||||
 | 
					            output[values[i]] = values[i + 1];
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return output;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    void proxy_set(const std::string& name, const std::string& value) {
 | 
					    void proxy_set(const std::string& name, const std::string& value) {
 | 
				
			||||||
        static_cast<Concrete&>(*this).set(name, value);
 | 
					        static_cast<Concrete&>(*this).set(name, value);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -109,6 +109,11 @@ public:
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    std::string get(const std::string& name) const;
 | 
					    std::string get(const std::string& name) const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Gets all options, including default values which are set by rdkafka
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    std::map<std::string, std::string> get_all() const;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Gets the rdkafka handle
 | 
					     * Gets the rdkafka handle
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,6 +36,7 @@
 | 
				
			|||||||
#include "consumer.h"
 | 
					#include "consumer.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using std::string;
 | 
					using std::string;
 | 
				
			||||||
 | 
					using std::map;
 | 
				
			||||||
using std::move;
 | 
					using std::move;
 | 
				
			||||||
using std::vector;
 | 
					using std::vector;
 | 
				
			||||||
using std::unordered_set;
 | 
					using std::unordered_set;
 | 
				
			||||||
@@ -194,6 +195,14 @@ string Configuration::get(const string& name) const {
 | 
				
			|||||||
    return string(buffer.data());
 | 
					    return string(buffer.data());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					map<string, string> Configuration::get_all() const {
 | 
				
			||||||
 | 
					    size_t count = 0;
 | 
				
			||||||
 | 
					    const char** all = rd_kafka_conf_dump(handle_.get(), &count);
 | 
				
			||||||
 | 
					    map<string, string> output = parse_dump(all, count);
 | 
				
			||||||
 | 
					    rd_kafka_conf_dump_free(all, count);
 | 
				
			||||||
 | 
					    return output;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Configuration::DeliveryReportCallback& Configuration::get_delivery_report_callback() const {
 | 
					const Configuration::DeliveryReportCallback& Configuration::get_delivery_report_callback() const {
 | 
				
			||||||
    return delivery_report_callback_;
 | 
					    return delivery_report_callback_;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -35,6 +35,7 @@
 | 
				
			|||||||
#include "buffer.h"
 | 
					#include "buffer.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using std::string;
 | 
					using std::string;
 | 
				
			||||||
 | 
					using std::map;
 | 
				
			||||||
using std::vector;
 | 
					using std::vector;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace cppkafka {
 | 
					namespace cppkafka {
 | 
				
			||||||
@@ -106,6 +107,14 @@ string TopicConfiguration::get(const string& name) const {
 | 
				
			|||||||
    return string(buffer.data());
 | 
					    return string(buffer.data());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					map<string, string> TopicConfiguration::get_all() const {
 | 
				
			||||||
 | 
					    size_t count = 0;
 | 
				
			||||||
 | 
					    const char** all = rd_kafka_topic_conf_dump(handle_.get(), &count);
 | 
				
			||||||
 | 
					    map<string, string> output = parse_dump(all, count);
 | 
				
			||||||
 | 
					    rd_kafka_conf_dump_free(all, count);
 | 
				
			||||||
 | 
					    return output;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
rd_kafka_topic_conf_t* TopicConfiguration::get_handle() const {
 | 
					rd_kafka_topic_conf_t* TopicConfiguration::get_handle() const {
 | 
				
			||||||
    return handle_.get();
 | 
					    return handle_.get();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,7 +19,7 @@ TEST_F(ConfigurationTest, GetSetConfig) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
TEST_F(ConfigurationTest, GetSetTopicConfig) {
 | 
					TEST_F(ConfigurationTest, GetSetTopicConfig) {
 | 
				
			||||||
    TopicConfiguration config;
 | 
					    TopicConfiguration config;
 | 
				
			||||||
    config.set("auto.commit.enable", "true");
 | 
					    config.set("auto.commit.enable", true);
 | 
				
			||||||
    EXPECT_EQ("true", config.get("auto.commit.enable"));
 | 
					    EXPECT_EQ("true", config.get("auto.commit.enable"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    EXPECT_THROW(config.get("asd"), ConfigOptionNotFound);
 | 
					    EXPECT_THROW(config.get("asd"), ConfigOptionNotFound);
 | 
				
			||||||
@@ -32,4 +32,18 @@ TEST_F(ConfigurationTest, SetOverloads) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    EXPECT_EQ("true", config.get("enable.auto.commit"));
 | 
					    EXPECT_EQ("true", config.get("enable.auto.commit"));
 | 
				
			||||||
    EXPECT_EQ("100", config.get("auto.commit.interval.ms"));
 | 
					    EXPECT_EQ("100", config.get("auto.commit.interval.ms"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_F(ConfigurationTest, GetAll) {
 | 
				
			||||||
 | 
					    Configuration config;
 | 
				
			||||||
 | 
					    config.set("enable.auto.commit", false);
 | 
				
			||||||
 | 
					    auto option_map = config.get_all();
 | 
				
			||||||
 | 
					    EXPECT_EQ("false", option_map.at("enable.auto.commit"));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					TEST_F(ConfigurationTest, TopicGetAll) {
 | 
				
			||||||
 | 
					    TopicConfiguration config;
 | 
				
			||||||
 | 
					    config.set("auto.commit.enable", false);
 | 
				
			||||||
 | 
					    auto option_map = config.get_all();
 | 
				
			||||||
 | 
					    EXPECT_EQ("false", option_map.at("auto.commit.enable"));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user