Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTIL: cfg parser respect inherited vars #545

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/utils/ucc_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "ucc_parser.h"
#include "ucc_malloc.h"
#include "ucc_string.h"
#include "ucc_log.h"
#include "khash.h"

Expand Down Expand Up @@ -283,22 +284,46 @@ static ucc_status_t ucc_apply_file_cfg_value(void * opts,
return UCC_ERR_NOT_FOUND;
}

static inline int check_var_in_env(const char *prefix, const char *var)
{
char *v;
int present;

if (UCC_OK != ucc_str_concat(prefix, var, &v)) {
return 0;
}
present = (getenv(v) != NULL);
ucc_free(v);
return present;
}

static ucc_status_t ucc_apply_file_cfg(void *opts, ucc_config_field_t *fields,
const char *env_prefix,
const char *component_prefix)
const char *component_prefix,
int recursive)
{
ucc_status_t status = UCC_OK;
ucc_status_t status = UCC_OK;
ucc_config_field_t *super;

while (fields->name != NULL) {
if (strlen(fields->name) == 0) {
status = ucc_apply_file_cfg(
opts, (ucc_config_field_t *)fields->parser.arg, env_prefix,
component_prefix);
super = (ucc_config_field_t *)fields->parser.arg;
status = ucc_apply_file_cfg(opts, super, env_prefix,
component_prefix, 1);
if (UCC_OK != status) {
return status;
}
fields++;
continue;
goto next;
}

if (recursive) {
if (check_var_in_env("UCC_", fields->name)) {
goto next;
}
if (0 != strcmp(env_prefix, "UCC_") &&
check_var_in_env(env_prefix, fields->name)) {
goto next;
}
}
status = ucc_apply_file_cfg_value(
opts, fields, env_prefix, component_prefix ? component_prefix : "",
Expand All @@ -310,7 +335,7 @@ static ucc_status_t ucc_apply_file_cfg(void *opts, ucc_config_field_t *fields,
if (status != UCC_OK && status != UCC_ERR_NOT_FOUND) {
return status;
}

next:
fields++;
}
return UCC_OK;
Expand All @@ -329,7 +354,7 @@ ucc_status_t ucc_config_parser_fill_opts(void *opts, ucc_config_field_t *fields,
status = ucs_status_to_ucc_status(ucs_status);

if (UCC_OK == status && ucc_global_config.file_cfg) {
status = ucc_apply_file_cfg(opts, fields, env_prefix, table_prefix);
status = ucc_apply_file_cfg(opts, fields, env_prefix, table_prefix, 0);
}

return status;
Expand Down
10 changes: 10 additions & 0 deletions test/gtest/utils/test_cfg_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,13 @@ UCC_TEST_F(test_cfg_file, env_preference) {
EXPECT_EQ(123, cfg.bar);
EXPECT_EQ(1, cfg.boo);
}

UCC_TEST_F(test_cfg_file, env_preference_inherited) {
std::string filename = test_dir + "ucc_test.conf";

setenv("GTEST_UCC_FOO", "123", 1);
EXPECT_EQ(UCC_OK, ucc_parse_file_config(filename.c_str(), &file_cfg));
init_cfg();
unsetenv("GTEST_UCC_FOO");
EXPECT_EQ(123, cfg.super.foo);
}