Skip to content

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions ids/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package ids
Copy link
Contributor

Choose a reason for hiding this comment

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

why not put this in id.go ? it's only 40 LOC and the other formatting stuff are there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would also need to be in short.go and then the common implementation would be separated from at least one use case. Here they naturally stay together and provide the reader with context.


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

Check failure on line 25 in ids/format.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `s.Write` is not checked (errcheck)
}
s.Write([]byte(id.Hex()))

Check failure on line 27 in ids/format.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `s.Write` is not checked (errcheck)

case 'q':
str := id.String()
buf := make([]byte, len(str)+2)
buf[0] = '"'
buf[len(buf)-1] = '"'
copy(buf[1:], str)
s.Write(buf)

Check failure on line 35 in ids/format.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `s.Write` is not checked (errcheck)

default:
s.Write([]byte(id.String()))

Check failure on line 38 in ids/format.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `s.Write` is not checked (errcheck)
}
}
57 changes: 57 additions & 0 deletions ids/format_test.go
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
}
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In an EVM implementation ids.ID is typically derived from a common.Hash (both [32]byte), which is idiomatically rendered as hex. My errors that include a block hash/ID use %#x.

t.Errorf("fmt.Sprintf(%q, %T) got %q; want %q", format, tt.id, got, want)

Check failure on line 45 in ids/format_test.go

View workflow job for this annotation

GitHub Actions / Lint

use of `t.Errorf` forbidden because "the require library should be used instead" (forbidigo)
}
}
})
}
}

func BenchmarkFormat(b *testing.B) {

Check failure on line 52 in ids/format_test.go

View workflow job for this annotation

GitHub Actions / Lint

unused-parameter: parameter 'b' seems to be unused, consider removing or renaming it as _ (revive)
// %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{})
}
Loading