-
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 all commits
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,48 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
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) | ||
} | ||
|
||
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
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 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 | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package ids | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
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 { | ||
require.Equalf(t, want, fmt.Sprintf(format, tt.id), "fmt.Sprintf(%q, %T)", format, tt.id) | ||
} | ||
}) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.