-
Notifications
You must be signed in to change notification settings - Fork 30
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
SNOW-1739603: Implement toml config file parsing #836
Open
sfc-gh-ext-simba-nl
wants to merge
17
commits into
master
Choose a base branch
from
SNOW-1739603-toml-file-configuration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65,031
−48
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7be2d7d
Implement toml config file parsing, add toml++ thirdparty library
sfc-gh-ext-simba-nl 446ed97
Update build_tomlplusplus.sh
sfc-gh-ext-simba-nl a129d94
Fix build scripts
sfc-gh-ext-simba-nl 9de1900
Fix windows 32-bit debug issue
sfc-gh-ext-simba-nl 7939e02
Remove build toml script on for unix to reset
sfc-gh-ext-simba-nl 9f6c9c0
Add back build toml script for unix
sfc-gh-ext-simba-nl 2514fa7
Add include <chrono> to fix VS17 build issues
sfc-gh-ext-simba-nl 2e1cf6d
Chmod build script
sfc-gh-jszczerbinski 6ef8d85
Fix unix build issue, use C++ map instead of returning string
sfc-gh-ext-simba-nl f5c5034
Merge branch 'SNOW-1739603-toml-file-configuration' of https://github…
sfc-gh-ext-simba-nl 802ce0e
Merge branch 'master' into SNOW-1739603-toml-file-configuration
sfc-gh-ext-simba-nl 0aaae82
Fix build issue
sfc-gh-ext-simba-nl 83adc13
Add SF_UNUSED to avoid code quality warnings
sfc-gh-ext-simba-nl 509051b
Merge master
sfc-gh-ext-simba-nl be67450
Refactor EnvOverride to be shared in tests/utils, change FILE to fstr…
sfc-gh-ext-simba-nl d5a44d1
Merge branch 'master' into SNOW-1739603-toml-file-configuration
sfc-gh-ext-simba-nl 23afd5a
Fix build error
sfc-gh-ext-simba-nl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include "snowflake/TomlConfigParser.hpp" | ||
#include "../logger/SFLogger.hpp" | ||
#include "memory.h" | ||
|
||
#define TOML_EXCEPTIONS 0 | ||
#include <toml++/toml.hpp> | ||
|
||
#undef snprintf | ||
#include <boost/filesystem.hpp> | ||
|
||
namespace | ||
{ | ||
using namespace Snowflake::Client; | ||
|
||
// constants | ||
const std::string ENV_SNOWFLAKE_HOME = "SNOWFLAKE_HOME"; | ||
const std::string ENV_SNOWFLAKE_DEF_CONN_NAME = "SNOWFLAKE_DEFAULT_CONNECTION_NAME"; | ||
const std::string SNOWFLAKE_HOME_DIR = ".snowflake"; | ||
const std::string TOML_FILENAME = "connections.toml"; | ||
|
||
// helpers | ||
std::string getEnvironmentVariableValue(const std::string& envVarName) { | ||
// Environment variables being checked point to file paths, hence MAX_PATH is used | ||
char envbuf[MAX_PATH + 1]; | ||
if (char* value = sf_getenv_s(envVarName.c_str(), envbuf, sizeof(envbuf))) { | ||
return std::string(value); | ||
} | ||
return ""; | ||
} | ||
|
||
boost::filesystem::path resolveTomlPath() { | ||
boost::filesystem::path tomlFilePath; | ||
// Check in SNOWFLAKE_HOME | ||
std::string snowflakeHomeEnv = getEnvironmentVariableValue(ENV_SNOWFLAKE_HOME); | ||
if (!snowflakeHomeEnv.empty()) { | ||
tomlFilePath = snowflakeHomeEnv; | ||
tomlFilePath.append(TOML_FILENAME); | ||
} else { | ||
// Check in ~/.snowflake | ||
#if defined(_WIN32) || defined(_WIN64) | ||
std::string homeDir = getEnvironmentVariableValue("USERPROFILE"); | ||
if (!homeDir.empty()) { | ||
tomlFilePath = homeDir; | ||
tomlFilePath.append(SNOWFLAKE_HOME_DIR); | ||
tomlFilePath.append(TOML_FILENAME); | ||
} else { | ||
// USERPROFILE is empty, try HOMEDRIVE and HOMEPATH | ||
std::string homeDriveEnv = getEnvironmentVariableValue("HOMEDRIVE"); | ||
std::string homePathEnv = getEnvironmentVariableValue("HOMEPATH"); | ||
if (!homeDriveEnv.empty() && !homePathEnv.empty()) { | ||
tomlFilePath = std::string(homeDriveEnv) + homePathEnv; | ||
tomlFilePath.append(SNOWFLAKE_HOME_DIR); | ||
tomlFilePath.append(TOML_FILENAME); | ||
} | ||
} | ||
#else | ||
std::string homeDir = getEnvironmentVariableValue("HOME"); | ||
if (!homeDir.empty()) { | ||
tomlFilePath = homeDir; | ||
tomlFilePath.append(SNOWFLAKE_HOME_DIR); | ||
tomlFilePath.append(TOML_FILENAME); | ||
} | ||
#endif | ||
} | ||
return tomlFilePath; | ||
} | ||
|
||
std::map<std::string, std::string> parseTomlFile( | ||
const boost::filesystem::path& filePath) { | ||
std::map<std::string, std::string> connectionParams; | ||
toml::parse_result result = toml::parse_file(filePath.c_str()); | ||
if (!result) | ||
{ | ||
CXX_LOG_ERROR("Failed to parse toml file: %s. Error: %s", filePath.c_str(), result.error().description().data()); | ||
return connectionParams; | ||
} | ||
toml::table table = std::move(result).table(); | ||
std::string configurationName = getEnvironmentVariableValue(ENV_SNOWFLAKE_DEF_CONN_NAME); | ||
if (configurationName.empty()) { | ||
configurationName = "default"; | ||
} | ||
toml::node_view config = table[configurationName]; | ||
if (!config) { | ||
CXX_LOG_ERROR("Could not find connection configuration name %s in toml file.", configurationName.c_str()); | ||
return connectionParams; | ||
} | ||
for (auto [key, val] : *config.as_table()) { | ||
connectionParams[key.data()] = val.as_string()->get(); | ||
} | ||
return connectionParams; | ||
} | ||
} | ||
|
||
std::map<std::string, std::string> load_toml_config() | ||
{ | ||
std::map<std::string, std::string> params; | ||
// Disable toml config parsing for 32-bit windows debug build due to linking issues | ||
// with _osfile causing hanging/assertions until dynamic linking is available | ||
#if (!defined(_WIN32) && !defined(_DEBUG)) || defined(_WIN64) | ||
boost::filesystem::path derivedTomlFilePath = resolveTomlPath(); | ||
|
||
if (!derivedTomlFilePath.empty()) { | ||
params = parseTomlFile(derivedTomlFilePath); | ||
} | ||
#endif | ||
return params; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use #define TOML_EXCEPTIONS 0
https://marzer.github.io/tomlplusplus/#mainpage-example-parsing-without-exceptions
We want to move towards exception free code