Skip to content

fix: document and fix macro hygiene for config_field! #16473

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

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 35 additions & 6 deletions datafusion/common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,10 @@ impl<F: ConfigField + Default> ConfigField for Option<F> {
}
}

fn default_transform<T>(input: &str) -> Result<T>
/// Default transformation to parse a [`ConfigField`] for a string.
///
/// This uses [`FromStr`] to parse the data.
pub fn default_config_transform<T>(input: &str) -> Result<T>
where
T: FromStr,
<T as FromStr>::Err: Sync + Send + Error + 'static,
Expand All @@ -1274,19 +1277,45 @@ where
})
}

/// Macro that generates [`ConfigField`] for a given type.
///
/// # Usage
/// This always requires [`Display`] to be implemented for the given type.
///
/// There are two ways to invoke this macro. The first one uses
/// [`default_config_transform`]/[`FromStr`] to parse the data:
///
/// ```ignore
/// config_field(MyType);
/// ```
///
/// Note that the parsing error MUST implement [`std::error::Error`]!
///
/// Or you can specify how you want to parse an [`str`] into the type:
///
/// ```ignore
/// fn parse_it(s: &str) -> Result<MyType> {
/// ...
/// }
///
/// config_field(
/// MyType,
/// value => parse_it(value)
/// );
/// ```
#[macro_export]
macro_rules! config_field {
($t:ty) => {
config_field!($t, value => default_transform(value)?);
config_field!($t, value => $crate::config::default_config_transform(value)?);
};

($t:ty, $arg:ident => $transform:expr) => {
impl ConfigField for $t {
fn visit<V: Visit>(&self, v: &mut V, key: &str, description: &'static str) {
impl $crate::config::ConfigField for $t {
fn visit<V: $crate::config::Visit>(&self, v: &mut V, key: &str, description: &'static str) {
v.some(key, self, description)
}

fn set(&mut self, _: &str, $arg: &str) -> Result<()> {
fn set(&mut self, _: &str, $arg: &str) -> $crate::error::Result<()> {
*self = $transform;
Ok(())
}
Expand All @@ -1295,7 +1324,7 @@ macro_rules! config_field {
}

config_field!(String);
config_field!(bool, value => default_transform(value.to_lowercase().as_str())?);
config_field!(bool, value => default_config_transform(value.to_lowercase().as_str())?);
config_field!(usize);
config_field!(f64);
config_field!(u64);
Expand Down
37 changes: 37 additions & 0 deletions datafusion/core/tests/macro_hygiene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,40 @@ mod config_namespace {
}
}
}

mod config_field {
// NO other imports!
use datafusion_common::config_field;

#[test]
fn test_macro() {
#[derive(Debug)]
struct E;

impl std::fmt::Display for E {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
unimplemented!()
}
}

impl std::error::Error for E {}

struct S;

impl std::str::FromStr for S {
type Err = E;

fn from_str(_s: &str) -> Result<Self, Self::Err> {
unimplemented!()
}
}

impl std::fmt::Display for S {
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
unimplemented!()
}
}

config_field!(S);
}
}