-
Notifications
You must be signed in to change notification settings - Fork 20
Config visibility report all sources and their values #268
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
Changes from 5 commits
b709d39
a518089
dfed242
51faecf
8e33b54
382e80f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,6 @@ dist/ | |
| out/ | ||
| MODULE.bazel.lock | ||
| .vscode | ||
| .cache/ | ||
| .cursor/ | ||
| .DS_Store | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <type_traits> | ||
| #include <unordered_map> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "error.h" | ||
| #include "optional.h" | ||
|
|
||
|
|
@@ -56,14 +62,72 @@ struct ConfigMetadata { | |
| : name(n), value(std::move(v)), origin(orig), error(std::move(err)) {} | ||
| }; | ||
|
|
||
| // Return the chosen configuration value, in order of precedence: `from_env`, | ||
| // `from_user`, and `fallback`. | ||
| // This overload directly populates both telemetry_configs[config_name] and | ||
| // metadata[config_name] using the stringified value, with all values found, | ||
| // from lowest to highest precedence. Returns the chosen value directly. | ||
| // | ||
| // The fallback parameter is optional and defaults to nullptr. | ||
| template <typename Value, typename Stringifier = std::nullptr_t, | ||
| typename DefaultValue = std::nullptr_t> | ||
| Value pick(const Optional<Value>& from_env, const Optional<Value>& from_user, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function does more than just
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, renamed to |
||
| std::unordered_map<ConfigName, std::vector<ConfigMetadata>>* | ||
| telemetry_configs, | ||
| std::unordered_map<ConfigName, ConfigMetadata>* metadata, | ||
| ConfigName config_name, DefaultValue fallback = nullptr, | ||
| Stringifier to_string_fn = nullptr) { | ||
| auto stringify = [&](const Value& v) -> std::string { | ||
| if constexpr (!std::is_same_v<Stringifier, std::nullptr_t>) { | ||
| return to_string_fn(v); // use provided function | ||
| } else if constexpr (std::is_constructible_v<std::string, Value>) { | ||
| return std::string(v); // default behaviour (works for string-like types) | ||
| } else { | ||
| static_assert(!std::is_same_v<Value, Value>, | ||
| "Non-string types require a stringifier function"); | ||
| return ""; // unreachable | ||
| } | ||
| }; | ||
|
|
||
| std::vector<ConfigMetadata> telemetry_entries; | ||
| Optional<Value> chosen_value; | ||
|
|
||
| auto add_entry = [&](ConfigMetadata::Origin origin, const Value& val) { | ||
| std::string val_str = stringify(val); | ||
| telemetry_entries.emplace_back( | ||
| ConfigMetadata{config_name, val_str, origin}); | ||
| chosen_value = val; | ||
| }; | ||
|
|
||
| // Add DEFAULT entry if fallback was provided (detected by type) | ||
| if constexpr (!std::is_same_v<DefaultValue, std::nullptr_t>) { | ||
| add_entry(ConfigMetadata::Origin::DEFAULT, fallback); | ||
| } | ||
|
|
||
| if (from_user) { | ||
| add_entry(ConfigMetadata::Origin::CODE, *from_user); | ||
| } | ||
|
|
||
| if (from_env) { | ||
| add_entry(ConfigMetadata::Origin::ENVIRONMENT_VARIABLE, *from_env); | ||
| } | ||
|
|
||
| (*telemetry_configs)[config_name] = std::move(telemetry_entries); | ||
| if (!(*telemetry_configs)[config_name].empty()) { | ||
| (*metadata)[config_name] = (*telemetry_configs)[config_name].back(); | ||
| } | ||
|
|
||
| return chosen_value.value_or(Value{}); | ||
| } | ||
|
|
||
| // Return a pair containing the configuration origin and value of a | ||
| // configuration value chosen from one of the specified `from_env`, | ||
| // `from_config`, and `fallback`. This function defines the relative precedence | ||
| // among configuration values originating from the environment, programmatic | ||
| // configuration, and default configuration. | ||
| template <typename Value, typename DefaultValue> | ||
| std::pair<ConfigMetadata::Origin, Value> pick(const Optional<Value> &from_env, | ||
| const Optional<Value> &from_user, | ||
| std::pair<ConfigMetadata::Origin, Value> pick(const Optional<Value>& from_env, | ||
| const Optional<Value>& from_user, | ||
| DefaultValue fallback) { | ||
| if (from_env) { | ||
| return {ConfigMetadata::Origin::ENVIRONMENT_VARIABLE, *from_env}; | ||
|
|
||
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.
Is this change necessary? IIRC docker would use the right platform depending on the host platform.
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.
oh yes I was just getting
WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requestedon my machine and added that but I can get rid of itThere 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.
fixed in 382e80f