@@ -47,18 +47,40 @@ var (
47
47
poolMu sync.Mutex
48
48
poolPos = randPoolSize // protected with poolMu
49
49
pool [randPoolSize ]byte // protected with poolMu
50
+
51
+ ErrInvalidUUIDFormat = errors .New ("invalid UUID format" )
52
+ ErrInvalidBracketedFormat = errors .New ("invalid bracketed UUID format" )
50
53
)
51
54
55
+ type URNPrefixError struct { prefix string }
56
+
57
+ func (e URNPrefixError ) Error () string {
58
+ return fmt .Sprintf ("invalid urn prefix: %q" , e .prefix )
59
+ }
60
+
61
+ func (e URNPrefixError ) Is (target error ) bool {
62
+ _ , ok := target .(URNPrefixError )
63
+ return ok
64
+ }
65
+
66
+ var ErrInvalidURNPrefix = URNPrefixError {}
67
+
52
68
type invalidLengthError struct { len int }
53
69
54
70
func (err invalidLengthError ) Error () string {
55
71
return fmt .Sprintf ("invalid UUID length: %d" , err .len )
56
72
}
57
73
74
+ func (e invalidLengthError ) Is (target error ) bool {
75
+ _ , ok := target .(invalidLengthError )
76
+ return ok
77
+ }
78
+
79
+ var ErrInvalidLength = invalidLengthError {}
80
+
58
81
// IsInvalidLengthError is matcher function for custom error invalidLengthError
59
82
func IsInvalidLengthError (err error ) bool {
60
- _ , ok := err .(invalidLengthError )
61
- return ok
83
+ return errors .Is (err , ErrInvalidLength )
62
84
}
63
85
64
86
// Parse decodes s into a UUID or returns an error if it cannot be parsed. Both
@@ -79,7 +101,7 @@ func Parse(s string) (UUID, error) {
79
101
// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
80
102
case 36 + 9 :
81
103
if ! strings .EqualFold (s [:9 ], "urn:uuid:" ) {
82
- return uuid , fmt . Errorf ( "invalid urn prefix: %q" , s [:9 ])
104
+ return uuid , URNPrefixError { s [:9 ]}
83
105
}
84
106
s = s [9 :]
85
107
@@ -93,7 +115,7 @@ func Parse(s string) (UUID, error) {
93
115
for i := range uuid {
94
116
uuid [i ], ok = xtob (s [i * 2 ], s [i * 2 + 1 ])
95
117
if ! ok {
96
- return uuid , errors . New ( "invalid UUID format" )
118
+ return uuid , ErrInvalidUUIDFormat
97
119
}
98
120
}
99
121
return uuid , nil
@@ -103,7 +125,8 @@ func Parse(s string) (UUID, error) {
103
125
// s is now at least 36 bytes long
104
126
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
105
127
if s [8 ] != '-' || s [13 ] != '-' || s [18 ] != '-' || s [23 ] != '-' {
106
- return uuid , errors .New ("invalid UUID format" )
128
+ return uuid , ErrInvalidUUIDFormat
129
+
107
130
}
108
131
for i , x := range [16 ]int {
109
132
0 , 2 , 4 , 6 ,
@@ -114,7 +137,7 @@ func Parse(s string) (UUID, error) {
114
137
} {
115
138
v , ok := xtob (s [x ], s [x + 1 ])
116
139
if ! ok {
117
- return uuid , errors . New ( "invalid UUID format" )
140
+ return uuid , ErrInvalidUUIDFormat
118
141
}
119
142
uuid [i ] = v
120
143
}
@@ -128,7 +151,7 @@ func ParseBytes(b []byte) (UUID, error) {
128
151
case 36 : // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
129
152
case 36 + 9 : // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
130
153
if ! bytes .EqualFold (b [:9 ], []byte ("urn:uuid:" )) {
131
- return uuid , fmt . Errorf ( "invalid urn prefix: %q" , b [:9 ])
154
+ return uuid , URNPrefixError { string ( b [:9 ])}
132
155
}
133
156
b = b [9 :]
134
157
case 36 + 2 : // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
@@ -138,7 +161,7 @@ func ParseBytes(b []byte) (UUID, error) {
138
161
for i := 0 ; i < 32 ; i += 2 {
139
162
uuid [i / 2 ], ok = xtob (b [i ], b [i + 1 ])
140
163
if ! ok {
141
- return uuid , errors . New ( "invalid UUID format" )
164
+ return uuid , ErrInvalidUUIDFormat
142
165
}
143
166
}
144
167
return uuid , nil
@@ -148,7 +171,7 @@ func ParseBytes(b []byte) (UUID, error) {
148
171
// s is now at least 36 bytes long
149
172
// it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
150
173
if b [8 ] != '-' || b [13 ] != '-' || b [18 ] != '-' || b [23 ] != '-' {
151
- return uuid , errors . New ( "invalid UUID format" )
174
+ return uuid , ErrInvalidUUIDFormat
152
175
}
153
176
for i , x := range [16 ]int {
154
177
0 , 2 , 4 , 6 ,
@@ -159,7 +182,7 @@ func ParseBytes(b []byte) (UUID, error) {
159
182
} {
160
183
v , ok := xtob (b [x ], b [x + 1 ])
161
184
if ! ok {
162
- return uuid , errors . New ( "invalid UUID format" )
185
+ return uuid , ErrInvalidUUIDFormat
163
186
}
164
187
uuid [i ] = v
165
188
}
@@ -205,14 +228,14 @@ func Validate(s string) error {
205
228
// UUID with "urn:uuid:" prefix
206
229
case 36 + 9 :
207
230
if ! strings .EqualFold (s [:9 ], "urn:uuid:" ) {
208
- return fmt . Errorf ( "invalid urn prefix: %q" , s [:9 ])
231
+ return URNPrefixError { s [:9 ]}
209
232
}
210
233
s = s [9 :]
211
234
212
235
// UUID enclosed in braces
213
236
case 36 + 2 :
214
237
if s [0 ] != '{' || s [len (s )- 1 ] != '}' {
215
- return fmt . Errorf ( "invalid bracketed UUID format" )
238
+ return ErrInvalidBracketedFormat
216
239
}
217
240
s = s [1 : len (s )- 1 ]
218
241
@@ -221,7 +244,7 @@ func Validate(s string) error {
221
244
for i := 0 ; i < len (s ); i += 2 {
222
245
_ , ok := xtob (s [i ], s [i + 1 ])
223
246
if ! ok {
224
- return errors . New ( "invalid UUID format" )
247
+ return ErrInvalidUUIDFormat
225
248
}
226
249
}
227
250
@@ -232,11 +255,11 @@ func Validate(s string) error {
232
255
// Check for standard UUID format
233
256
if len (s ) == 36 {
234
257
if s [8 ] != '-' || s [13 ] != '-' || s [18 ] != '-' || s [23 ] != '-' {
235
- return errors . New ( "invalid UUID format" )
258
+ return ErrInvalidUUIDFormat
236
259
}
237
260
for _ , x := range []int {0 , 2 , 4 , 6 , 9 , 11 , 14 , 16 , 19 , 21 , 24 , 26 , 28 , 30 , 32 , 34 } {
238
261
if _ , ok := xtob (s [x ], s [x + 1 ]); ! ok {
239
- return errors . New ( "invalid UUID format" )
262
+ return ErrInvalidUUIDFormat
240
263
}
241
264
}
242
265
}
0 commit comments