forked from mirror/go.uuid
Moved codecs suport into `codec.go`
This commit is contained in:
parent
2558856ae9
commit
95d61fadb1
|
@ -0,0 +1,206 @@
|
|||
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
package uuid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// FromBytes returns UUID converted from raw byte slice input.
|
||||
// It will return error if the slice isn't 16 bytes long.
|
||||
func FromBytes(input []byte) (u UUID, err error) {
|
||||
err = u.UnmarshalBinary(input)
|
||||
return
|
||||
}
|
||||
|
||||
// FromBytesOrNil returns UUID converted from raw byte slice input.
|
||||
// Same behavior as FromBytes, but returns a Nil UUID on error.
|
||||
func FromBytesOrNil(input []byte) UUID {
|
||||
uuid, err := FromBytes(input)
|
||||
if err != nil {
|
||||
return Nil
|
||||
}
|
||||
return uuid
|
||||
}
|
||||
|
||||
// FromString returns UUID parsed from string input.
|
||||
// Input is expected in a form accepted by UnmarshalText.
|
||||
func FromString(input string) (u UUID, err error) {
|
||||
err = u.UnmarshalText([]byte(input))
|
||||
return
|
||||
}
|
||||
|
||||
// FromStringOrNil returns UUID parsed from string input.
|
||||
// Same behavior as FromString, but returns a Nil UUID on error.
|
||||
func FromStringOrNil(input string) UUID {
|
||||
uuid, err := FromString(input)
|
||||
if err != nil {
|
||||
return Nil
|
||||
}
|
||||
return uuid
|
||||
}
|
||||
|
||||
// MarshalText implements the encoding.TextMarshaler interface.
|
||||
// The encoding is the same as returned by String.
|
||||
func (u UUID) MarshalText() (text []byte, err error) {
|
||||
text = []byte(u.String())
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
// Following formats are supported:
|
||||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
||||
// "6ba7b8109dad11d180b400c04fd430c8"
|
||||
// ABNF for supported UUID text representation follows:
|
||||
// uuid := canonical | hashlike | braced | urn
|
||||
// plain := canonical | hashlike
|
||||
// canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
|
||||
// hashlike := 12hexoct
|
||||
// braced := '{' plain '}'
|
||||
// urn := URN ':' UUID-NID ':' plain
|
||||
// URN := 'urn'
|
||||
// UUID-NID := 'uuid'
|
||||
// 12hexoct := 6hexoct 6hexoct
|
||||
// 6hexoct := 4hexoct 2hexoct
|
||||
// 4hexoct := 2hexoct 2hexoct
|
||||
// 2hexoct := hexoct hexoct
|
||||
// hexoct := hexdig hexdig
|
||||
// hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
|
||||
// 'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
|
||||
// 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
|
||||
func (u *UUID) UnmarshalText(text []byte) (err error) {
|
||||
switch len(text) {
|
||||
case 32:
|
||||
return u.decodeHashLike(text)
|
||||
case 36:
|
||||
return u.decodeCanonical(text)
|
||||
case 38:
|
||||
return u.decodeBraced(text)
|
||||
case 41:
|
||||
fallthrough
|
||||
case 45:
|
||||
return u.decodeURN(text)
|
||||
default:
|
||||
return fmt.Errorf("uuid: incorrect UUID length: %s", text)
|
||||
}
|
||||
}
|
||||
|
||||
// decodeCanonical decodes UUID string in format
|
||||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
|
||||
func (u *UUID) decodeCanonical(t []byte) (err error) {
|
||||
if t[8] != '-' || t[13] != '-' || t[18] != '-' || t[23] != '-' {
|
||||
return fmt.Errorf("uuid: incorrect UUID format %s", t)
|
||||
}
|
||||
|
||||
src := t[:]
|
||||
dst := u[:]
|
||||
|
||||
for i, byteGroup := range byteGroups {
|
||||
if i > 0 {
|
||||
src = src[1:] // skip dash
|
||||
}
|
||||
_, err = hex.Decode(dst[:byteGroup/2], src[:byteGroup])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
src = src[byteGroup:]
|
||||
dst = dst[byteGroup/2:]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// decodeHashLike decodes UUID string in format
|
||||
// "6ba7b8109dad11d180b400c04fd430c8".
|
||||
func (u *UUID) decodeHashLike(t []byte) (err error) {
|
||||
src := t[:]
|
||||
dst := u[:]
|
||||
|
||||
if _, err = hex.Decode(dst, src); err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// decodeBraced decodes UUID string in format
|
||||
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}" or in format
|
||||
// "{6ba7b8109dad11d180b400c04fd430c8}".
|
||||
func (u *UUID) decodeBraced(t []byte) (err error) {
|
||||
l := len(t)
|
||||
|
||||
if t[0] != '{' || t[l-1] != '}' {
|
||||
return fmt.Errorf("uuid: incorrect UUID format %s", t)
|
||||
}
|
||||
|
||||
return u.decodePlain(t[1 : l-1])
|
||||
}
|
||||
|
||||
// decodeURN decodes UUID string in format
|
||||
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" or in format
|
||||
// "urn:uuid:6ba7b8109dad11d180b400c04fd430c8".
|
||||
func (u *UUID) decodeURN(t []byte) (err error) {
|
||||
total := len(t)
|
||||
|
||||
urn_uuid_prefix := t[:9]
|
||||
|
||||
if !bytes.Equal(urn_uuid_prefix, urnPrefix) {
|
||||
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
|
||||
}
|
||||
|
||||
return u.decodePlain(t[9:total])
|
||||
}
|
||||
|
||||
// decodePlain decodes UUID string in canonical format
|
||||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8" or in hash-like format
|
||||
// "6ba7b8109dad11d180b400c04fd430c8".
|
||||
func (u *UUID) decodePlain(t []byte) (err error) {
|
||||
switch len(t) {
|
||||
case 32:
|
||||
return u.decodeHashLike(t)
|
||||
case 36:
|
||||
return u.decodeCanonical(t)
|
||||
default:
|
||||
return fmt.Errorf("uuid: incorrrect UUID length: %s", t)
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||
func (u UUID) MarshalBinary() (data []byte, err error) {
|
||||
data = u.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
||||
// It will return error if the slice isn't 16 bytes long.
|
||||
func (u *UUID) UnmarshalBinary(data []byte) (err error) {
|
||||
if len(data) != Size {
|
||||
err = fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
|
||||
return
|
||||
}
|
||||
copy(u[:], data)
|
||||
|
||||
return
|
||||
}
|
|
@ -0,0 +1,255 @@
|
|||
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
package uuid
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFromBytes(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
u1, err := FromBytes(b1)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from bytes: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
b2 := []byte{}
|
||||
|
||||
_, err = FromBytes(b2)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error parsing from empty byte slice, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalBinary(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
b2, err := u.MarshalBinary()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b1, b2) {
|
||||
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalBinary(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
u1 := UUID{}
|
||||
err := u1.UnmarshalBinary(b1)
|
||||
if err != nil {
|
||||
t.Errorf("Error unmarshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
b2 := []byte{}
|
||||
u2 := UUID{}
|
||||
|
||||
err = u2.UnmarshalBinary(b2)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromString(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
||||
s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
|
||||
s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
||||
s4 := "6ba7b8109dad11d180b400c04fd430c8"
|
||||
s5 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"
|
||||
|
||||
_, err := FromString("")
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse empty string, got %s", err)
|
||||
}
|
||||
|
||||
u1, err := FromString(s1)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
u2, err := FromString(s2)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u2) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u2)
|
||||
}
|
||||
|
||||
u3, err := FromString(s3)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u3) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u3)
|
||||
}
|
||||
|
||||
u4, err := FromString(s4)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u4) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u4)
|
||||
}
|
||||
|
||||
u5, err := FromString(s5)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u5) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u5)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringShort(t *testing.T) {
|
||||
// Invalid 35-character UUID string
|
||||
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
|
||||
|
||||
for i := len(s1); i >= 0; i-- {
|
||||
_, err := FromString(s1[:i])
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse too short string, got %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringLong(t *testing.T) {
|
||||
// Invalid 37+ character UUID string
|
||||
s := []string{
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
|
||||
}
|
||||
|
||||
for _, str := range s {
|
||||
_, err := FromString(str)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse too long string, passed %s", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringInvalid(t *testing.T) {
|
||||
// Invalid UUID string formats
|
||||
s := []string{
|
||||
"6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
|
||||
"urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
"uuid:urn:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
"uuid:urn:6ba7b8109dad11d180b400c04fd430c8",
|
||||
"6ba7b8109-dad-11d1-80b4-00c04fd430c8",
|
||||
"6ba7b810-9dad1-1d1-80b4-00c04fd430c8",
|
||||
"6ba7b810-9dad-11d18-0b4-00c04fd430c8",
|
||||
"6ba7b810-9dad-11d1-80b40-0c04fd430c8",
|
||||
"6ba7b810+9dad+11d1+80b4+00c04fd430c8",
|
||||
"(6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8>",
|
||||
"zba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
"6ba7b810-9dad11d180b400c04fd430c8",
|
||||
"6ba7b8109dad-11d180b400c04fd430c8",
|
||||
"6ba7b8109dad11d1-80b400c04fd430c8",
|
||||
"6ba7b8109dad11d180b4-00c04fd430c8",
|
||||
}
|
||||
|
||||
for _, str := range s {
|
||||
_, err := FromString(str)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse invalid string, passed %s", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringOrNil(t *testing.T) {
|
||||
u := FromStringOrNil("")
|
||||
if u != Nil {
|
||||
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromBytesOrNil(t *testing.T) {
|
||||
b := []byte{}
|
||||
u := FromBytesOrNil(b)
|
||||
if u != Nil {
|
||||
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalText(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||
|
||||
b2, err := u.MarshalText()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b1, b2) {
|
||||
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalText(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||
|
||||
u1 := UUID{}
|
||||
err := u1.UnmarshalText(b1)
|
||||
if err != nil {
|
||||
t.Errorf("Error unmarshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
b2 := []byte("")
|
||||
u2 := UUID{}
|
||||
|
||||
err = u2.UnmarshalText(b2)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to unmarshal from empty string")
|
||||
}
|
||||
}
|
190
uuid.go
190
uuid.go
|
@ -27,7 +27,6 @@ package uuid
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Size of a UUID in bytes.
|
||||
|
@ -48,9 +47,6 @@ const (
|
|||
DomainOrg
|
||||
)
|
||||
|
||||
// Used in string method conversion
|
||||
const dash byte = '-'
|
||||
|
||||
// String parse helpers.
|
||||
var (
|
||||
urnPrefix = []byte("urn:uuid:")
|
||||
|
@ -107,13 +103,13 @@ func (u UUID) String() string {
|
|||
buf := make([]byte, 36)
|
||||
|
||||
hex.Encode(buf[0:8], u[0:4])
|
||||
buf[8] = dash
|
||||
buf[8] = '-'
|
||||
hex.Encode(buf[9:13], u[4:6])
|
||||
buf[13] = dash
|
||||
buf[13] = '-'
|
||||
hex.Encode(buf[14:18], u[6:8])
|
||||
buf[18] = dash
|
||||
buf[18] = '-'
|
||||
hex.Encode(buf[19:23], u[8:10])
|
||||
buf[23] = dash
|
||||
buf[23] = '-'
|
||||
hex.Encode(buf[24:], u[10:])
|
||||
|
||||
return string(buf)
|
||||
|
@ -129,184 +125,6 @@ func (u *UUID) SetVariant() {
|
|||
u[8] = (u[8] & 0xbf) | 0x80
|
||||
}
|
||||
|
||||
// MarshalText implements the encoding.TextMarshaler interface.
|
||||
// The encoding is the same as returned by String.
|
||||
func (u UUID) MarshalText() (text []byte, err error) {
|
||||
text = []byte(u.String())
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
||||
// Following formats are supported:
|
||||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
||||
// "6ba7b8109dad11d180b400c04fd430c8"
|
||||
// ABNF for supported UUID text representation follows:
|
||||
// uuid := canonical | hashlike | braced | urn
|
||||
// plain := canonical | hashlike
|
||||
// canonical := 4hexoct '-' 2hexoct '-' 2hexoct '-' 6hexoct
|
||||
// hashlike := 12hexoct
|
||||
// braced := '{' plain '}'
|
||||
// urn := URN ':' UUID-NID ':' plain
|
||||
// URN := 'urn'
|
||||
// UUID-NID := 'uuid'
|
||||
// 12hexoct := 6hexoct 6hexoct
|
||||
// 6hexoct := 4hexoct 2hexoct
|
||||
// 4hexoct := 2hexoct 2hexoct
|
||||
// 2hexoct := hexoct hexoct
|
||||
// hexoct := hexdig hexdig
|
||||
// hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
|
||||
// 'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
|
||||
// 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
|
||||
func (u *UUID) UnmarshalText(text []byte) (err error) {
|
||||
switch len(text) {
|
||||
case 32:
|
||||
return u.decodeHashLike(text)
|
||||
case 36:
|
||||
return u.decodeCanonical(text)
|
||||
case 38:
|
||||
return u.decodeBraced(text)
|
||||
case 41:
|
||||
fallthrough
|
||||
case 45:
|
||||
return u.decodeURN(text)
|
||||
default:
|
||||
return fmt.Errorf("uuid: incorrect UUID length: %s", text)
|
||||
}
|
||||
}
|
||||
|
||||
// decodeCanonical decodes UUID string in format
|
||||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8".
|
||||
func (u *UUID) decodeCanonical(t []byte) (err error) {
|
||||
if t[8] != '-' || t[13] != '-' || t[18] != '-' || t[23] != '-' {
|
||||
return fmt.Errorf("uuid: incorrect UUID format %s", t)
|
||||
}
|
||||
|
||||
src := t[:]
|
||||
dst := u[:]
|
||||
|
||||
for i, byteGroup := range byteGroups {
|
||||
if i > 0 {
|
||||
src = src[1:] // skip dash
|
||||
}
|
||||
_, err = hex.Decode(dst[:byteGroup/2], src[:byteGroup])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
src = src[byteGroup:]
|
||||
dst = dst[byteGroup/2:]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// decodeHashLike decodes UUID string in format
|
||||
// "6ba7b8109dad11d180b400c04fd430c8".
|
||||
func (u *UUID) decodeHashLike(t []byte) (err error) {
|
||||
src := t[:]
|
||||
dst := u[:]
|
||||
|
||||
if _, err = hex.Decode(dst, src); err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// decodeBraced decodes UUID string in format
|
||||
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}" or in format
|
||||
// "{6ba7b8109dad11d180b400c04fd430c8}".
|
||||
func (u *UUID) decodeBraced(t []byte) (err error) {
|
||||
l := len(t)
|
||||
|
||||
if t[0] != '{' || t[l-1] != '}' {
|
||||
return fmt.Errorf("uuid: incorrect UUID format %s", t)
|
||||
}
|
||||
|
||||
return u.decodePlain(t[1 : l-1])
|
||||
}
|
||||
|
||||
// decodeURN decodes UUID string in format
|
||||
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8" or in format
|
||||
// "urn:uuid:6ba7b8109dad11d180b400c04fd430c8".
|
||||
func (u *UUID) decodeURN(t []byte) (err error) {
|
||||
total := len(t)
|
||||
|
||||
urn_uuid_prefix := t[:9]
|
||||
|
||||
if !bytes.Equal(urn_uuid_prefix, urnPrefix) {
|
||||
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
|
||||
}
|
||||
|
||||
return u.decodePlain(t[9:total])
|
||||
}
|
||||
|
||||
// decodePlain decodes UUID string in canonical format
|
||||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8" or in hash-like format
|
||||
// "6ba7b8109dad11d180b400c04fd430c8".
|
||||
func (u *UUID) decodePlain(t []byte) (err error) {
|
||||
switch len(t) {
|
||||
case 32:
|
||||
return u.decodeHashLike(t)
|
||||
case 36:
|
||||
return u.decodeCanonical(t)
|
||||
default:
|
||||
return fmt.Errorf("uuid: incorrrect UUID length: %s", t)
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||
func (u UUID) MarshalBinary() (data []byte, err error) {
|
||||
data = u.Bytes()
|
||||
return
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
||||
// It will return error if the slice isn't 16 bytes long.
|
||||
func (u *UUID) UnmarshalBinary(data []byte) (err error) {
|
||||
if len(data) != Size {
|
||||
err = fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
|
||||
return
|
||||
}
|
||||
copy(u[:], data)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FromBytes returns UUID converted from raw byte slice input.
|
||||
// It will return error if the slice isn't 16 bytes long.
|
||||
func FromBytes(input []byte) (u UUID, err error) {
|
||||
err = u.UnmarshalBinary(input)
|
||||
return
|
||||
}
|
||||
|
||||
// FromBytesOrNil returns UUID converted from raw byte slice input.
|
||||
// Same behavior as FromBytes, but returns a Nil UUID on error.
|
||||
func FromBytesOrNil(input []byte) UUID {
|
||||
uuid, err := FromBytes(input)
|
||||
if err != nil {
|
||||
return Nil
|
||||
}
|
||||
return uuid
|
||||
}
|
||||
|
||||
// FromString returns UUID parsed from string input.
|
||||
// Input is expected in a form accepted by UnmarshalText.
|
||||
func FromString(input string) (u UUID, err error) {
|
||||
err = u.UnmarshalText([]byte(input))
|
||||
return
|
||||
}
|
||||
|
||||
// FromStringOrNil returns UUID parsed from string input.
|
||||
// Same behavior as FromString, but returns a Nil UUID on error.
|
||||
func FromStringOrNil(input string) UUID {
|
||||
uuid, err := FromString(input)
|
||||
if err != nil {
|
||||
return Nil
|
||||
}
|
||||
return uuid
|
||||
}
|
||||
|
||||
// Must is a helper that wraps a call to a function returning (UUID, error)
|
||||
// and panics if the error is non-nil. It is intended for use in variable
|
||||
// initializations such as
|
||||
|
|
228
uuid_test.go
228
uuid_test.go
|
@ -103,231 +103,3 @@ func TestSetVariant(t *testing.T) {
|
|||
t.Errorf("Incorrect variant for UUID after u.setVariant(): %d", u.Variant())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromBytes(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
u1, err := FromBytes(b1)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from bytes: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
b2 := []byte{}
|
||||
|
||||
_, err = FromBytes(b2)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error parsing from empty byte slice, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalBinary(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
b2, err := u.MarshalBinary()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b1, b2) {
|
||||
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalBinary(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
u1 := UUID{}
|
||||
err := u1.UnmarshalBinary(b1)
|
||||
if err != nil {
|
||||
t.Errorf("Error unmarshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
b2 := []byte{}
|
||||
u2 := UUID{}
|
||||
|
||||
err = u2.UnmarshalBinary(b2)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromString(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
|
||||
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
||||
s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
|
||||
s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
||||
s4 := "6ba7b8109dad11d180b400c04fd430c8"
|
||||
s5 := "urn:uuid:6ba7b8109dad11d180b400c04fd430c8"
|
||||
|
||||
_, err := FromString("")
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse empty string, got %s", err)
|
||||
}
|
||||
|
||||
u1, err := FromString(s1)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
u2, err := FromString(s2)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u2) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u2)
|
||||
}
|
||||
|
||||
u3, err := FromString(s3)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u3) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u3)
|
||||
}
|
||||
|
||||
u4, err := FromString(s4)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u4) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u4)
|
||||
}
|
||||
|
||||
u5, err := FromString(s5)
|
||||
if err != nil {
|
||||
t.Errorf("Error parsing UUID from string: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u5) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u5)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringShort(t *testing.T) {
|
||||
// Invalid 35-character UUID string
|
||||
s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
|
||||
|
||||
for i := len(s1); i >= 0; i-- {
|
||||
_, err := FromString(s1[:i])
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse too short string, got %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringLong(t *testing.T) {
|
||||
// Invalid 37+ character UUID string
|
||||
s := []string{
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
|
||||
"6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
|
||||
}
|
||||
|
||||
for _, str := range s {
|
||||
_, err := FromString(str)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse too long string, passed %s", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringInvalid(t *testing.T) {
|
||||
// Invalid UUID string formats
|
||||
s := []string{
|
||||
"6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
|
||||
"urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
"uuid:urn:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
"uuid:urn:6ba7b8109dad11d180b400c04fd430c8",
|
||||
"6ba7b8109-dad-11d1-80b4-00c04fd430c8",
|
||||
"6ba7b810-9dad1-1d1-80b4-00c04fd430c8",
|
||||
"6ba7b810-9dad-11d18-0b4-00c04fd430c8",
|
||||
"6ba7b810-9dad-11d1-80b40-0c04fd430c8",
|
||||
"6ba7b810+9dad+11d1+80b4+00c04fd430c8",
|
||||
"(6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
|
||||
"{6ba7b810-9dad-11d1-80b4-00c04fd430c8>",
|
||||
"zba7b810-9dad-11d1-80b4-00c04fd430c8",
|
||||
"6ba7b810-9dad11d180b400c04fd430c8",
|
||||
"6ba7b8109dad-11d180b400c04fd430c8",
|
||||
"6ba7b8109dad11d1-80b400c04fd430c8",
|
||||
"6ba7b8109dad11d180b4-00c04fd430c8",
|
||||
}
|
||||
|
||||
for _, str := range s {
|
||||
_, err := FromString(str)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to parse invalid string, passed %s", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromStringOrNil(t *testing.T) {
|
||||
u := FromStringOrNil("")
|
||||
if u != Nil {
|
||||
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFromBytesOrNil(t *testing.T) {
|
||||
b := []byte{}
|
||||
u := FromBytesOrNil(b)
|
||||
if u != Nil {
|
||||
t.Errorf("Should return Nil UUID on parse failure, got %s", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshalText(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||
|
||||
b2, err := u.MarshalText()
|
||||
if err != nil {
|
||||
t.Errorf("Error marshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(b1, b2) {
|
||||
t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalText(t *testing.T) {
|
||||
u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
|
||||
b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||
|
||||
u1 := UUID{}
|
||||
err := u1.UnmarshalText(b1)
|
||||
if err != nil {
|
||||
t.Errorf("Error unmarshaling UUID: %s", err)
|
||||
}
|
||||
|
||||
if !Equal(u, u1) {
|
||||
t.Errorf("UUIDs should be equal: %s and %s", u, u1)
|
||||
}
|
||||
|
||||
b2 := []byte("")
|
||||
u2 := UUID{}
|
||||
|
||||
err = u2.UnmarshalText(b2)
|
||||
if err == nil {
|
||||
t.Errorf("Should return error trying to unmarshal from empty string")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue