Skip to content

Commit

Permalink
chore: improve hello app const correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
cwaldren-ld committed Nov 21, 2023
1 parent f1e93cd commit c0e5027
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
19 changes: 11 additions & 8 deletions examples/hello-cpp-client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ char const* get_with_env_fallback(char const* source_val,
char const* error_msg);

using namespace launchdarkly;
using namespace launchdarkly::client_side;

int main() {
char const* mobile_key = get_with_env_fallback(
MOBILE_KEY, "LD_MOBILE_KEY",
Expand All @@ -27,7 +29,7 @@ int main() {
"variable.\n"
"The value of MOBILE_KEY in main.c takes priority over LD_MOBILE_KEY.");

auto config = client_side::ConfigBuilder(mobile_key).Build();
auto config = ConfigBuilder(mobile_key).Build();
if (!config) {
std::cout << "error: config is invalid: " << config.error() << '\n';
return 1;
Expand All @@ -36,12 +38,13 @@ int main() {
auto context =
ContextBuilder().Kind("user", "example-user-key").Name("Sandy").Build();

auto client = client_side::Client(std::move(*config), std::move(context));
auto client = Client(std::move(*config), std::move(context));

auto start_result = client.StartAsync();
auto status = start_result.wait_for(
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
if (status == std::future_status::ready) {

if (auto const status = start_result.wait_for(
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
status == std::future_status::ready) {
if (start_result.get()) {
std::cout << "*** SDK successfully initialized!\n\n";
} else {
Expand All @@ -54,7 +57,7 @@ int main() {
return 1;
}

bool flag_value = client.BoolVariation(FEATURE_FLAG_KEY, false);
bool const flag_value = client.BoolVariation(FEATURE_FLAG_KEY, false);

std::cout << "*** Feature flag '" << FEATURE_FLAG_KEY << "' is "
<< (flag_value ? "true" : "false") << " for this user\n\n";
Expand All @@ -69,8 +72,8 @@ char const* get_with_env_fallback(char const* source_val,
return source_val;
}

char const* from_env = std::getenv(env_variable);
if (from_env && strlen(from_env)) {
if (char const* from_env = std::getenv(env_variable);
from_env && strlen(from_env)) {

Check warning on line 76 in examples/hello-cpp-client/main.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/examples/hello-cpp-client/main.cpp:76:9 [readability-implicit-bool-conversion]

implicit conversion 'const char *' -> bool

Check warning on line 76 in examples/hello-cpp-client/main.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

/examples/hello-cpp-client/main.cpp:76:21 [readability-implicit-bool-conversion]

implicit conversion 'size_t' (aka 'unsigned long') -> bool
return from_env;
}

Expand Down
22 changes: 13 additions & 9 deletions examples/hello-cpp-server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ char const* get_with_env_fallback(char const* source_val,
char const* env_variable,
char const* error_msg);
using namespace launchdarkly;
using namespace launchdarkly::server_side;

int main() {
char const* sdk_key = get_with_env_fallback(
SDK_KEY, "LD_SDK_KEY",
Expand All @@ -28,18 +30,19 @@ int main() {
"variable.\n"
"The value of SDK_KEY in main.c takes priority over LD_SDK_KEY.");

auto config = server_side::ConfigBuilder(sdk_key).Build();
auto config = ConfigBuilder(sdk_key).Build();
if (!config) {
std::cout << "error: config is invalid: " << config.error() << '\n';
return 1;
}

auto client = server_side::Client(std::move(*config));
auto client = Client(std::move(*config));

auto start_result = client.StartAsync();
auto status = start_result.wait_for(
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
if (status == std::future_status::ready) {

if (auto const status = start_result.wait_for(
std::chrono::milliseconds(INIT_TIMEOUT_MILLISECONDS));
status == std::future_status::ready) {
if (start_result.get()) {
std::cout << "*** SDK successfully initialized!\n\n";
} else {
Expand All @@ -52,10 +55,11 @@ int main() {
return 1;
}

auto context =
auto const context =
ContextBuilder().Kind("user", "example-user-key").Name("Sandy").Build();

bool flag_value = client.BoolVariation(context, FEATURE_FLAG_KEY, false);
bool const flag_value =
client.BoolVariation(context, FEATURE_FLAG_KEY, false);

std::cout << "*** Feature flag '" << FEATURE_FLAG_KEY << "' is "
<< (flag_value ? "true" : "false") << " for this user\n\n";
Expand All @@ -70,8 +74,8 @@ char const* get_with_env_fallback(char const* source_val,
return source_val;
}

char const* from_env = std::getenv(env_variable);
if (from_env && strlen(from_env)) {
if (char const* from_env = std::getenv(env_variable);
from_env && strlen(from_env)) {
return from_env;
}

Expand Down

0 comments on commit c0e5027

Please sign in to comment.