Skip to content
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

Use a custom error type for invalid lengths, replacing fmt.Errorf #69

Merged
merged 2 commits into from Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions uuid.go
Expand Up @@ -35,6 +35,12 @@ const (

var rander = rand.Reader // random function

type invalidLengthError struct{ len int }

func (err *invalidLengthError) Error() string {
return fmt.Sprintf("invalid UUID length: %d", err.len)
}

// Parse decodes s into a UUID or returns an error. Both the standard UUID
// forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the
Expand Down Expand Up @@ -68,7 +74,7 @@ func Parse(s string) (UUID, error) {
}
return uuid, nil
default:
return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
return uuid, &invalidLengthError{len(s)}
}
// s is now at least 36 bytes long
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Expand Down Expand Up @@ -112,7 +118,7 @@ func ParseBytes(b []byte) (UUID, error) {
}
return uuid, nil
default:
return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
return uuid, &invalidLengthError{len(b)}
}
// s is now at least 36 bytes long
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Expand Down
39 changes: 39 additions & 0 deletions uuid_test.go
Expand Up @@ -517,6 +517,15 @@ func TestRandomFromReader(t *testing.T) {
}
}

func TestWrongLength(t *testing.T) {
_, err := Parse("12345")
if err == nil {
t.Errorf("expected ‘12345’ was invalid")
} else if err.Error() != "invalid UUID length: 5" {
t.Errorf("expected a different error message for an invalid length")
}
}

var asString = "f47ac10b-58cc-0372-8567-0e02b2c3d479"
var asBytes = []byte(asString)

Expand Down Expand Up @@ -595,3 +604,33 @@ func BenchmarkUUID_URN(b *testing.B) {
}
}
}

func BenchmarkParseBadLength(b *testing.B) {
short := asString[:10]
for i := 0; i < b.N; i++ {
_, err := Parse(short)
if err == nil {
b.Fatalf("expected ‘%s’ was invalid", short)
}
}
}

func BenchmarkParseLen32Truncated(b *testing.B) {
partial := asString[:len(asString)-4]
for i := 0; i < b.N; i++ {
_, err := Parse(partial)
if err == nil {
b.Fatalf("expected ‘%s’ was invalid", partial)
}
}
}

func BenchmarkParseLen36Corrupted(b *testing.B) {
wrong := asString[:len(asString)-1] + "x"
for i := 0; i < b.N; i++ {
_, err := Parse(wrong)
if err == nil {
b.Fatalf("expected ‘%s’ was invalid", wrong)
}
}
}