Arnavion / k8s-openapi

Rust definitions of the resource types in the Kubernetes client API
Apache License 2.0
379 stars 41 forks source link

Mutable 'Meta' trait #66

Closed ctron closed 4 years ago

ctron commented 4 years ago

From what I understood, the Meta trait is immutable. However it would be nice to have a mutable version of that as well.

Assume the following scenario:

    async fn create_or_update<T, F>(
        &self,
        reflector: &Reflector<T>,
        namespace: String,
        name: String,
        mutator: F,
    ) -> Result<()>
    where
        T: Clone + DeserializeOwned + Meta + Default + MetaMut,
        F: FnOnce(T) -> T,
    {
        let object = match reflector.get(&name).await {
            Err(e) => Err(e)?,
            Ok(None) => {
                let mut object: T = Default::default();

                // allow setting meta
                object.meta_mut().replace(ObjectMeta {
                  namespace: Some(namespace),
                  name: Some(name),
                  ..Default::default()
                });

                mutator(object)
            }
            Ok(Some(object)) => mutator(object),
        };

        writer.write(object)
    }

Other way could be:

object.meta_mut().set_namespace(namespace);
object.meta_mut().set_name(name);
Arnavion commented 4 years ago

Meta is a kube trait, but yes k8s_openapi::Metadata should have a fn metadata_mut(&mut self) -> Option<&mut <Self as Metadata>::Ty> too.

ctron commented 4 years ago

Awesome, thanks! Any plans on a new version? :grin: