-
Notifications
You must be signed in to change notification settings - Fork 2k
Refactor example metadata parsing utilities(#20204) #20233
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
comphead
merged 1 commit into
apache:main
from
cj-zhukov:cj-zhukov/refactor-example-metadata-parsing-utilities
Feb 10, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
datafusion-examples/src/utils/example_metadata/discover.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Utilities for discovering example groups in the repository filesystem. | ||
| //! | ||
| //! An example group is defined as a directory containing a `main.rs` file | ||
| //! under the examples root. This module is intentionally filesystem-focused | ||
| //! and does not perform any parsing or rendering. | ||
|
|
||
| use std::fs; | ||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use datafusion::error::Result; | ||
|
|
||
| /// Discovers all example group directories under the given root. | ||
| /// | ||
| /// A directory is considered an example group if it contains a `main.rs` file. | ||
| pub fn discover_example_groups(root: &Path) -> Result<Vec<PathBuf>> { | ||
| let mut groups = Vec::new(); | ||
| for entry in fs::read_dir(root)? { | ||
| let entry = entry?; | ||
| let path = entry.path(); | ||
|
|
||
| if path.is_dir() && path.join("main.rs").exists() { | ||
| groups.push(path); | ||
| } | ||
| } | ||
| groups.sort(); | ||
| Ok(groups) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| use std::fs::{self, File}; | ||
|
|
||
| use tempfile::TempDir; | ||
|
|
||
| #[test] | ||
| fn discover_example_groups_finds_dirs_with_main_rs() -> Result<()> { | ||
| let tmp = TempDir::new()?; | ||
| let root = tmp.path(); | ||
|
|
||
| // valid example group | ||
| let group1 = root.join("group1"); | ||
| fs::create_dir(&group1)?; | ||
| File::create(group1.join("main.rs"))?; | ||
|
|
||
| // not an example group | ||
| let group2 = root.join("group2"); | ||
| fs::create_dir(&group2)?; | ||
|
|
||
| let groups = discover_example_groups(root)?; | ||
|
|
||
| assert_eq!(groups.len(), 1); | ||
| assert_eq!(groups[0], group1); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
113 changes: 113 additions & 0 deletions
113
datafusion-examples/src/utils/example_metadata/layout.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Repository layout utilities. | ||
| //! | ||
| //! This module provides a small helper (`RepoLayout`) that encapsulates | ||
| //! knowledge about the DataFusion repository structure, in particular | ||
| //! where example groups are located relative to the repository root. | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
|
|
||
| use datafusion::error::{DataFusionError, Result}; | ||
|
|
||
| /// Describes the layout of a DataFusion repository. | ||
| /// | ||
| /// This type centralizes knowledge about where example-related | ||
| /// directories live relative to the repository root. | ||
| #[derive(Debug, Clone)] | ||
| pub struct RepoLayout { | ||
| root: PathBuf, | ||
| } | ||
|
|
||
| impl From<&Path> for RepoLayout { | ||
| fn from(path: &Path) -> Self { | ||
| Self { | ||
| root: path.to_path_buf(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl RepoLayout { | ||
| /// Creates a layout from an explicit repository root. | ||
| pub fn from_root(root: PathBuf) -> Self { | ||
| Self { root } | ||
| } | ||
|
|
||
| /// Detects the repository root based on `CARGO_MANIFEST_DIR`. | ||
| /// | ||
| /// This is intended for use from binaries inside the workspace. | ||
| pub fn detect() -> Result<Self> { | ||
| let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
|
|
||
| let root = manifest_dir.parent().ok_or_else(|| { | ||
| DataFusionError::Execution( | ||
| "CARGO_MANIFEST_DIR does not have a parent".to_string(), | ||
| ) | ||
| })?; | ||
|
|
||
| Ok(Self { | ||
| root: root.to_path_buf(), | ||
| }) | ||
| } | ||
|
|
||
| /// Returns the repository root directory. | ||
| pub fn root(&self) -> &Path { | ||
| &self.root | ||
| } | ||
|
|
||
| /// Returns the `datafusion-examples/examples` directory. | ||
| pub fn examples_root(&self) -> PathBuf { | ||
| self.root.join("datafusion-examples").join("examples") | ||
| } | ||
|
|
||
| /// Returns the directory for a single example group. | ||
| /// | ||
| /// Example: `examples/udf` | ||
| pub fn example_group_dir(&self, group: &str) -> PathBuf { | ||
| self.examples_root().join(group) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn detect_sets_non_empty_root() -> Result<()> { | ||
| let layout = RepoLayout::detect()?; | ||
| assert!(!layout.root().as_os_str().is_empty()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn examples_root_is_under_repo_root() -> Result<()> { | ||
| let layout = RepoLayout::detect()?; | ||
| let examples_root = layout.examples_root(); | ||
| assert!(examples_root.starts_with(layout.root())); | ||
| assert!(examples_root.ends_with("datafusion-examples/examples")); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn example_group_dir_appends_group_name() -> Result<()> { | ||
| let layout = RepoLayout::detect()?; | ||
| let group_dir = layout.example_group_dir("foo"); | ||
| assert!(group_dir.ends_with("datafusion-examples/examples/foo")); | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Documentation generator for DataFusion examples. | ||
| //! | ||
| //! # Design goals | ||
| //! | ||
| //! - Keep README.md in sync with runnable examples | ||
| //! - Fail fast on malformed documentation | ||
| //! | ||
| //! # Overview | ||
| //! | ||
| //! Each example group corresponds to a directory under | ||
| //! `datafusion-examples/examples/<group>` containing a `main.rs` file. | ||
| //! Documentation is extracted from structured `//!` comments in that file. | ||
| //! | ||
| //! For each example group, the generator produces: | ||
| //! | ||
| //! ```text | ||
| //! ## <Group Name> Examples | ||
| //! ### Group: `<group>` | ||
| //! #### Category: Single Process | Distributed | ||
| //! | ||
| //! | Subcommand | File Path | Description | | ||
| //! ``` | ||
| //! | ||
| //! # Usage | ||
| //! | ||
| //! Generate documentation for a single group only: | ||
| //! | ||
| //! ```bash | ||
| //! cargo run --bin examples-docs -- dataframe | ||
| //! ``` | ||
| //! | ||
| //! Generate documentation for all examples: | ||
| //! | ||
| //! ```bash | ||
| //! cargo run --bin examples-docs | ||
| //! ``` | ||
|
|
||
| pub mod discover; | ||
| pub mod layout; | ||
| pub mod model; | ||
| pub mod parser; | ||
| pub mod render; | ||
|
|
||
| #[cfg(test)] | ||
| pub mod test_utils; | ||
|
|
||
| pub use layout::RepoLayout; | ||
| pub use model::{Category, ExampleEntry, ExampleGroup, GroupName}; | ||
| pub use parser::parse_main_rs_docs; | ||
| pub use render::generate_examples_readme; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: This will also pass if
main.rsis a folderThere 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.
Agree with you. I'm going to work on it in a follow-up PR as well.