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

imagefilter: add SupportedOutputFormats #1168

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

schuellerf
Copy link
Contributor

@schuellerf schuellerf commented Jan 24, 2025

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

@schuellerf schuellerf requested a review from a team as a code owner January 24, 2025 16:15
@schuellerf schuellerf force-pushed the add-GetSupportedOutputFormats branch 3 times, most recently from 19a040d to 66d1982 Compare January 24, 2025 16:16
@schuellerf schuellerf marked this pull request as draft January 24, 2025 16:18
@schuellerf schuellerf force-pushed the add-GetSupportedOutputFormats branch from 66d1982 to ddda18f Compare January 24, 2025 16:33
Copy link
Contributor

@mvo5 mvo5 left a 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.

pkg/imagefilter/formatter.go Outdated Show resolved Hide resolved
pkg/imagefilter/formatter.go Outdated Show resolved Hide resolved
Copy link
Contributor

@mvo5 mvo5 left a comment

Choose a reason for hiding this comment

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

Quick drive-by:

  1. the PR description should follow the convention: module: change (e.g. imagefilter: add SupportedOutputFormats)
  2. 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.

@schuellerf schuellerf changed the title Add get supported output formats imagefilter: add SupportedOutputFormats Jan 27, 2025
@schuellerf schuellerf force-pushed the add-GetSupportedOutputFormats branch 2 times, most recently from 392df90 to 3125747 Compare January 28, 2025 08:53
@schuellerf schuellerf marked this pull request as ready for review January 28, 2025 08:53
@schuellerf schuellerf requested a review from mvo5 January 28, 2025 08:53
@schuellerf schuellerf force-pushed the add-GetSupportedOutputFormats branch from 3125747 to a7a1dc6 Compare January 28, 2025 09:02
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.
@schuellerf schuellerf force-pushed the add-GetSupportedOutputFormats branch from a7a1dc6 to 192ae46 Compare January 29, 2025 11:15
@@ -1,4 +1,4 @@
package imagefilter_test
package imagefilter
Copy link
Contributor

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.

Copy link
Contributor

@mvo5 mvo5 Jan 29, 2025

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{
Copy link
Member

@supakeen supakeen Jan 29, 2025

Choose a reason for hiding this comment

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

Two notes:

  1. We normally don't use Hungarian notation; e.g. writing the type (Map) in the variable name and personally I don't like it.
  2. 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)
Copy link
Member

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?

Copy link
Contributor

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) {
Copy link
Member

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 :)

Copy link
Member

@supakeen supakeen left a 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.

@supakeen
Copy link
Member

the PR description should follow the convention: module: change (e.g. imagefilter: add SupportedOutputFormats)

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants