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

feat(session_config): allow add alias for parameter name #13963

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions src/common/proc_macro/src/session_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use syn::DeriveInput;
#[derive(FromAttributes)]
struct Parameter {
pub rename: Option<syn::LitStr>,
pub alias: Option<syn::Expr>,
pub default: syn::Expr,
pub flags: Option<syn::LitStr>,
pub check_hook: Option<syn::Expr>,
Expand All @@ -39,6 +40,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {
let mut get_match_branches = vec![];
let mut reset_match_branches = vec![];
let mut show_all_list = vec![];
let mut alias_to_entry_name_branches = vec![];

for field in fields {
let field_ident = field.ident.expect_or_abort("Field need to be named");
Expand All @@ -62,6 +64,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {
Parameter::from_attributes(&field.attrs).expect_or_abort("Failed to parse attribute");
let Parameter {
rename,
alias,
default,
flags,
check_hook: check_hook_name,
Expand All @@ -78,6 +81,12 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {
quote! {stringify!(#ident)}
};

if let Some(alias) = alias {
alias_to_entry_name_branches.push(quote! {
#alias => #entry_name.to_string(),
})
}

let flags = flags.map(|f| f.value()).unwrap_or_default();
let flags: Vec<_> = flags.split('|').map(|str| str.trim()).collect();

Expand Down Expand Up @@ -236,6 +245,13 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {
Default::default()
}

fn alias_to_entry_name(key_name: &str) -> String {
yuhao-su marked this conversation as resolved.
Show resolved Hide resolved
match key_name {
#(#alias_to_entry_name_branches)*
_ => key_name.to_string(),
}
}

#(#struct_impl_get)*

#(#struct_impl_set)*
Expand All @@ -244,6 +260,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {

/// Set a parameter given it's name and value string.
pub fn set(&mut self, key_name: &str, value: String, reporter: &mut impl ConfigReporter) -> SessionConfigResult<()> {
let key_name = Self::alias_to_entry_name(key_name);
match key_name.to_ascii_lowercase().as_ref() {
#(#set_match_branches)*
_ => Err(SessionConfigError::UnrecognizedEntry(key_name.to_string())),
Expand All @@ -252,6 +269,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {

/// Get a parameter by it's name.
pub fn get(&self, key_name: &str) -> SessionConfigResult<String> {
let key_name = Self::alias_to_entry_name(key_name);
match key_name.to_ascii_lowercase().as_ref() {
#(#get_match_branches)*
_ => Err(SessionConfigError::UnrecognizedEntry(key_name.to_string())),
Expand All @@ -260,6 +278,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream {

/// Reset a parameter by it's name.
pub fn reset(&mut self, key_name: &str, reporter: &mut impl ConfigReporter) -> SessionConfigResult<()> {
let key_name = Self::alias_to_entry_name(key_name);
match key_name.to_ascii_lowercase().as_ref() {
#(#reset_match_branches)*
_ => Err(SessionConfigError::UnrecognizedEntry(key_name.to_string())),
Expand Down
22 changes: 22 additions & 0 deletions src/common/src/session_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,25 @@ pub trait ConfigReporter {
impl ConfigReporter for () {
fn report_status(&mut self, _key: &str, _new_val: String) {}
}

#[cfg(test)]
mod test {
use super::*;

#[derive(SessionConfig)]
struct TestConfig {
#[parameter(default = 1, alias = "test_param_alias" | "alias_param_test")]
test_param: i32,
}

#[test]
fn test_session_config_alias() {
let mut config = TestConfig::default();
config.set("test_param", "2".to_string(), &mut ()).unwrap();
assert_eq!(config.get("test_param_alias").unwrap(), "2");
config
.set("alias_param_test", "3".to_string(), &mut ())
.unwrap();
assert_eq!(config.get("test_param_alias").unwrap(), "3");
}
}
Loading