kopium icon indicating copy to clipboard operation
kopium copied to clipboard

hide prelude imports inside a local module to allow shadowing imports

Open clux opened this issue 1 year ago • 3 comments

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 avatar Apr 01 '24 22:04 clux

cc @Dav1dde who maybe has opinions

clux avatar Apr 01 '24 22:04 clux

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.

Dav1dde avatar Apr 02 '24 10:04 Dav1dde

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.

clux avatar Apr 16 '24 22:04 clux