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

refactor(stackable-versioned): Convert Override to enum #922

Merged
merged 4 commits into from
Nov 28, 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
70 changes: 22 additions & 48 deletions crates/stackable-versioned-macros/src/codegen/container/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ pub(crate) struct KubernetesCrateOptions {
impl Default for KubernetesCrateOptions {
fn default() -> Self {
Self {
k8s_openapi: Override::new_default(parse_quote! { ::k8s_openapi }),
serde_json: Override::new_default(parse_quote! { ::serde_json }),
kube_core: Override::new_default(parse_quote! { ::kube::core }),
schemars: Override::new_default(parse_quote! { ::schemars }),
serde: Override::new_default(parse_quote! { ::serde }),
k8s_openapi: Override::Default(parse_quote! { ::k8s_openapi }),
serde_json: Override::Default(parse_quote! { ::serde_json }),
kube_core: Override::Default(parse_quote! { ::kube::core }),
schemars: Override::Default(parse_quote! { ::schemars }),
serde: Override::Default(parse_quote! { ::serde }),
}
}
}
Expand All @@ -330,23 +330,23 @@ impl From<KubernetesCrateArguments> for KubernetesCrateOptions {
let mut crate_options = Self::default();

if let Some(k8s_openapi) = args.k8s_openapi {
crate_options.k8s_openapi = Override::new_custom(k8s_openapi);
crate_options.k8s_openapi = Override::Overridden(k8s_openapi);
}

if let Some(serde_json) = args.serde_json {
crate_options.serde_json = Override::new_custom(serde_json);
crate_options.serde_json = Override::Overridden(serde_json);
}

if let Some(kube_core) = args.kube_core {
crate_options.kube_core = Override::new_custom(kube_core);
crate_options.kube_core = Override::Overridden(kube_core);
}

if let Some(schemars) = args.schemars {
crate_options.schemars = Override::new_custom(schemars);
crate_options.schemars = Override::Overridden(schemars);
}

if let Some(serde) = args.serde {
crate_options.serde = Override::new_custom(serde);
crate_options.serde = Override::Overridden(serde);
}

crate_options
Expand All @@ -365,23 +365,23 @@ impl ToTokens for KubernetesCrateOptions {
serde,
} = self;

if let Some(k8s_openapi) = k8s_openapi.get_if_overridden() {
if let Override::Overridden(k8s_openapi) = k8s_openapi {
crate_overrides.extend(quote! { k8s_openapi = #k8s_openapi, });
}

if let Some(serde_json) = serde_json.get_if_overridden() {
if let Override::Overridden(serde_json) = serde_json {
crate_overrides.extend(quote! { serde_json = #serde_json, });
}

if let Some(kube_core) = kube_core.get_if_overridden() {
if let Override::Overridden(kube_core) = kube_core {
crate_overrides.extend(quote! { kube_core = #kube_core, });
}

if let Some(schemars) = schemars.get_if_overridden() {
if let Override::Overridden(schemars) = schemars {
crate_overrides.extend(quote! { schemars = #schemars, });
}

if let Some(serde) = serde.get_if_overridden() {
if let Override::Overridden(serde) = serde {
crate_overrides.extend(quote! { serde = #serde, });
}

Expand All @@ -393,44 +393,18 @@ impl ToTokens for KubernetesCrateOptions {

/// Wraps a value to indicate whether it is original or has been overridden.
#[derive(Debug)]
pub(crate) struct Override<T> {
is_overridden: bool,
inner: T,
}

impl<T> Override<T> {
/// Mark a value as a default.
///
/// This is used to indicate that the value is a default and was not overridden.
pub(crate) fn new_default(inner: T) -> Self {
Override {
is_overridden: false,
inner,
}
}

/// Mark a value as overridden.
///
/// This is used to indicate that the value was overridden and not the default.
pub(crate) fn new_custom(inner: T) -> Self {
Override {
is_overridden: true,
inner,
}
}

pub(crate) fn get_if_overridden(&self) -> Option<&T> {
match &self.is_overridden {
true => Some(&self.inner),
false => None,
}
}
pub(crate) enum Override<T> {
Default(T),
Overridden(T),
}

impl<T> Deref for Override<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.inner
match &self {
Override::Default(inner) => inner,
Override::Overridden(inner) => inner,
}
}
}
2 changes: 2 additions & 0 deletions crates/stackable-versioned/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.

- Simplify crate override handling and generation ([#919]).
- Bump Rust to 1.82.0 ([#891]).
- Refactor the Override type ([#922]).

### Fixed

Expand All @@ -33,6 +34,7 @@ All notable changes to this project will be documented in this file.
[#914]: https://github.com/stackabletech/operator-rs/pull/914
[#919]: https://github.com/stackabletech/operator-rs/pull/919
[#920]: https://github.com/stackabletech/operator-rs/pull/920
[#922]: https://github.com/stackabletech/operator-rs/pull/922

## [0.4.1] - 2024-10-23

Expand Down