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

Lazy configuration validator #124

Merged
merged 1 commit into from
Aug 2, 2020
Merged

Lazy configuration validator #124

merged 1 commit into from
Aug 2, 2020

Conversation

karanankit01
Copy link
Contributor

configuration field value validation

@@ -43,6 +45,228 @@ Config::Config() {

node_["double_quote_to_single_quote"] = false;
node_["single_quote_to_double_quote"] = false;

Copy link
Contributor

@tammela tammela Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You propagated the same lambda throughout this function. Why not a variable?

auto validate_integer = [](std::pair<std::string, std::any> elem) {
        if (strcmp(elem.second.type().name(), "i") != 0) {
            // Bug in the code
            assert(0);
        }
        int value = std::any_cast<int>(elem.second);
        if (value > 0) {
             return value;
         }
        std::cerr << "Integer value of " << elem.first << " is out of range" << std::endl;
        exit(1);
    };

validator["column_limit"] = validate_integer;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not repeat code

src/Config.h Outdated
@@ -30,4 +32,6 @@ class Config {
void set(const char *key, T value) const {
node_[key] = value;
}
typedef std::function<int(std::pair<std::string, std::any>)> Validator;
std::map<std::string, Validator> validation;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::map<std::string, Validator> validation;
std::map<std::string, Validator> validators;

src/Config.h Outdated
@@ -30,4 +32,6 @@ class Config {
void set(const char *key, T value) const {
node_[key] = value;
}
typedef std::function<int(std::pair<std::string, std::any>)> Validator;
Copy link
Contributor

@tammela tammela Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a std::pair instead of two arguments?
Seems overkill to me.

// Validators
auto validate_integer = [](std::string key, std::any elem) {
int value = std::any_cast<int>(elem);
if (value > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would split this like you did because it might be desirable to have some configurations with 0s as well.

Suggested change
if (value > 0) {
if (value >= 0) {

And then make validators["spaces_before_call"] = validate_integer; as well

src/Config.h Outdated
@@ -30,4 +33,6 @@ class Config {
void set(const char *key, T value) const {
node_[key] = value;
}
typedef std::function<std::any(std::string, std::any)> Validator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this typedef to before the class declaration

string configFileName(file); \
Config config; \
if (fs::exists(configFileName)) { \
std::cout << configFileName << " exist" << endl; \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::cout << configFileName << " exist" << endl; \

TEST_CASE("Config file " + string(file) + " have error", "format_file") { \
string configFileName(file); \
Config config; \
if (fs::exists(configFileName)) { \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this check, I think we can drop it

src/Config.cpp Outdated
if (value > 0) {
return value;
}
std::cerr << "Integer value of " << key << " is out of range" << std::endl;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::cerr << "Integer value of " << key << " is out of range" << std::endl;
std::cerr << "[ERROR] Processing " << key << " failed";

src/Config.cpp Outdated
return value;
}
std::cerr << "Integer value of " << key << " is out of range" << std::endl;
throw domain_error("Config value is out of Range");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw domain_error("Config value is out of Range");
throw domain_error("[ERROR] Configuration value is out of range. Must be a positive integer.");

src/Config.cpp Outdated
node_[key] = any_cast<bool>(kv.second);
} else if (strcmp(kv.second.type().name(), "c") == 0) {
node_[key] = any_cast<char>(kv.second);
if(datatype[key]== 'i'){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A switch statement here makes the code more readable.
Also don't forget to useclang-format

namespace fs = filesystem;

#define TEST_CONFIG_FILE(file) \
TEST_CASE("Config file " + string(file) + " have error", "format_file") { \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TEST_CASE("Config file " + string(file) + " have error", "format_file") { \
TEST_CASE("Configuration file " + string(file) + " has an error", "format_file") { \

@tammela
Copy link
Contributor

tammela commented Jul 26, 2020

Ok, time to do things right in the git history.
Squash all commits to one commit with the following message:
"initial implementation of validators"
Then I will re-review and you post the changes with fixup!

src/Config.cpp Outdated
return value;
}
std::cerr << "[ERROR] Processing " << key << " failed" << std::endl;
throw domain_error("[ERROR] Configuration value is out of range. Must be a positive integer.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw domain_error("[ERROR] Configuration value is out of range. Must be a positive integer.");
throw domain_error("[ERROR] Configuration value is out of range. Must be a positive integer greater than 0.");

Copy link
Contributor

@tammela tammela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@karanankit01 LGTM.
Please squash all commits using git rebase origin/master -i --autosquash
@Koihik please merge after squashing

@Koihik Koihik merged commit acb531b into Koihik:master Aug 2, 2020
@karanankit01 karanankit01 deleted the lazy branch August 3, 2020 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants