Skip to content

[#1013] Implement the Database Adapter 2#1034

Open
marcocapozzoli wants to merge 3 commits intomasc/1013-database-adapter-cppfrom
masc/1013-database-adapter-cpp-2
Open

[#1013] Implement the Database Adapter 2#1034
marcocapozzoli wants to merge 3 commits intomasc/1013-database-adapter-cppfrom
masc/1013-database-adapter-cpp-2

Conversation

@marcocapozzoli
Copy link
Collaborator

This pull request adds the ContextLoader to validate the context file for mapping.

Resolves #1013

@andre-senna andre-senna changed the title [#1013] Implement the Database Adapter 2 [#1013] Implement the Database Adapter and Postgres concrete adapter in C++ Feb 9, 2026
@andre-senna andre-senna changed the title [#1013] Implement the Database Adapter and Postgres concrete adapter in C++ [#1013] Implement the Database Adapter 2 Feb 9, 2026
namespace fs = std::filesystem;

bool load_context_file(const string& file_path, vector<TableMapping>& out) {
out.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? I don't see the point of doing this inside this function. If the caller wants a cleanup made upfront, it would make it itself before calling this method. Enforcing a cleanup here prevent the caller from calling the function twice for different input files in order to add more mappings to a previously computed vector of mappings. You may not do this right now but I don't see any reason to restrict the function this way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Enforcing a cleanup here prevent the caller from calling the function twice for different input files

Yes, you're right, I hadn't thought of that.


if (!fs::exists(file_path)) {
LOG_ERROR("Context file " + file_path + " does not exist");
has_error = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't the code return from here? What's the point of going on if the file don't exist?

for (char c : tn)
if (c == '.') ++count_dot;
if (count_dot != 1) {
LOG_ERROR(msg_base + ".table_name must be in format 'schema.table' (single dot)");
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
LOG_ERROR(msg_base + ".table_name must be in format 'schema.table' (single dot)");
LOG_ERROR(msg_base + ".table_name must be in format 'schema.table'");

} else {
size_t pos = tn.find('.');
if (pos == 0 || pos + 1 >= tn.size()) {
LOG_ERROR(msg_base + ".table_name parts must not be empty in a table entry");
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
LOG_ERROR(msg_base + ".table_name parts must not be empty in a table entry");
LOG_ERROR(msg_base + ".table_name must be in format 'schema.table'");

}

if (has_error) {
LOG_ERROR("Context file validation failed with errors. Please fix the issues and try again.");
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand you allowed the JSON processing continue after an error to allow the caller to see all syntax error messages at once instead of going one by one. If this is the case you aren't supposed to give any outputs when errors occur. So you need to change the logic of the way you output the mappings.

I suggest creating a local vector and here in this if, if no errors occurred you feed the contents of this internal vector to the output (without cleaning it up before). If errors occur, nothing is added to the output.


namespace fs = std::filesystem;

bool load_context_file(const string& file_path, vector<TableMapping>& out) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't put actual code in .h files. Usually, the only exception is when you are declaring a templated class (which is not the case here).

To achieve what you intended here, I suggest creating a class ContextLoader (with a .h and a .cc files) and declaring load_context_file() as a static method (i.e. a class method).

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.

2 participants