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

Agent Config Parsing Casing Fixes #2666

Merged
merged 6 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bs58 = "0.5.0"
bytes = "1"
clap = "4"
color-eyre = "0.6"
config = "~0.13.3"
config = "0.13.3"
convert_case = "0.6"
crunchy = "0.2"
ctrlc = "3.2"
Expand Down
2 changes: 1 addition & 1 deletion rust/hyperlane-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ publish.workspace = true
version.workspace = true

[dependencies]
# Main block
async-trait.workspace = true
bs58.workspace = true
color-eyre = { workspace = true, optional = true }
config.workspace = true
convert_case.workspace = true
derive-new.workspace = true
ed25519-dalek.workspace = true
ethers.workspace = true
Expand Down
46 changes: 31 additions & 15 deletions rust/hyperlane-base/src/settings/loader/arguments.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::ffi::{OsStr, OsString};

use config::{ConfigError, Map, Source, Value, ValueKind};
use convert_case::{Case, Casing};
use itertools::Itertools;

/// A source for loading configuration from command line arguments.
/// Command line argument keys are case-insensitive, and the following forms are
/// supported:
///
/// * `--key=value`
/// * `--key="value"`
Expand All @@ -23,6 +23,10 @@ pub struct CommandLineArguments {
/// Ignore empty env values (treat as unset).
ignore_empty: bool,

/// What casing to use for the keys in the environment. By default it will not mutate the key
/// value.
casing: Option<Case>,

/// Alternate source for the environment. This can be used when you want to
/// test your own code using this source, without the need to change the
/// actual system environment variables.
Expand All @@ -41,6 +45,11 @@ impl CommandLineArguments {
self
}

pub fn casing(mut self, casing: Case) -> Self {
self.casing = Some(casing);
self
}

pub fn source<I, S>(mut self, source: I) -> Self
where
I: IntoIterator<Item = S>,
Expand Down Expand Up @@ -77,12 +86,15 @@ impl Source for CommandLineArguments {
continue;
}

let mut key = key.to_lowercase();

// If separator is given replace with `.`
if !separator.is_empty() && separator != "." {
key = key.replace(separator, ".");
}
let key = if let Some(case) = self.casing {
// if case is given, replace case of each key component
key.split(separator).map(|s| s.to_case(case)).join(".")
mattiekat marked this conversation as resolved.
Show resolved Hide resolved
} else if !separator.is_empty() && separator != "." {
// If separator is given replace with `.`
key.replace(separator, ".")
} else {
key
};

m.insert(key, Value::new(Some(&uri), ValueKind::String(value)));
}
Expand Down Expand Up @@ -283,19 +295,21 @@ mod test {
Some(Value::new(
Some(&origin),
ValueKind::String($value.to_owned())
))
)),
$key
);
};
}

const ARGUMENTS: &[&str] = &[
"--key-a",
"value-a",
"--keY-b=value-b",
"--key-c=\"value c\"",
"--KEY-d='valUE d'",
"--key-b=value-b",
"--key-c-partA=\"value c a\"",
"--key-c-PartB=\"value c b\"",
"--key-d='valUE d'",
"--key-e=''",
"--key-F",
"--key-f",
"--key-g=value-g",
"--key-h",
];
Expand All @@ -309,7 +323,8 @@ mod test {

assert_arg!(config, "key.a", "value-a");
assert_arg!(config, "key.b", "value-b");
assert_arg!(config, "key.c", "value c");
assert_arg!(config, "key.c.partA", "value c a");
assert_arg!(config, "key.c.PartB", "value c b");
assert_arg!(config, "key.d", "valUE d");
assert_arg!(config, "key.e", "");
assert_arg!(config, "key.f", "");
Expand All @@ -329,7 +344,8 @@ mod test {

assert_arg!(config, "key.a", "value-a");
assert_arg!(config, "key.b", "value-b");
assert_arg!(config, "key.c", "value c");
assert_arg!(config, "key.c.partA", "value c a");
assert_arg!(config, "key.c.PartB", "value c b");
assert_arg!(config, "key.d", "valUE d");
assert_arg!(config, "key.g", "value-g");

Expand Down
Loading
Loading