Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 8 commits into from
Apr 25, 2024

Conversation

clux
Copy link
Member

@clux clux commented Apr 1, 2024

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 added 2 commits April 1, 2024 23:05
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.

fixes #214

Signed-off-by: clux <sszynrae@gmail.com>
@clux clux marked this pull request as ready for review April 1, 2024 22:11
Signed-off-by: clux <sszynrae@gmail.com>
src/main.rs Outdated Show resolved Hide resolved
@clux
Copy link
Member Author

clux commented Apr 1, 2024

cc @Dav1dde who maybe has opinions

@Dav1dde
Copy link
Member

Dav1dde commented Apr 2, 2024

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
Copy link
Member Author

clux commented Apr 16, 2024

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 clux changed the title add a configurable --no-import flag for granular import control hide prelude imports inside a local module to allow shadowing imports Apr 16, 2024
Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
Signed-off-by: clux <sszynrae@gmail.com>
Copy link
Member

@Dav1dde Dav1dde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I was thinking of! Should allow us to add multiple unconditional imports in the future while at the same time not breaking user overrides.

@clux clux merged commit d57a9ac into main Apr 25, 2024
6 checks passed
@clux clux deleted the hide-import-flag branch April 25, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Codegen for standard Condition structure broken when running with hide-prelude
2 participants