-
Notifications
You must be signed in to change notification settings - Fork 54
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
imagefilter: add SupportedOutputFormats #1168
base: main
Are you sure you want to change the base?
Conversation
19a040d
to
66d1982
Compare
66d1982
to
ddda18f
Compare
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.
Thank you! I like the idea of having the SupportedOutputFormats() helper. Some suggestions for the implementation inline.
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.
Quick drive-by:
- the PR description should follow the convention:
module: change
(e.g. imagefilter: add SupportedOutputFormats) - Rebasing this on top of main and dropping the diff for the "short" output would most likely get this merged much quicker as I feel "short" output will need some more design/discussion but this here is very nice and straightforward.
392df90
to
3125747
Compare
3125747
to
a7a1dc6
Compare
Renaming package to enable importing of internal variables. Needed for following implementations and their tests.
Implements function to get a list of all supported output formats.
Add a test for the fail-case.
a7a1dc6
to
192ae46
Compare
@@ -1,4 +1,4 @@ | |||
package imagefilter_test | |||
package imagefilter |
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.
You can drop the rename commit, the export_test.go
change in the next commit should do what you need and the package <pkg>_test
is a common (and deliberate) pattern used here.
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.
Sorry, I realize this was a rather terse comment. Please just drop the first commit (and I personally would just squash 2+3 [edit: sorry, totally fine to keep]) and then with this diff:
diff --git a/pkg/imagefilter/formatter_test.go b/pkg/imagefilter/formatter_test.go
index 2a4f2ab8c..5bc7e105f 100644
--- a/pkg/imagefilter/formatter_test.go
+++ b/pkg/imagefilter/formatter_test.go
@@ -107,8 +107,8 @@ func TestResultsFormatter(t *testing.T) {
}
func TestSupportedOutputFormats(t *testing.T) {
- formatters := SupportedOutputFormats()
- assert.Len(t, formatters, len(supportedFormatterMap))
+ formatters := imagefilter.SupportedOutputFormats()
+ assert.Len(t, formatters, len(imagefilter.SupportedFormatterMap))
assert.Contains(t, formatters, "text")
assert.Contains(t, formatters, "json")
assert.Contains(t, formatters, "shell")
@@ -116,7 +116,7 @@ func TestSupportedOutputFormats(t *testing.T) {
}
func TestResultsFormatterError(t *testing.T) {
- fmter, err := NewResultsFormatter(OutputFormat("unsupported"))
+ fmter, err := imagefilter.NewResultsFormatter(imagefilter.OutputFormat("unsupported"))
assert.Error(t, err)
assert.Nil(t, fmter)
}
things should be fine.
@@ -27,20 +29,28 @@ type ResultsFormatter interface { | |||
Output(io.Writer, []Result) error | |||
} | |||
|
|||
// NewResultFormatter will create a formatter based on the given format. | |||
var supportedFormatterMap = map[string]ResultsFormatter{ |
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.
Two notes:
- We normally don't use Hungarian notation; e.g. writing the type (
Map
) in the variable name and personally I don't like it. - The behavior here is slightly different because all key lookups now return the same instance of a formatter. Since formatters don't keep state (right now) this might be OK, just wanted to write it down as it's a behavioral change I think?
// SupportedOutputFormats returns a list of supported output formats | ||
func SupportedOutputFormats() []string { | ||
keys := maps.Keys(supportedFormatterMap) | ||
sort.Strings(keys) |
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.
Just a question; but maps.Keys
returns an iterator. Which part of this function turns that into a slice, is it the return
or the sort.Strings
?
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.
This is a bit of a funny one, the new "maps" package in 1.23 does return an iterator but the compat package golang.org/x/exp/maps (that we need to use as we are not on 1.23 yet) just returns a []string slice. We can keep the compat maps until it gets removed and then we either port to iterators or just implement our own .Keys() (its trivial enough).
require.NoError(t, err) | ||
err = fmter.Output(&buf, res) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expectsOutput, buf.String(), tc) | ||
} | ||
} | ||
|
||
func TestSupportedOutputFormats(t *testing.T) { |
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 think we should test the sorted-ness here as well as it seems like an important feature of the SupportedOutputFormats
function :)
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.
Some minor questions and bits.
I noticed the commits are now prefixed with the full package path instead of the module name. I don't know how I feel about this :) |
Convenience function for concise help output
(This is based on #1166 and #1167)
The last commit could be split off, it's not directly related to the implementation of
SupportedOutputFormats
just extends the tests