Skip to content

Commit

Permalink
Remove TypeMeta fields from derived kind objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nightkr committed Dec 3, 2021
1 parent 7877fc5 commit 7b9a983
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
29 changes: 20 additions & 9 deletions kube-derive/src/custom_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
// We enforce metadata + spec's existence (always there)
// => No default impl
let rootident = Ident::new(&struct_name, Span::call_site());
let rootident_str = rootident.to_string();

// if status set, also add that
let StatusInformation {
Expand All @@ -196,9 +197,15 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
impl_hasstatus,
} = process_status(&rootident, &status, &visibility, &kube_core);
let has_status = status.is_some();
let serialize_status = if has_status {
quote! {
obj.serialize_field("status", &self.status)?;
}
} else {
quote! {}
};

let mut derive_paths: Vec<Path> = vec![
syn::parse_quote! { #serde::Serialize },
syn::parse_quote! { #serde::Deserialize },
syn::parse_quote! { Clone },
syn::parse_quote! { Debug },
Expand Down Expand Up @@ -239,10 +246,6 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
#[derive(#(#derive_paths),*)]
#[serde(rename_all = "camelCase")]
#visibility struct #rootident {
#schemars_skip
#visibility api_version: String,
#schemars_skip
#visibility kind: String,
#schemars_skip
#visibility metadata: #k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta,
#visibility spec: #ident,
Expand All @@ -251,8 +254,6 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
impl #rootident {
pub fn new(name: &str, spec: #ident) -> Self {
Self {
api_version: <#rootident as #kube_core::Resource>::api_version(&()).to_string(),
kind: <#rootident as #kube_core::Resource>::kind(&()).to_string(),
metadata: #k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta {
name: Some(name.to_string()),
..Default::default()
Expand All @@ -262,6 +263,18 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
}
}
}
impl #serde::Serialize for #rootident {
fn serialize<S: #serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
use #serde::ser::SerializeStruct;
let mut obj = ser.serialize_struct(#rootident_str, 4 + usize::from(#has_status))?;
obj.serialize_field("apiVersion", &<#rootident as #kube_core::Resource>::api_version(&()))?;
obj.serialize_field("kind", &<#rootident as #kube_core::Resource>::kind(&()))?;
obj.serialize_field("metadata", &self.metadata)?;
obj.serialize_field("spec", &self.spec)?;
#serialize_status
obj.end()
}
}
};

// 2. Implement Resource trait
Expand Down Expand Up @@ -310,8 +323,6 @@ pub(crate) fn derive(input: proc_macro2::TokenStream) -> proc_macro2::TokenStrea
impl Default for #rootident {
fn default() -> Self {
Self {
api_version: <#rootident as #kube_core::Resource>::api_version(&()).to_string(),
kind: <#rootident as #kube_core::Resource>::kind(&()).to_string(),
metadata: #k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta::default(),
spec: Default::default(),
#status_default
Expand Down
45 changes: 38 additions & 7 deletions kube-derive/tests/crd_schema_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use chrono::{DateTime, Utc};
use chrono::{DateTime, NaiveDateTime, Utc};
use kube_derive::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
Expand All @@ -16,6 +16,7 @@ use serde::{Deserialize, Serialize};
shortname = "f"
)]
#[kube(apiextensions = "v1")]
#[serde(rename_all = "camelCase")]
struct FooSpec {
non_nullable: String,

Expand Down Expand Up @@ -57,6 +58,36 @@ fn test_shortnames() {
assert_eq!(&["fo", "f"], Foo::shortnames());
}

#[test]
fn test_serialized_matches_expected() {
assert_eq!(
serde_json::to_value(Foo::new("bar", FooSpec {
non_nullable: "asdf".to_string(),
non_nullable_with_default: "asdf".to_string(),
nullable_skipped: None,
nullable: None,
nullable_skipped_with_default: None,
nullable_with_default: None,
timestamp: DateTime::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc),
}))
.unwrap(),
serde_json::json!({
"apiVersion": "clux.dev/v1",
"kind": "Foo",
"metadata": {
"name": "bar",
},
"spec": {
"nonNullable": "asdf",
"nonNullableWithDefault": "asdf",
"nullable": null,
"nullableWithDefault": null,
"timestamp": "1970-01-01T00:00:00Z",
}
})
)
}

#[test]
fn test_crd_schema_matches_expected() {
use kube::core::CustomResourceExt;
Expand Down Expand Up @@ -90,28 +121,28 @@ fn test_crd_schema_matches_expected() {
"properties": {
"spec": {
"properties": {
"non_nullable": {
"nonNullable": {
"type": "string"
},
"non_nullable_with_default": {
"nonNullableWithDefault": {
"default": "default_value",
"type": "string"
},

"nullable_skipped": {
"nullableSkipped": {
"nullable": true,
"type": "string"
},
"nullable": {
"nullable": true,
"type": "string"
},
"nullable_skipped_with_default": {
"nullableSkippedWithDefault": {
"default": "default_nullable",
"nullable": true,
"type": "string"
},
"nullable_with_default": {
"nullableWithDefault": {
"default": "default_nullable",
"nullable": true,
"type": "string"
Expand All @@ -123,7 +154,7 @@ fn test_crd_schema_matches_expected() {
}
},
"required": [
"non_nullable",
"nonNullable",
"timestamp"
],
"type": "object"
Expand Down

0 comments on commit 7b9a983

Please sign in to comment.