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

Improve detecting whether default can be derived #241

Merged
merged 8 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 10 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl Kopium {
}
self.print_docstr(&s.docs, "");
if s.is_main_container() {
self.print_derives(s);
self.print_derives(s, &structs);
//root struct gets kube derives unless opted out
if !self.hide_kube {
println!(
Expand Down Expand Up @@ -277,7 +277,7 @@ impl Kopium {
println!("pub struct {} {{", s.name);
}
} else {
self.print_derives(s);
self.print_derives(s, &structs);
let spec_trimmed_name = s.name.as_str().replace(&format!("{}Spec", kind), kind);
if s.is_enum {
println!("pub enum {} {{", spec_trimmed_name);
Expand Down Expand Up @@ -338,23 +338,24 @@ impl Kopium {
}
}

fn print_derives(&self, s: &Container) {
fn print_derives(&self, s: &Container, containers: &[Container]) {
let mut derives = vec!["Serialize", "Deserialize", "Clone", "Debug"];

if s.is_main_container() && !self.hide_kube {
// CustomResource first for root struct
derives.insert(0, "CustomResource");
}
if self.builders {

// TypedBuilder does not work with enums
if self.builders && !s.is_enum {
derives.push("TypedBuilder");
}

for derive in &self.derive {
if s.is_enum && derive.derived_trait == "Default" {
// Need to drop Default from enum as this cannot be derived.
// Enum defaults need to either be manually derived
// or we can insert enum defaults
continue;
if derive.derived_trait == "Default" {
if !s.can_derive_default(containers) {
continue;
}
}

if derive.is_applicable_to(s) && !derives.contains(&derive.derived_trait.as_str()) {
Expand Down
30 changes: 30 additions & 0 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ impl Container {
pub fn contains_conditions(&self) -> bool {
self.members.iter().any(|m| m.type_.contains("Vec<Condition>"))
}

pub fn can_derive_default(&self, containers: &[Container]) -> bool {
clux marked this conversation as resolved.
Show resolved Hide resolved
if self.is_enum {
// Need to drop Default from enum as this cannot be derived.
// Enum defaults need to either be manually derived
// or we can insert enum defaults
return false;
}

for m in &self.members {
if !m.type_.contains('<')
&& !m.type_.contains("::")
&& m.type_ != "String"
&& m.type_ != "IntOrString"
&& m.type_ != "NaiveDate"
&& m.type_ != "DateTime"
&& m.type_.chars().next().unwrap_or_default().is_uppercase()
clux marked this conversation as resolved.
Show resolved Hide resolved
{
if containers
.iter()
.find(|c| c.name == m.type_)
.is_some_and(|c| !c.can_derive_default(containers))
clux marked this conversation as resolved.
Show resolved Hide resolved
{
return false;
}
}
}

true
}
}

impl Container {
Expand Down
Loading