Skip to content

refactor(stackable-versioned): Move preserve_module into options() #961

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 2 commits into from
Feb 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
version(name = "v1alpha1"),
version(name = "v1"),
version(name = "v2alpha1"),
preserve_module
options(preserve_module)
)]
// ---
pub(crate) mod versioned {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
version(name = "v1alpha1"),
version(name = "v1"),
version(name = "v2alpha1"),
preserve_module
options(preserve_module)
)]
// ---
pub(crate) mod versioned {
Expand Down
24 changes: 14 additions & 10 deletions crates/stackable-versioned-macros/src/attrs/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ use darling::{
use itertools::Itertools;
use k8s_version::Version;

pub trait CommonOptions {
fn allow_unsorted(&self) -> Flag;
}

#[derive(Debug, FromMeta)]
#[darling(and_then = CommonRootArguments::validate)]
pub(crate) struct CommonRootArguments {
pub(crate) struct CommonRootArguments<T>
where
T: CommonOptions + Default,
{
#[darling(default)]
pub(crate) options: RootOptions,
pub(crate) options: T,

#[darling(multiple, rename = "version")]
pub(crate) versions: SpannedValue<Vec<VersionArguments>>,
}

impl CommonRootArguments {
impl<T> CommonRootArguments<T>
where
T: CommonOptions + Default,
{
fn validate(mut self) -> Result<Self> {
let mut errors = Error::accumulator();

Expand All @@ -32,7 +42,7 @@ impl CommonRootArguments {
// (if allow_unsorted is set).
self.versions.sort_by(|lhs, rhs| lhs.name.cmp(&rhs.name));

if !self.options.allow_unsorted.is_present() && !is_sorted {
if !self.options.allow_unsorted().is_present() && !is_sorted {
let versions = self.versions.iter().map(|v| v.name).join(", ");

errors.push(Error::custom(format!(
Expand All @@ -59,12 +69,6 @@ impl CommonRootArguments {
}
}

#[derive(Clone, Debug, Default, FromMeta)]
pub(crate) struct RootOptions {
pub(crate) allow_unsorted: Flag,
pub(crate) skip: Option<SkipArguments>,
}

/// This struct contains supported version arguments.
///
/// Supported arguments are:
Expand Down
18 changes: 15 additions & 3 deletions crates/stackable-versioned-macros/src/attrs/container.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use darling::{Error, FromAttributes, FromMeta, Result};
use darling::{util::Flag, Error, FromAttributes, FromMeta, Result};

use crate::attrs::{
common::{CommonRootArguments, SkipArguments},
common::{CommonOptions, CommonRootArguments, SkipArguments},
k8s::KubernetesArguments,
};

Expand All @@ -12,7 +12,7 @@ pub(crate) struct StandaloneContainerAttributes {
pub(crate) kubernetes_arguments: Option<KubernetesArguments>,

#[darling(flatten)]
pub(crate) common_root_arguments: CommonRootArguments,
pub(crate) common: CommonRootArguments<StandaloneContainerOptions>,
}

impl StandaloneContainerAttributes {
Expand All @@ -25,6 +25,18 @@ impl StandaloneContainerAttributes {
}
}

#[derive(Debug, FromMeta, Default)]
pub(crate) struct StandaloneContainerOptions {
pub(crate) allow_unsorted: Flag,
pub(crate) skip: Option<SkipArguments>,
}

impl CommonOptions for StandaloneContainerOptions {
fn allow_unsorted(&self) -> Flag {
self.allow_unsorted
}
}

#[derive(Debug, FromAttributes)]
#[darling(
attributes(versioned),
Expand Down
16 changes: 14 additions & 2 deletions crates/stackable-versioned-macros/src/attrs/module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use darling::{util::Flag, FromMeta};

use crate::attrs::common::CommonRootArguments;
use crate::attrs::common::{CommonOptions, CommonRootArguments, SkipArguments};

#[derive(Debug, FromMeta)]
pub(crate) struct ModuleAttributes {
#[darling(flatten)]
pub(crate) common_root_arguments: CommonRootArguments,
pub(crate) common: CommonRootArguments<ModuleOptions>,
}

#[derive(Debug, FromMeta, Default)]
pub(crate) struct ModuleOptions {
pub(crate) allow_unsorted: Flag,
pub(crate) skip: Option<SkipArguments>,
pub(crate) preserve_module: Flag,
}

impl CommonOptions for ModuleOptions {
fn allow_unsorted(&self) -> Flag {
self.allow_unsorted
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Container {
let options = ContainerOptions {
kubernetes_options: None,
skip_from: attributes
.common_root_arguments
.common
.options
.skip
.map_or(false, |s| s.from.is_present()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Container {

let options = ContainerOptions {
skip_from: attributes
.common_root_arguments
.common
.options
.skip
.map_or(false, |s| s.from.is_present()),
Expand Down
4 changes: 2 additions & 2 deletions crates/stackable-versioned-macros/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) struct VersionDefinition {
impl From<&StandaloneContainerAttributes> for Vec<VersionDefinition> {
fn from(attributes: &StandaloneContainerAttributes) -> Self {
attributes
.common_root_arguments
.common
.versions
.iter()
.map(|v| VersionDefinition {
Expand All @@ -53,7 +53,7 @@ impl From<&StandaloneContainerAttributes> for Vec<VersionDefinition> {
impl From<&ModuleAttributes> for Vec<VersionDefinition> {
fn from(attributes: &ModuleAttributes) -> Self {
attributes
.common_root_arguments
.common
.versions
.iter()
.map(|v| VersionDefinition {
Expand Down
11 changes: 8 additions & 3 deletions crates/stackable-versioned-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ mod utils;
/// #[versioned(
/// version(name = "v1alpha1"),
/// version(name = "v1"),
/// preserve_module
/// options(preserve_module)
/// )]
/// mod versioned {
/// struct Foo {
Expand Down Expand Up @@ -732,9 +732,14 @@ fn versioned_impl(attrs: proc_macro2::TokenStream, input: Item) -> proc_macro2::
};

let versions: Vec<VersionDefinition> = (&module_attributes).into();
let preserve_modules = module_attributes.preserve_module.is_present();
let preserve_modules = module_attributes
.common
.options
.preserve_module
.is_present();

let skip_from = module_attributes
.common_root_arguments
.common
.options
.skip
.as_ref()
Expand Down
6 changes: 6 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Changed

- BREAKING: Move `preserve_module` option into `options` to unify option interface ([#961]).

[#961]: https://github.com/stackabletech/operator-rs/pull/961

## [0.5.1] - 2025-02-14

### Added
Expand Down