/
codec.go
178 lines (160 loc) · 5.01 KB
/
codec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
// Copyright (C) 2013-2018 by Maxim Bublis <b@codemonkey.ru>
// Use of this source code is governed by a MIT-style
// license that can be found in licenses/MIT-gofrs.txt.
// This code originated in github.com/gofrs/uuid.
package uuid
import (
"bytes"
"encoding/hex"
"fmt"
)
// FromBytes returns a UUID generated from the raw byte slice input.
// It will return an error if the slice isn't 16 bytes long.
func FromBytes(input []byte) (UUID, error) {
u := UUID{}
err := u.UnmarshalBinary(input)
return u, err
}
// FromBytesOrNil returns a UUID generated from the raw byte slice input.
// Same behavior as FromBytes(), but returns uuid.Nil instead of an error.
func FromBytesOrNil(input []byte) UUID {
uuid, err := FromBytes(input)
if err != nil {
return Nil
}
return uuid
}
// FromString returns a UUID parsed from the input string.
// Input is expected in a form accepted by UnmarshalText.
func FromString(input string) (UUID, error) {
u := UUID{}
err := u.UnmarshalText([]byte(input))
return u, err
}
// FromStringOrNil returns a UUID parsed from the input string.
// Same behavior as FromString(), but returns uuid.Nil instead of an 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 the String() method.
func (u UUID) MarshalText() ([]byte, error) {
return []byte(u.String()), nil
}
// 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"
// "{6ba7b8109dad11d180b400c04fd430c8}",
// "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
// "urn:uuid:6ba7b8109dad11d180b400c04fd430c8",
// "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8",
// "6ba7-b810-9dad-11d1-80b4-00c0-4fd4-30c8"
//
// ABNF for supported UUID text representation follows:
//
// URN := 'urn'
// UUID-NID := 'uuid'
//
// hexdig := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' |
// 'a' | 'b' | 'c' | 'd' | 'e' | 'f' |
// 'A' | 'B' | 'C' | 'D' | 'E' | 'F'
//
// hexoct := hexdig hexdig
// 2hexoct := hexoct hexoct
// 4hexoct := 2hexoct 2hexoct
// 6hexoct := 4hexoct 2hexoct
// 12hexoct := 6hexoct 6hexoct
//
// hashlike := 12hexoct
// hyphenated := hyphen after any group of 4 hexdig
// Ex.6ba7-b810-9dad-11d1-80b4-00c0-4fd4-30c8
// Ex.6ba7-b810-9dad11d1-80b400c0-4fd4-30c8
//
// uuid := hyphenated | hashlike | braced | urn
//
// braced := '{' hyphenated '}' | '{' hashlike '}'
// urn := URN ':' UUID-NID ':' hyphenated
func (u *UUID) UnmarshalText(text []byte) error {
l := len(text)
stringifiedText := string(text)
if l < 32 || l > 48 {
return fmt.Errorf("uuid: incorrect UUID length: %s", text)
} else if stringifiedText[0] == '{' && stringifiedText[l-1] == '}' {
return u.decodeHyphenated(text[1 : l-1])
} else if bytes.Equal(text[:9], urnPrefix) {
return u.decodeHyphenated(text[9:l])
} else {
return u.decodeHyphenated(text)
}
}
// decodeHashLike decodes UUID strings that are using the following format:
//
// "6ba7b8109dad11d180b400c04fd430c8".
func (u *UUID) decodeHashLike(t []byte) error {
src := t[:]
dst := u[:]
_, err := hex.Decode(dst, src)
return err
}
// decodeHyphenated decodes UUID strings that are using the following format:
//
// "6ba7-b810-9dad-11d1-80b4-00c0-4fd4-30c8"
// "6ba7b810-9dad-11d1-80b400c0-4fd4-30c8"
func (u *UUID) decodeHyphenated(t []byte) error {
l := len(t)
if l < 32 || l > 40 {
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
}
hashLike := make([]byte, 32)
countSinceHyphen := 0
i := 0
for _, c := range t {
if i >= len(hashLike) {
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
}
if c == '-' {
if countSinceHyphen == 0 || countSinceHyphen%4 != 0 {
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
}
countSinceHyphen = 0
continue
}
hashLike[i] = c
i++
countSinceHyphen++
}
if i != len(hashLike) {
return fmt.Errorf("uuid: incorrect UUID format: %s", t)
}
return u.decodeHashLike(hashLike)
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (u UUID) MarshalBinary() ([]byte, error) {
return u.bytes(), nil
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
// It will return an error if the slice isn't 16 bytes long.
func (u *UUID) UnmarshalBinary(data []byte) error {
if len(data) != Size {
return fmt.Errorf("uuid: UUID must be exactly 16 bytes long, got %d bytes", len(data))
}
copy(u[:], data)
return nil
}