Skip to content

Commit

Permalink
Make onConfigChanged hook's argument const to prevent accidental modi…
Browse files Browse the repository at this point in the history
…fications by end user
  • Loading branch information
adams85 committed Mar 1, 2024
1 parent 45e2bd5 commit 2f70f78
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions include/configcat/configcatoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace configcat {
class Hooks {
public:
explicit Hooks(const std::function<void()>& onClientReady = nullptr,
const std::function<void(std::shared_ptr<Settings>)>& onConfigChanged = nullptr,
const std::function<void(std::shared_ptr<const Settings>)>& onConfigChanged = nullptr,
const std::function<void(const EvaluationDetailsBase&)>& onFlagEvaluated = nullptr,
const std::function<void(const std::string&)>& onError = nullptr) {
if (onClientReady) {
Expand All @@ -43,7 +43,7 @@ class Hooks {
onClientReadyCallbacks.push_back(callback);
}

void addOnConfigChanged(const std::function<void(std::shared_ptr<Settings>)>& callback) {
void addOnConfigChanged(const std::function<void(std::shared_ptr<const Settings>)>& callback) {
std::lock_guard<std::mutex> lock(mutex);
onConfigChangedCallbacks.push_back(callback);
}
Expand Down Expand Up @@ -97,7 +97,7 @@ class Hooks {
private:
std::mutex mutex;
std::vector<std::function<void()>> onClientReadyCallbacks;
std::vector<std::function<void(std::shared_ptr<Settings>)>> onConfigChangedCallbacks;
std::vector<std::function<void(std::shared_ptr<const Settings>)>> onConfigChangedCallbacks;
std::vector<std::function<void(const EvaluationDetailsBase&)>> onFlagEvaluatedCallbacks;
std::vector<std::function<void(const std::string&)>> onErrorCallbacks;
};
Expand Down
4 changes: 2 additions & 2 deletions test/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class HookCallbacks {
public:
bool isReady = false;
int isReadyCallCount = 0;
std::shared_ptr<configcat::Settings> changedConfig;
std::shared_ptr<const configcat::Settings> changedConfig;
int changedConfigCallCount = 0;
configcat::EvaluationDetails<> evaluationDetails;
int evaluationDetailsCallCount = 0;
Expand All @@ -57,7 +57,7 @@ class HookCallbacks {
isReadyCallCount += 1;
}

void onConfigChanged(std::shared_ptr<configcat::Settings> config) {
void onConfigChanged(std::shared_ptr<const configcat::Settings> config) {
changedConfig = config;
changedConfigCallCount += 1;
}
Expand Down
14 changes: 7 additions & 7 deletions test/test-hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_F(HooksTest, Init) {
HookCallbacks hookCallbacks;
auto hooks = make_shared<Hooks>(
[&]() { hookCallbacks.onClientReady(); },
[&](std::shared_ptr<configcat::Settings> config) { hookCallbacks.onConfigChanged(config); },
[&](std::shared_ptr<const configcat::Settings> config) { hookCallbacks.onConfigChanged(config); },
[&](const EvaluationDetailsBase& details) { hookCallbacks.onFlagEvaluated(details); },
[&](const string& error) { hookCallbacks.onError(error); }
);
Expand All @@ -43,7 +43,7 @@ TEST_F(HooksTest, Init) {
expectedConfig->preferences = {};
expectedConfig->segments = {};
Config actualConfig;
actualConfig.settings = hookCallbacks.changedConfig;
actualConfig.settings = make_shared<Settings>(*hookCallbacks.changedConfig);

EXPECT_EQ("testValue", value);
EXPECT_TRUE(hookCallbacks.isReady);
Expand All @@ -56,7 +56,7 @@ TEST_F(HooksTest, Init) {
EXPECT_EQ("id", hookCallbacks.evaluationDetails.variationId);
EXPECT_TRUE(hookCallbacks.evaluationDetails.user == nullptr);
EXPECT_FALSE(hookCallbacks.evaluationDetails.isDefaultValue);
EXPECT_TRUE(hookCallbacks.evaluationDetails.errorMessage->empty());
EXPECT_FALSE(hookCallbacks.evaluationDetails.errorMessage.has_value());
EXPECT_EQ(1, hookCallbacks.evaluationDetailsCallCount);

EXPECT_TRUE(hookCallbacks.error.empty());
Expand All @@ -69,7 +69,7 @@ TEST_F(HooksTest, Subscribe) {
HookCallbacks hookCallbacks;
auto hooks = make_shared<Hooks>();
hooks->addOnClientReady([&]() { hookCallbacks.onClientReady(); });
hooks->addOnConfigChanged([&](std::shared_ptr<configcat::Settings> config) { hookCallbacks.onConfigChanged(config); });
hooks->addOnConfigChanged([&](std::shared_ptr<const configcat::Settings> config) { hookCallbacks.onConfigChanged(config); });
hooks->addOnFlagEvaluated([&](const EvaluationDetailsBase& details) { hookCallbacks.onFlagEvaluated(details); });
hooks->addOnError([&](const string& error) { hookCallbacks.onError(error); });

Expand All @@ -89,7 +89,7 @@ TEST_F(HooksTest, Subscribe) {
expectedConfig->preferences = {};
expectedConfig->segments = {};
Config actualConfig;
actualConfig.settings = hookCallbacks.changedConfig;
actualConfig.settings = make_shared<Settings>(*hookCallbacks.changedConfig);

EXPECT_EQ("testValue", value);
EXPECT_TRUE(hookCallbacks.isReady);
Expand All @@ -102,7 +102,7 @@ TEST_F(HooksTest, Subscribe) {
EXPECT_EQ("id", hookCallbacks.evaluationDetails.variationId);
EXPECT_TRUE(hookCallbacks.evaluationDetails.user == nullptr);
EXPECT_FALSE(hookCallbacks.evaluationDetails.isDefaultValue);
EXPECT_TRUE(hookCallbacks.evaluationDetails.errorMessage->empty());
EXPECT_FALSE(hookCallbacks.evaluationDetails.errorMessage.has_value());
EXPECT_EQ(1, hookCallbacks.evaluationDetailsCallCount);

EXPECT_TRUE(hookCallbacks.error.empty());
Expand Down Expand Up @@ -134,7 +134,7 @@ TEST_F(HooksTest, Evaluation) {
EXPECT_EQ("testStringKey", details.key);
EXPECT_EQ("id1", details.variationId);
EXPECT_FALSE(details.isDefaultValue);
EXPECT_TRUE(details.errorMessage->empty());
EXPECT_FALSE(details.errorMessage.has_value());
EXPECT_TRUE(details.matchedPercentageOption == std::nullopt);

auto& rule = details.matchedTargetingRule;
Expand Down

0 comments on commit 2f70f78

Please sign in to comment.