kube-rs / kopium

Kubernetes OPenapI UnMangler
Apache License 2.0
113 stars 21 forks source link

hide prelude imports inside a local module to allow shadowing imports #222

Closed clux closed 6 months ago

clux commented 7 months ago

allows more control than --hide-prelude which disables everything, but is also likely to break for people who only override one struct, because we might add more structs to our prelude (as happened when we added Condition to our imports).

following David's Idea below, the generated output now wraps imports in a private module:

// WARNING: generated by kopium - manual changes will be overwritten
// kopium command: kopium multiversions.clux.dev -A
// kopium version: 0.17.2

#[allow(unused_imports)]
mod prelude {
    pub use kube::CustomResource;
    pub use serde::{Serialize, Deserialize};
    pub use std::collections::BTreeMap;
    pub use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString;
}
use self::prelude::*;

so that to override any of these users can add extra overrides to any part of it using shadowing:

cargo run --bin kopium -- prometheusrules.monitoring.coreos.com > generated.rs
echo "use mycrate::IntStringWrap as IntOrString;" >> generated.rs

fixes #214

clux commented 7 months ago

cc @Dav1dde who maybe has opinions

Dav1dde commented 7 months ago

Not quite up to date, the main problem why hide-prelude exists is to silence unused import warnings or allow easier import overrides?

I think instead of trying to be smart with imports another solution could be to just keep filling the prelude but hide all unused warnings etc.

#![allow(unused)]

mod prelude {
    pub use std::collections::HashMap as Map;
}
use self::prelude::*;

// User supplied
use std::collections::BTreeMap as Map;

The generated code doesn't need to be perfect (it's machine generated after all), I've ran into similar issues in glad, where it's just better to take the solution which is readable enough but allows for way more flexibility in generation.

clux commented 6 months ago

Not quite up to date, the main problem why hide-prelude exists is to silence unused import warnings or allow easier import overrides?

yeah!

I think instead of trying to be smart with imports another solution could be to just keep filling the prelude but hide all unused warnings etc.

#![allow(unused)]

mod prelude {
    pub use std::collections::HashMap as Map;
}
use self::prelude::*;

// User supplied
use std::collections::BTreeMap as Map;

for some reason i didn't understand this the first time i looked at it, but now i think it makes sense. if people can shadow imports as parts of generation such as:

kopium X > crd.rs
echo "use mycrate::Map as Map;" >> crd.rs

then your setup there seems a lot easier to maintain, i'll try it out! sorry for the delay.