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

helper/validation: Prevented panics with ToDiagFunc() function when used inside Schema type Elem field #915

Merged
merged 2 commits into from Mar 28, 2022

Conversation

bflad
Copy link
Member

@bflad bflad commented Mar 23, 2022

Closes #734

… used inside `Schema` type `Elem` field

Reference: #734
@bflad bflad added bug Something isn't working subsystem/diagnostics Issues and feature requests related to the diagnostics returned to core. labels Mar 23, 2022
@bflad bflad added this to the v2.13.0 milestone Mar 23, 2022
@bflad bflad requested a review from a team as a code owner March 23, 2022 20:24
Copy link
Contributor

@detro detro left a comment

Choose a reason for hiding this comment

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

It's great to see this bug (that I hit 2 weeks ago) getting fixed.
I left a question that is more for my learning than anything.

ws, es := validator(i, attr.Name)
// A practitioner-friendly key for any SchemaValidateFunc output.
// Generally this should be the last attribute name on the path.
// If not found for some unexpected reason, an empty string is fine
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you provide a bit of details about why it wouldn't not be, in some cases, that the last element in the path is not the one that implements GetAttrStep?
And if it's not, then what are those? IndexStep?

Copy link
Contributor

Choose a reason for hiding this comment

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

FYI I dug a bit into go-cty to try to understand this a bit: I do get that those *Step structs are representation of operations that can be done on a Path, but I don't fully follow how they get assembled and why we would hit a scenario like the one described in the comment you added.

Copy link
Member Author

Choose a reason for hiding this comment

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

why it wouldn't not be, in some cases, that the last element in the path is not the one that implements GetAttrStep?

An IndexStep in cty represents a path inside a list, map, or set for one of the elements. For example, the path to the first element in a list is 0.

I don't fully follow how they get assembled

Paths are assembled as part of decoding a value. The core folks would be best to ask how this works in terms of how a configuration/schema gets decoded to certain types and paths.

why we would hit a scenario like the one described in the comment you added

If a new path type is added to cty. Just coding for future safety because the runtime code for providers should never introduce an unnecessary panic.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried out the forcetypeassert linter in golangci-lint and it catches some other potentially problematic code, but unfortunately doesn't catch this particular code pattern of a type assertion on a slice element. Not sure its worth the effort of enabling it in this project, but maybe some of the others.

Copy link
Contributor

Choose a reason for hiding this comment

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

Big 👍 from me on adding this linter everywhere.

@bflad bflad merged commit a0d2e21 into main Mar 28, 2022
@bflad bflad deleted the bflad-helper-validation-ToDiagFunc branch March 28, 2022 14:06
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working subsystem/diagnostics Issues and feature requests related to the diagnostics returned to core.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ToDiagFunc() panics when used on a list element
2 participants