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

Support braced hashlike on FromString as documented. #14

Merged
merged 2 commits into from
Jul 14, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,11 @@ func (u *UUID) UnmarshalText(text []byte) (err error) {
switch len(text) {
case 32:
return u.decodeHashLike(text)
case 34, 38:
return u.decodeBraced(text)
case 36:
return u.decodeCanonical(text)
case 38:
return u.decodeBraced(text)
case 41:
fallthrough
case 45:
case 41, 45:
return u.decodeURN(text)
default:
return fmt.Errorf("uuid: incorrect UUID length: %s", text)
Expand Down
19 changes: 18 additions & 1 deletion codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (s *codecTestSuite) TestFromString(c *C) {
s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
s4 := "6ba7b8109dad11d180b400c04fd430c8"
s5 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"
s5 := "{6ba7b8109dad11d180b400c04fd430c8}"
s6 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"

_, err := FromString("")
c.Assert(err, NotNil)
Expand All @@ -114,6 +115,10 @@ func (s *codecTestSuite) TestFromString(c *C) {
u5, err := FromString(s5)
c.Assert(err, IsNil)
c.Assert(u5, Equals, u)

u6, err := FromString(s6)
c.Assert(err, IsNil)
c.Assert(u6, Equals, u)
}

func (s *codecTestSuite) BenchmarkFromString(c *C) {
Expand Down Expand Up @@ -190,14 +195,26 @@ func (s *codecTestSuite) TestFromStringInvalid(c *C) {
}

func (s *codecTestSuite) TestFromStringOrNil(c *C) {
expect := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}

u := FromStringOrNil("")
c.Assert(u, Equals, Nil)

s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
u = FromStringOrNil(s1)
c.Assert(u, Equals, expect)
}

func (s *codecTestSuite) TestFromBytesOrNil(c *C) {
expect := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}

b := []byte{}
u := FromBytesOrNil(b)
c.Assert(u, Equals, Nil)

b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
u = FromBytesOrNil(b1)
c.Assert(u, Equals, expect)
}

func (s *codecTestSuite) TestMarshalText(c *C) {
Expand Down