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 all 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
}
}
47 changes: 47 additions & 0 deletions ids/format_test.go
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)
}
})
}
}