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

Add convenience functions for ErrorsCount, WarningsCount, Errors andWarnings #392

Merged
merged 4 commits into from
Jun 27, 2022

Conversation

bendbennett
Copy link
Contributor

Closes: #391

Adds the following convenience functions to Diagnostics:

  • ErrorsCount()
  • WarningsCount()
  • Errors()
  • Warnings()

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.

As said verbally, the only thing missing is a CHANGELOG entry and I left a nitpick/suggestion regarding the count methods.

🚀

Comment on lines 75 to 88
func (diags Diagnostics) ErrorsCount() int {
count := 0

for _, d := range diags {
if SeverityError == d.Severity() {
count++
}
}

return count
}

// WarningsCount returns the number of Diagnostic in Diagnostics that are SeverityWarning.
func (diags Diagnostics) WarningsCount() int {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: both those *Count methods could be implemented as a len(Call_to_non_Count_methods).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Fixed.

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.

🚀

Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Code-wise I think this looks good, although I'd love to see/hear about expected use cases before expanding the public API here.

@@ -0,0 +1,3 @@
```release-note:feature
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Typically features are "big" features whereas enhancements cover everything else

Suggested change
```release-note:feature
```release-note:enhancement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

expected diag.Diagnostics
}
tests := map[string]testCase{
"errors": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Might be good to check what happens when:

  • There is a nil diag.Diagnostics, it should return nil or empty diag.Diagnostics
  • There is an empty diag.Diagnostics, it should return empty diag.Diagnostics
  • There are only warning diagnostics, it should return empty diag.Diagnostics

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added additional test coverage.

expected diag.Diagnostics
}
tests := map[string]testCase{
"errors": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Might be good to check what happens when:

  • There is a nil diag.Diagnostics, it should return nil or empty diag.Diagnostics
  • There is an empty diag.Diagnostics, it should return empty diag.Diagnostics
  • There are only error diagnostics, it should return empty diag.Diagnostics

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added additional test coverage.

expected int
}
tests := map[string]testCase{
"errors": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Similar to the Errors testing, I think we should cover some additional cases just to ensure it returns 0 instead of panics, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added additional test coverage.

expected int
}
tests := map[string]testCase{
"errors": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Similar to the Warning testing, I think we should cover some additional cases just to ensure it returns 0 instead of panics, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added additional test coverage.

@bendbennett
Copy link
Contributor Author

Code-wise I think this looks good, although I'd love to see/hear about expected use cases before expanding the public API here.

One use-case is in the instance where we're running Any() with All() validation and we want to be able to return all warnings. For instance https://github.com/hashicorp/terraform-plugin-framework-validators/pull/43/files#r907476258

@detro
Copy link
Contributor

detro commented Jun 27, 2022

To add to the "use cases": it's useful when writing tests to count the expected errors, instead of just checking HasErrors().

@bendbennett bendbennett requested a review from bflad June 27, 2022 16:30
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

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

Looks good to me 🚀

@bendbennett bendbennett merged commit 5a338a7 into main Jun 27, 2022
@bendbennett bendbennett deleted the bendbennett/issues-391 branch June 27, 2022 17:45
@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 Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add convenience functions for diagnostics ErrorsCount, WarningsCount, Errors and Warnings
3 participants