Skip to content

Commit

Permalink
When parsing a 38-char UUID, require '{' and '}' characters
Browse files Browse the repository at this point in the history
It was not intentional to simply ignore these characters, and
examples/docs/tests all indicate that curly braces were the intended
characters. I've added some test cases to ensure things work.

Fixes google#60
  • Loading branch information
Carrotman42 committed May 19, 2020
1 parent cb32006 commit 6d1eb31
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func Parse(s string) (UUID, error) {

// {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
case 36 + 2:
if s[0] != '{' || s[37] != '}' {
return uuid, fmt.Errorf("invalid prefix/suffix: got %q and %q, want '{' and '}'", s[0], s[37])
}
s = s[1:]

// xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Expand Down Expand Up @@ -101,6 +104,9 @@ func ParseBytes(b []byte) (UUID, error) {
}
b = b[9:]
case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
if b[0] != '{' || b[37] != '}' {
return uuid, fmt.Errorf("invalid prefix/suffix: got %q and %q, want '{' and '}'", b[0], b[37])
}
b = b[1:]
case 32: // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
var ok bool
Expand Down
3 changes: 3 additions & 0 deletions uuid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ var tests = []test{
{"g47ac10b-58cc-4372-a567-0e02b2c3d479", 0, Invalid, false},

{"{f47ac10b-58cc-0372-8567-0e02b2c3d479}", 0, RFC4122, true},
{"[f47ac10b-58cc-0372-8567-0e02b2c3d479]", 0, Invalid, false},
{"{f47ac10b-58cc-0372-8567-0e02b2c3d479]", 0, Invalid, false},
{"[f47ac10b-58cc-0372-8567-0e02b2c3d479}", 0, Invalid, false},
{"{f47ac10b-58cc-0372-8567-0e02b2c3d479", 0, Invalid, false},
{"f47ac10b-58cc-0372-8567-0e02b2c3d479}", 0, Invalid, false},

Expand Down

0 comments on commit 6d1eb31

Please sign in to comment.