Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
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
48 changes: 48 additions & 0 deletions ids/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

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

type idForFormatting interface {
String() string
Hex() string
}

// format implements the [fmt.Formatter] interface for [ID] and [ShortID].
func format[T interface {
idForFormatting
ID | ShortID
Comment on lines +25 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we constrain the types to be either ID or ShortID?

}](s fmt.State, verb rune, id T) {
switch verb {
case 'x':
if s.Flag('#') {
s.Write([]byte("0x")) //nolint:errcheck // [fmt.Formatter] doesn't allow for returning errors, and the implementation of [fmt.State] always returns nil on Write()
}
s.Write([]byte(id.Hex())) //nolint:errcheck // See above

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

default:
s.Write([]byte(id.String())) //nolint:errcheck // See above
}
}
55 changes: 55 additions & 0 deletions ids/format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package ids

import (
"fmt"
"testing"
)

func TestFormat(t *testing.T) {
type test struct {
id idForFormatting
want map[string]string // format -> output
}
makeTestCase := func(id idForFormatting) 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.

//nolint:forbidigo // require.Equal() is inappropriate as it unnecessarily stops future tests and `assert.Equal()` isn't allowed
t.Errorf("fmt.Sprintf(%q, %T) got %q; want %q", format, tt.id, got, want)
}
}
})
}
}

func BenchmarkFormat(*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{})
}