-
Notifications
You must be signed in to change notification settings - Fork 352
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
fix(trait): bool trait props should be pointer, otherwise omitted in CR #1848 #1850
Conversation
I think there might be an underlying issue with how CLI boolean properties get mapped into trait structs. So before we patch all the traits, let me find out why. |
@astefanutti I spotted the underlying issue here. So as I understand it it's essentially how mapstructure handles bool struct properties when they are annotated with I thought that |
@tadayosi thanks. My understanding is that the issue occurs for boolean fields for which the traits constructor default the value to |
@astefanutti Ah yes, my comment was incorrect. It is not mapstructure but JSON marshalling as you explained. Thanks for your investigation. |
One more thing. In my test case |
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.
Can I suggest that we remove default trait fields initialisation from the trait factory methods? I now see it as an anti-pattern somehow, and the defaulting logic would be better move into the Configure
method. That would also prevent using that lovely &[]bool{true}[0]
construct 😉.
Do you mean that |
@astefanutti Yes, in the sense that |
@tadayosi ah right. I did not mention that in my review, as I already agree with the approach you've taken. I think that all the boolean fields should be declared as pointers, as even if the current default is |
To be clear, the root cause is not whether the prop has a certain default value or not, but the way JSON marshalling handles "zero" primitive values when "omitempty" is added:
So theoretically every prop (including Tomorrow I'll follow up with Antonin's suggestion of moving the defaulting logic to Configure. |
After investigation I realised that it's not as easy as I initially thought. It's because each trait's It's still possible to move the defaulting logic to Instead I just removed this fancy |
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'd suggest to create the following method into the util.go
file and use it so that the structs initialisation can be inlined:
func BoolP(b bool) *bool {
return &b
}
For example, that would give:
return &affinityTrait{
BaseTrait: NewBaseTrait("affinity", 1300),
PodAffinity: BoolP(false),
PodAntiAffinity: BoolP(false),
}
@tadayosi I see what you mean, that is convenient to keep the defaulting early in the trait struct creation. |
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.
Thanks!
This fixes #1848
Release Note