-
Notifications
You must be signed in to change notification settings - Fork 687
feat: parse Ref from version and version numbers #5584
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ use std::io::ErrorKind; | |
| pub const MAIN_BRANCH: &str = "main"; | ||
|
|
||
| /// Lance Ref | ||
| #[derive(Debug, Clone)] | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum Ref { | ||
| // Version number points of the current branch | ||
| VersionNumber(u64), | ||
|
|
@@ -45,10 +45,51 @@ impl From<u64> for Ref { | |
|
|
||
| impl From<&str> for Ref { | ||
| fn from(reference: &str) -> Self { | ||
| let reference = reference.trim(); | ||
| if let Some((branch, version)) = reference.split_once(':') { | ||
| if branch.is_empty() { | ||
| return Tag(reference.to_string()); | ||
| } | ||
|
|
||
| let branch = if branch == MAIN_BRANCH { | ||
| None | ||
| } else if check_valid_branch(branch).is_ok() { | ||
| Some(branch.to_string()) | ||
| } else { | ||
| return Tag(reference.to_string()); | ||
| }; | ||
|
|
||
| let version = match version { | ||
| "" | "latest" => None, | ||
| v => match v.parse::<u64>() { | ||
| Ok(v) => Some(v), | ||
| Err(_) => return Tag(reference.to_string()), | ||
| }, | ||
| }; | ||
|
|
||
| return Version(branch, version); | ||
| } | ||
|
|
||
| if reference == MAIN_BRANCH { | ||
| return Version(None, None); | ||
| } | ||
|
|
||
| if reference.chars().all(|c| c.is_ascii_digit()) { | ||
| if let Ok(v) = reference.parse::<u64>() { | ||
| return VersionNumber(v); | ||
| } | ||
| } | ||
|
|
||
| Tag(reference.to_string()) | ||
| } | ||
| } | ||
|
|
||
| impl From<String> for Ref { | ||
| fn from(reference: String) -> Self { | ||
| Self::from(reference.as_str()) | ||
| } | ||
| } | ||
|
|
||
| impl From<(&str, u64)> for Ref { | ||
| fn from(reference: (&str, u64)) -> Self { | ||
| Version(standardize_branch(reference.0), Some(reference.1)) | ||
|
|
@@ -221,6 +262,13 @@ impl Tags<'_> { | |
| message: format!("tag {} already exists", tag), | ||
| }); | ||
| } | ||
|
|
||
| let branch_file = branch_contents_path(&root_location.path, tag); | ||
| if self.object_store().exists(&branch_file).await? { | ||
| return Err(Error::RefConflict { | ||
| message: format!("tag {} conflicts with existing branch", tag), | ||
| }); | ||
| } | ||
| let tag_contents = self.build_tag_content_by_ref(reference).await?; | ||
|
|
||
| self.object_store() | ||
|
|
@@ -257,6 +305,13 @@ impl Tags<'_> { | |
| message: format!("tag {} does not exist", tag), | ||
| }); | ||
| } | ||
|
|
||
| let branch_file = branch_contents_path(&root_location.path, tag); | ||
| if self.object_store().exists(&branch_file).await? { | ||
| return Err(Error::RefConflict { | ||
| message: format!("tag {} conflicts with existing branch", tag), | ||
| }); | ||
| } | ||
| let tag_contents = self.build_tag_content_by_ref(reference).await?; | ||
|
|
||
| self.object_store() | ||
|
|
@@ -393,6 +448,13 @@ impl Branches<'_> { | |
|
|
||
| let source_branch = source_branch.and_then(standardize_branch); | ||
| let root_location = self.refs.root()?; | ||
|
|
||
| let tag_file = tag_path(&root_location.path, branch_name); | ||
| if self.object_store().exists(&tag_file).await? { | ||
| return Err(Error::RefConflict { | ||
| message: format!("branch {} conflicts with existing tag", branch_name), | ||
| }); | ||
| } | ||
| let branch_file = branch_contents_path(&root_location.path, branch_name); | ||
| if self.object_store().exists(&branch_file).await? { | ||
| return Err(Error::RefConflict { | ||
|
|
@@ -793,6 +855,41 @@ mod tests { | |
|
|
||
| use rstest::rstest; | ||
|
|
||
| #[rstest] | ||
| fn test_parse_ref_ok( | ||
| #[values( | ||
| ("0", Ref::VersionNumber(0)), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another thought I proposed in Spark is that If a branch has a Basically the first 3 characters equaling What do we think? @Xuanwo @wjones127 @hamersaw
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds great to me! Just a few clarifying questions:
I've been trying to convince myself that it would be more simple (parsing + API) to only support branches in table IDs and versions / tags in |
||
| ("42", Ref::VersionNumber(42)), | ||
| ("main", Ref::Version(None, None)), | ||
| ("main:", Ref::Version(None, None)), | ||
| ("main:latest", Ref::Version(None, None)), | ||
| ("main:10", Ref::Version(None, Some(10))), | ||
| ("feature/a:", Ref::Version(Some("feature/a".to_string()), None)), | ||
| ("feature/a:latest", Ref::Version(Some("feature/a".to_string()), None)), | ||
| ("feature/a:10", Ref::Version(Some("feature/a".to_string()), Some(10))), | ||
| ("tag1", Ref::Tag("tag1".to_string())) | ||
| )] | ||
| (input, expected): (&str, Ref), | ||
| ) { | ||
| assert_eq!(Ref::from(input), expected); | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn test_ref_from_str_invalid_syntax_falls_back_to_tag( | ||
| #[values( | ||
| "", | ||
| ":", | ||
| ":10", | ||
| "main:bad", | ||
| "feature/a", | ||
| "/start-with-slash", | ||
| "feature//double-slash" | ||
| )] | ||
| input: &str, | ||
| ) { | ||
| assert_eq!(Ref::from(input), Ref::Tag(input.trim().to_string())); | ||
| } | ||
|
|
||
| #[rstest] | ||
| fn test_ok_ref( | ||
| #[values( | ||
|
|
||
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:
standardize_branchwas designed as the entry point for this scenario