Skip to content

Commit c4561e3

Browse files
authored
Merge pull request #64 from kube-rs/typed-builder
Add optional typed-builder derives
2 parents f956d60 + 3f7a794 commit c4561e3

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ features = ["derive"]
4444
[dev-dependencies]
4545
schemars = "0.8.8"
4646
serde_yaml = "0.8.23"
47+
typed-builder = "0.10.0"

justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ test-mv:
3434

3535
test-agent:
3636
kubectl apply -f tests/agent-crd.yaml
37-
cargo run --bin kopium -- -iAf tests/agent-crd.yaml > tests/gen.rs
37+
cargo run --bin kopium -- -ibAf tests/agent-crd.yaml > tests/gen.rs
3838
echo "pub type CR = Agent;" >> tests/gen.rs
3939
kubectl apply -f tests/agent.yaml
4040
cargo test --test runner -- --nocapture
@@ -69,7 +69,7 @@ test-linkerd-serverauth:
6969

7070
test-linkerd-server:
7171
kubectl apply --server-side -f tests/server-crd.yaml
72-
cargo run --bin kopium -- -iz servers.policy.linkerd.io > tests/gen.rs
72+
cargo run --bin kopium -- -ibz servers.policy.linkerd.io > tests/gen.rs
7373
echo "pub type CR = Server;" >> tests/gen.rs
7474
kubectl apply -f tests/server.yaml
7575
cargo test --test runner -- --nocapture

src/main.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ struct Kopium {
105105
#[structopt(long, short = "d")]
106106
docs: bool,
107107

108+
/// Emit builder derives via the typed_builder crate
109+
#[structopt(long, short = "b")]
110+
builders: bool,
111+
108112
/// Schema mode to use for kube-derive
109113
///
110114
/// The default is --schema=disabled and will compile without a schema,
@@ -293,6 +297,15 @@ impl Kopium {
293297
format_ident!("{}", name)
294298
};
295299
let spec_trimmed_type = m.type_.as_str().replace(&format!("{}Spec", kind), &kind);
300+
if self.builders {
301+
if spec_trimmed_type.starts_with("Option") {
302+
println!("#[builder(default, setter(strip_option))]");
303+
} else if spec_trimmed_type.starts_with("Vec")
304+
|| spec_trimmed_type.starts_with("BTreeMap")
305+
{
306+
println!("#[builder(default)]");
307+
}
308+
}
296309
println!(" pub {}: {},", safe_name, spec_trimmed_type);
297310
}
298311
println!("}}");
@@ -346,6 +359,9 @@ impl Kopium {
346359
// CustomResource first for root struct
347360
derives.insert(0, "CustomResource".to_string());
348361
}
362+
if self.builders {
363+
derives.push("TypedBuilder".to_string());
364+
}
349365
derives.extend(self.derive.clone()); // user derives last in user order
350366
println!("#[derive({})]", derives.join(", "));
351367
}
@@ -358,6 +374,9 @@ impl Kopium {
358374
if !self.hide_kube {
359375
println!("use kube::CustomResource;");
360376
}
377+
if self.builders {
378+
println!("use typed_builder::TypedBuilder;");
379+
}
361380
if self.derive.contains(&"JsonSchema".to_string()) {
362381
println!("use schemars::JsonSchema;");
363382
}

0 commit comments

Comments
 (0)