-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
Improve detecting whether default can be derived #241
Conversation
ca3ac84
to
ea11e55
Compare
Hey thanks for this. I think the idea here is interesting, but I am a little confused about how practically useful it is. Maybe I am missing something, but if you have an enum embedded deeply within your spec struct, and want to derive If that's the case, isn't it better to not ask to derive The thing I do for enums is to |
Yes, but this change was useful for me when I did not want to go through the code and manually implement default for enums. My use case was specifically generating types for Here are the CRDs. I am specifically using
CertificateSpec {
some_prop: value,
..Default::default(),
} For that to work, I need default to be derived on Now, the output won't compile, because /// CertificateAdditionalOutputFormat defines an additional output format of a Certificate resource. These contain supplementary data formats of the signed certificate chain and paired private key.
#[derive(Serialize, Deserialize, Clone, TypedBuilder, Debug, PartialEq, JsonSchema)]
pub struct CertificateAdditionalOutputFormats {
/// Type is the name of the format type that should be written to the Certificate's target Secret.
#[serde(rename = "type")]
pub r#type: CertificateAdditionalOutputFormatsType,
}
/// CertificateAdditionalOutputFormat defines an additional output format of a Certificate resource. These contain supplementary data formats of the signed certificate chain and paired private key.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub enum CertificateAdditionalOutputFormatsType {
#[serde(rename = "DER")]
Der,
#[serde(rename = "CombinedPEM")]
CombinedPem,
}
I could of course manually change it, but I want to avoid manual changes in generated code. Also, this PR contains another small change:
|
This checks if a struct contains a required member for which `Default` is not implemented (Enums or other structs which don't have `Default`). If it does, Default will not be derived for that struct. Signed-off-by: Aaron Dewes <[email protected]>
ea11e55
to
140075b
Compare
Ah, thanks for clarifying. You're searching the other way from what I was expecting. I thought you wouldn't get a (For clarity I typically would have generated this to I like the |
Signed-off-by: Aaron Dewes <[email protected]>
608444f
to
936dd56
Compare
d7b5f33
to
991292e
Compare
When searching recursively from the top, we can cache the result. If we have a deeply nested struct A -> B -> C -> D -> Enum and start by checking A, the other structs B, C and D can cache the result of the recursive search. This also adds more tests. Signed-off-by: Aaron Dewes <[email protected]>
991292e
to
b11337e
Compare
…ed for the top level spec This is rare, but for example happens with cert-manager's "Challenge" resource. Signed-off-by: Aaron Dewes <[email protected]>
This option enforces the old behaviour around derive Signed-off-by: Aaron Dewes <[email protected]>
I now added a flag Please let me know if that sounds okay to you, or if I should instead extend the syntax for |
Signed-off-by: Aaron Dewes <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like the optimisation. added some minor import/default cleanups and a suggestion to invert the flag to avoid breaking things. possibly an inverted flag could be generalised to take the type we are doing detection on in the future.
Signed-off-by: Aaron Dewes <[email protected]>
Signed-off-by: Aaron Dewes <[email protected]>
I've implemented your suggestions. The I'll check if I can add a way to also implement support for that in another PR, but I'm not sure if that is going to work out. |
Yeah, agreed, that is an orthogonal concern atm. But proper handling of it would also be very welcome! |
I'll merge in and probably do a release early next week. If you have other things you want to try like the Thanks a lot for this! |
FYI, I think this behavior is currently always on (not opt-in like expected) - even without the |
There's a typo I didn't notice. i'll fix it |
Thank you both. I'll ship a patch release for this. |
This checks if a struct contains a required member for which
Default
is not implemented (Enums or other structs which don't haveDefault
). If it does, Default will not be derived for that struct.