Skip to content

Conversation

@vax-r
Copy link
Contributor

@vax-r vax-r commented Oct 16, 2025

Summary

As the TODO in limayaml/defaults.go claims, change the type of
"Script" under "type Provision struct" from "string" to "*string".

Update all callers to pass a pointer and handle "nil" in comparisons,
validation, and marshalling.

AkihiroSuda
AkihiroSuda previously approved these changes Oct 16, 2025
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda added this to the v2.0.0 milestone Oct 16, 2025
@AkihiroSuda AkihiroSuda added the kind/refactoring Refactoring label Oct 16, 2025
@vax-r vax-r force-pushed the provision_pointer branch 2 times, most recently from 7b77052 to 842bc25 Compare October 16, 2025 11:36
As the TODO in limayaml/defaults.go claims, change the type of
"Script" under "type Provision struct" from "string" to "*string".

Update all callers to pass a pointer and handle "nil" in comparisons,
validation, and marshalling.

Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
@vax-r vax-r force-pushed the provision_pointer branch from 842bc25 to e7cb6aa Compare October 16, 2025 11:39
@vax-r
Copy link
Contributor Author

vax-r commented Oct 16, 2025

@AkihiroSuda -
Thanks !
I just update the PR and I think all the CI tests are passed.
Please take a look if you are available !

@vax-r vax-r requested a review from AkihiroSuda October 16, 2025 12:32
@AkihiroSuda AkihiroSuda merged commit 3207075 into lima-vm:master Oct 16, 2025
37 checks passed
@jandubois
Copy link
Member

I think FillDefaults() should have:

if y.Provision.Script == nil {
    y.Provision.Script = ptr.Of("")
}

instead of having p.Script != nil && *p.Script != "" multiple times in Validate().


I would also suggest to change Probe.Script to a pointer as well, for symmetry. It is confusing to see *p.Script in the Provision code, but p.Script in the Probe code for the same thing.

@vax-r
Copy link
Contributor Author

vax-r commented Oct 16, 2025

I think FillDefaults() should have:

if y.Provision.Script == nil {
    y.Provision.Script = ptr.Of("")
}

instead of having p.Script != nil && *p.Script != "" multiple times in Validate().

I would also suggest to change Probe.Script to a pointer as well, for symmetry. It is confusing to see *p.Script in the Provision code, but p.Script in the Probe code for the same thing.

@jandubois -
Thanks for your suggestion .
I'll work on that and send another PR ASAP.

Though I have a question, conventions like p.Script != nil && *p.Script != "" appears multiple places e.g.

if cfg.MountType != nil && *cfg.MountType == limatype.NINEP {

Tbh if <variable> == nil and <variable> != nil costs the same instruction but <variable> = ptr.Of("") will incur one more assignment, and this thing will still bump into the check of <variable> != "".
So basically this way will incur one more assignment. I am not sure if it's worth it ?

I would also suggest to change Probe.Script to a pointer as well, for symmetry. It is confusing to see *p.Script in the Provision code, but p.Script in the Probe code for the same thing.

I get your idea on this part, just have question for the former part.
Thanks.

@jandubois
Copy link
Member

So basically this way will incur one more assignment. I am not sure if it's worth it ?

I'm not concerned about the "cost" at all; it is trivial. I just want to simplify the code for the parts where having a value is not really optional anyways.

With the exception of the Ansible provisioning mode, not having a script will result in an error during validation. So we could just as well keep the rest of the validation code simpler.

It is also more maintenance friendly, as future changes cannot "forget" to check for nil because once the template has defaults filled in, there should be no more nils for non-optional data.

In my mind, only struct pointers should be allowed to have nil values, but pointers to string, bool, or int should be set to a pointer of their respective null value.

@vax-r
Copy link
Contributor Author

vax-r commented Oct 17, 2025

In my mind, only struct pointers should be allowed to have nil values, but pointers to string, bool, or int should be set to a pointer of their respective null value.

Thanks for your explanation ! makes sense to me, I'll ammend them up ASAP.

vax-r added a commit to vax-r/lima that referenced this pull request Oct 19, 2025
Change the type of "Script" under "type Probe struct"
from "string" to "*string". Make it consistent with
"Provision.Script".

Check lima-vm#4211 for details.

Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactoring Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants