Skip to content

Commit bb0fa8c

Browse files
committed
add field_path!() macro
Signed-off-by: Andrew Duffy <[email protected]>
1 parent 332f981 commit bb0fa8c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

vortex-dtype/src/field.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ impl Display for Field {
9191
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9292
pub struct FieldPath(Vec<Field>);
9393

94+
/// A helpful constructor for creating `FieldPath`s to nested
95+
/// struct fields of the format `field_path!(x.y.z)`
96+
#[macro_export]
97+
macro_rules! field_path {
98+
($front:ident) => {{
99+
$crate::FieldPath::from_name(stringify!($front))
100+
}};
101+
($front:ident $(. $rest:ident)+) => {{
102+
$crate::FieldPath::from_iter([
103+
$crate::Field::from(stringify!($front)),
104+
$($crate::Field::from(stringify!($rest))),+
105+
])
106+
}};
107+
}
108+
94109
impl FieldPath {
95110
/// The selector for the root (i.e., the top-level struct itself)
96111
pub fn root() -> Self {
@@ -350,7 +365,7 @@ mod tests {
350365
#[test]
351366
fn nested_field_not_found() {
352367
let dtype = DType::struct_([("a", DType::Bool(NonNullable))], NonNullable);
353-
let path = FieldPath::from_name("b");
368+
let path = field_path!(b);
354369
assert!(path.resolve(dtype.clone()).is_none());
355370
assert!(!path.exists_in(dtype.clone()));
356371

@@ -365,7 +380,7 @@ mod tests {
365380
[("a", DType::Primitive(PType::I32, NonNullable))],
366381
NonNullable,
367382
);
368-
let path = FieldPath::from_name("a").push("b");
383+
let path = field_path!(a.b);
369384
assert!(path.resolve(dtype.clone()).is_none());
370385
assert!(!path.exists_in(dtype.clone()));
371386

vortex-layout/src/layouts/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl PathStrategy {
8585
///
8686
/// ```no_run
8787
/// # use std::sync::Arc;
88-
/// # use vortex_dtype::{Field, FieldPath};
88+
/// # use vortex_dtype::{field_path, Field, FieldPath};
8989
/// # use vortex_layout::layouts::compact::CompactCompressor;
9090
/// # use vortex_layout::layouts::compressed::CompressingStrategy;
9191
/// # use vortex_layout::layouts::flat::writer::FlatLayoutStrategy;
@@ -105,7 +105,7 @@ impl PathStrategy {
105105
/// Arc::new(compress_btrblocks),
106106
/// )
107107
/// .with_leaf_strategy(
108-
/// vec![Field::from("request"), Field::from("body"), Field::from("bytes")],
108+
/// vec![field_path!(request.body.bytes)],
109109
/// Arc::new(compress_compact),
110110
/// );
111111
/// ```

0 commit comments

Comments
 (0)