-
Notifications
You must be signed in to change notification settings - Fork 771
fix(ids): %x
formatting of ID
and ShortID
#3956
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
4be5415
7a734ad
0d49c50
70de15a
e6a7c33
6489323
5e841fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package ids | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not put this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would also need to be in |
||
|
||
import "fmt" | ||
|
||
var _ = []fmt.Formatter{ID{}, ShortID{}} | ||
|
||
// Format implements the [fmt.Formatter] interface. | ||
func (id ID) Format(s fmt.State, verb rune) { | ||
format(s, verb, id) | ||
} | ||
|
||
// Format implements the [fmt.Formatter] interface. | ||
func (id ShortID) Format(s fmt.State, verb rune) { | ||
format(s, verb, id) | ||
} | ||
|
||
// format implements the [fmt.Formatter] interface for [ID] and [ShortID]. | ||
func format[T interface { | ||
String() string | ||
Hex() string | ||
}](s fmt.State, verb rune, id T) { | ||
switch verb { | ||
case 'x': | ||
if s.Flag('#') { | ||
s.Write([]byte("0x")) | ||
} | ||
s.Write([]byte(id.Hex())) | ||
|
||
case 'q': | ||
str := id.String() | ||
buf := make([]byte, len(str)+2) | ||
buf[0] = '"' | ||
buf[len(buf)-1] = '"' | ||
copy(buf[1:], str) | ||
s.Write(buf) | ||
|
||
default: | ||
s.Write([]byte(id.String())) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ids | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestFormat(t *testing.T) { | ||
type ( | ||
idInterface interface { | ||
String() string | ||
Hex() string | ||
} | ||
ARR4N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test struct { | ||
id idInterface | ||
want map[string]string // format -> output | ||
} | ||
) | ||
makeTestCase := func(id idInterface) test { | ||
return test{ | ||
id: id, | ||
want: map[string]string{ | ||
"%v": id.String(), | ||
"%s": id.String(), | ||
"%q": `"` + id.String() + `"`, | ||
"%x": id.Hex(), | ||
"%#x": `0x` + id.Hex(), | ||
}, | ||
} | ||
} | ||
|
||
tests := []test{ | ||
makeTestCase(ID{}), | ||
makeTestCase(GenerateTestID()), | ||
makeTestCase(GenerateTestID()), | ||
makeTestCase(ShortID{}), | ||
makeTestCase(GenerateTestShortID()), | ||
makeTestCase(GenerateTestShortID()), | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.id.String(), func(t *testing.T) { | ||
for format, want := range tt.want { | ||
if got := fmt.Sprintf(format, tt.id); got != want { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious to know the use case for this PR. We don't pass ID into Sprintf but use the logger, and there we use zap.Stringer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In an EVM implementation |
||
t.Errorf("fmt.Sprintf(%q, %T) got %q; want %q", format, tt.id, got, want) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkFormat(b *testing.B) { | ||
// %q uses a []byte so this is just to demonstrate that it's on the stack | ||
// otherwise someone, not naming any names, might want to "fix" it. | ||
_ = fmt.Sprintf("%q", ID{}) | ||
_ = fmt.Sprintf("%q", ShortID{}) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.