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(stackable-versioned): Generate From implementations for automatic versions upgrades #790

Merged
merged 19 commits into from
Jun 5, 2024
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
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions crates/stackable-versioned-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "stackable-versioned-macros"
version = "0.1.0"
authors.workspace = true
license.workspace = true
edition.workspace = true
repository.workspace = true

[lib]
proc-macro = true

[dependencies]
k8s-version = { path = "../k8s-version", features = ["darling"] }

darling.workspace = true
proc-macro2.workspace = true
syn.workspace = true
quote.workspace = true

[dev-dependencies]
rstest.workspace = true
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl ContainerAttributes {
}
}

// TODO (@Techassi): Add validation for skip(from) for last version,
// which will skip nothing, because nothing is generated in the first
// place.

// Ensure every version is unique and isn't declared multiple times. This
// is inspired by the itertools all_unique function.
let mut unique = HashSet::new();
Expand All @@ -83,10 +87,12 @@ impl ContainerAttributes {
///
/// - `name` of the version, like `v1alpha1`.
/// - `deprecated` flag to mark that version as deprecated.
/// - `skip` option to skip generating various pieces of code.
#[derive(Clone, Debug, FromMeta)]
pub(crate) struct VersionAttributes {
pub(crate) deprecated: Flag,
pub(crate) name: Version,
pub(crate) skip: Option<SkipOptions>,
}

/// This struct contains supported container options.
Expand All @@ -95,7 +101,19 @@ pub(crate) struct VersionAttributes {
///
/// - `allow_unsorted`, which allows declaring versions in unsorted order,
/// instead of enforcing ascending order.
/// - `skip` option to skip generating various pieces of code.
#[derive(Clone, Debug, Default, FromMeta)]
pub(crate) struct ContainerOptions {
pub(crate) allow_unsorted: Flag,
pub(crate) skip: Option<SkipOptions>,
}

/// This struct contains supported skip options.
///
/// Supported options are:
///
/// - `from` flag, which skips generating [`From`] implementations when provided.
#[derive(Clone, Debug, Default, FromMeta)]
pub(crate) struct SkipOptions {
pub(crate) from: Flag,
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use darling::{util::SpannedValue, Error, FromField, FromMeta};
use k8s_version::Version;
use syn::{Field, Ident};
use proc_macro2::Span;
use syn::{Field, Ident, Path};

use crate::{attrs::container::ContainerAttributes, consts::DEPRECATED_PREFIX};

Expand Down Expand Up @@ -40,6 +41,16 @@ pub(crate) struct FieldAttributes {
#[derive(Clone, Debug, FromMeta)]
pub(crate) struct AddedAttributes {
pub(crate) since: SpannedValue<Version>,

#[darling(rename = "default", default = "default_default_fn")]
pub(crate) default_fn: SpannedValue<Path>,
}

fn default_default_fn() -> SpannedValue<Path> {
SpannedValue::new(
syn::parse_str("std::default::Default::default").unwrap(),
Span::call_site(),
)
}

#[derive(Clone, Debug, FromMeta)]
Expand Down Expand Up @@ -139,6 +150,7 @@ impl FieldAttributes {

// First, validate that the added version is less than the deprecated
// version.
// NOTE (@Techassi): Is this already covered by the code below?
if let (Some(added_version), Some(deprecated_version)) = (added_version, deprecated_version)
{
if added_version >= deprecated_version {
Expand Down
Loading
Loading