Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Implement fmt.Formatter #72
Implement fmt.Formatter #72
Changes from 1 commit
61107f3
8817e46
0af1998
8b125a7
094e8c2
5d8c397
90065f9
5f199e6
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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'm not convinced about this approach.
When
UUID
does not implementfmt.Formatter
, this program fails thego vet
check, and produces obviously wrong output: https://play.golang.org/p/stts25e3_2aHowever, when
UUID
does implementfmt.Formatter
, the program passesgo vet
, and produces more plausible looking (but wrong) output: https://play.golang.org/p/hNOR123ZjkUThe behavior in the second case seems dangerous to me. Using the wrong formatting verb is a programmer error and this implementation of
fmt.Formatter
hides it fromgo vet
, and swallows it when it produces output. I don't know what a good solution to this problem might be, short of trying to re-create the original format string from thefmt.State
and callingfmt.Sprintf
with a[16]byte
value instead of aUUID
, to avoid recursion.For reference,
pkg/errors
implementsfmt.Formatter
on some of its types, and when it doesn't understand a verb, it simply produces no output, e.g. https://github.com/pkg/errors/blob/master/stack.go#L64-L84. I'm not a huge fan of this approach either, but it is at least a data point we have, if we're going to implementfmt.Formatter
at all.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.
There doesn't seem to be any way to indicate that a formatting verb is invalid/not supported, so it feels like "do nothing" is the only option. I'm okay with that path for all of the formatting verbs that don't apply: %b, %c, %d, %o, %U, %e, %E, %f, %F, %g, %G.
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.
To clarify, "do nothing" means "emit no output"?
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.
Correct, "do nothing" would be to return an empty string for all of those verbs.
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.
Okay. I guess that's reasonable, and there is a precedent for it too. But my concern with respect to the fact that this conceals programmer errors such as using the wrong flags remains.
The
fmt.Formatter
implementation is opaque to static analysis tools likego vet
. We're gaining convenience and a nice API for printing UUIDs as hexadecimal strings, but we are losing precision under the lens of static analysis tools. We should decide if this trade-off is worth making.