-
Notifications
You must be signed in to change notification settings - Fork 288
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
tiltfile: make probe types play nicer with None #4163
Conversation
This seems very complex. And I don't want to have to write custom logic for every type for handling None Have you considered having Probe implement the Unpacker interface and handling None there? Alternatively, have you considered filing a bug upstream to have starlark's UnpackArgs treat "None" the same as unspecified for optional args? Alan might be open to it. |
I agree it's still a lot of boilerplate -- I'd actually stashed this approach, but the simplified version missed out on explicit handling of In terms of In theory, Starlark should probably behave closer to In the interim, I can eliminate the |
not totally sure i understand what you're saying. you think it would be harder to do it with Unpacker? eh, i've worked with the starlark folks a bunch, i can send them a Q about it |
filed as google/starlark-go#347 |
Let me play around with |
FWIW Here's what it looks like with a per-type That could be a reasonable interim until changes are made in Starlark to address the immediate ergonomics issues...let me know what you think and I can rebase this PR or just close it out. |
ya that seems reasonable. The starlark folks also seem open to accepting a PR to make the '??' suffix "None means absent", if you want to take a crack at that. |
a9bf7be
to
e0a7f34
Compare
Add `Unpack()` implementations that check for `starlark.None` first and then attempt to cast to the correct type.
e0a7f34
to
1048231
Compare
Okay, rebased this PR to just use explicit Only unavoidable downside is that for using structs within other structs, we'll always need to take some extra care, as |
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.
Ya, i'd love to get to a place where a lot of the starlark.Struct -> Value -> Go struct management could be autogenerated, or reflection-based, but that's part of what the goal of all this extensibility/apiserver stuff is.
There's often a lot of boilerplate involved for optional object arguments
that can be
None
or an actual value; it's necessary to use astarlark.Value
, check to see if it'snil
(kwarg not specified)or
starlark.None
(kwarg specified) or present and then make surethe expected type constraint is met.
This is exacerbated bystarlarkstruct.Struct
, which does not playnice with Go nil by design, and when you've got a struct with embedded
structs that can be
None
, this results in a bunch of extra nil/None/typechecking on top of the above.
Adding anOptional
type helps abstract some of this and providestype-checking and a couple convenience methods depending on whether
you're interoping with Go code (
ValueOrNil()
) or Starlark types(
ValueOrNone()
). There's still some type-casting, unfortunately,but it makes things safer and cuts down on boilerplate code
substantially.
This adds
Unpack()
implementations that check forstarlark.None
firstand then attempt to cast to the correct type.
Closes #4160.