Skip to content

Commit

Permalink
pkg/uuid: use google/uuid.Validate in Valid
Browse files Browse the repository at this point in the history
So that any UUID string accepted by uuid.Parse also validates correctly
with uuid.Valid, for the sake of consistency.

Note that this means that uuid.Valid error messages no longer contain
the input string, but that's for the best, as that often led
to rendered errors repeating themselves:

    valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401":
        ./in.cue:16:5

The new rendered error is better:

    valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID format:
        ./in.cue:16:5

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Ib7b44247b1ff2efe3fe90b916211b268ad8d856a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199881
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Aug 23, 2024
1 parent a19cab1 commit e525f29
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 70 deletions.
80 changes: 24 additions & 56 deletions pkg/uuid/testdata/uuid.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@ urn: a: uuid.URN(sha1.a)
toInt: a: uuid.ToInt(sha1.a)
-- out/uuid-v3 --
Errors:
valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401":
valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID format:
./in.cue:16:5
valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): invalid UUID "052ef62d-7223-58b6-a551-c1deee":
valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): invalid UUID length: 30:
./in.cue:17:5
valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID "{123e4567-e89b-12d3-a456-426655440000":
valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID length: 37:
./in.cue:18:5
valid.ok.b: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401":
./in.cue:9:5
valid.ok.c: invalid value "urn:uuid:123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID "urn:uuid:123e4567-e89b-12d3-a456-426655440000":
./in.cue:10:5
valid.ok.d: invalid value "{123e4567-e89b-12d3-a456-426655440000}" (does not satisfy uuid.Valid): invalid UUID "{123e4567-e89b-12d3-a456-426655440000}":
./in.cue:11:5
valid.ok.e: invalid value "123e4567e89b12d3a456426655440000" (does not satisfy uuid.Valid): invalid UUID "123e4567e89b12d3a456426655440000":
./in.cue:12:5
parse.err.a: error in call to uuid.Parse: invalid UUID format:
./in.cue:35:13
parse.err.b: error in call to uuid.Parse: invalid UUID length: 30:
Expand Down Expand Up @@ -88,15 +80,15 @@ invalidTable: {
valid: {
ok: {
a: "052ef62d-7223-58b6-a551-c1deee46d401"
b: _|_ // valid.ok.b: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): valid.ok.b: invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401"
c: _|_ // valid.ok.c: invalid value "urn:uuid:123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): valid.ok.c: invalid UUID "urn:uuid:123e4567-e89b-12d3-a456-426655440000"
d: _|_ // valid.ok.d: invalid value "{123e4567-e89b-12d3-a456-426655440000}" (does not satisfy uuid.Valid): valid.ok.d: invalid UUID "{123e4567-e89b-12d3-a456-426655440000}"
e: _|_ // valid.ok.e: invalid value "123e4567e89b12d3a456426655440000" (does not satisfy uuid.Valid): valid.ok.e: invalid UUID "123e4567e89b12d3a456426655440000"
b: "052EF62D-7223-58B6-A551-C1DEEE46D401"
c: "urn:uuid:123e4567-e89b-12d3-a456-426655440000"
d: "{123e4567-e89b-12d3-a456-426655440000}"
e: "123e4567e89b12d3a456426655440000"
}
err: {
a: _|_ // valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): valid.err.a: invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401"
b: _|_ // valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): valid.err.b: invalid UUID "052ef62d-7223-58b6-a551-c1deee"
c: _|_ // valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): valid.err.c: invalid UUID "{123e4567-e89b-12d3-a456-426655440000"
a: _|_ // valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): valid.err.a: invalid UUID format
b: _|_ // valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): valid.err.b: invalid UUID length: 30
c: _|_ // valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): valid.err.c: invalid UUID length: 37
}
}
parse: {
Expand Down Expand Up @@ -132,57 +124,33 @@ toInt: {
diff old new
--- old
+++ new
@@ -1,25 +1,18 @@
@@ -1,13 +1,10 @@
Errors:
valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401":
valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID format:
./in.cue:16:5
- ./in.cue:26:13
valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): invalid UUID "052ef62d-7223-58b6-a551-c1deee":
valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): invalid UUID length: 30:
./in.cue:17:5
- ./in.cue:26:13
valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID "{123e4567-e89b-12d3-a456-426655440000":
valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID length: 37:
./in.cue:18:5
- ./in.cue:26:13
valid.ok.b: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401":
./in.cue:9:5
- ./in.cue:23:12
valid.ok.c: invalid value "urn:uuid:123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID "urn:uuid:123e4567-e89b-12d3-a456-426655440000":
./in.cue:10:5
- ./in.cue:23:12
valid.ok.d: invalid value "{123e4567-e89b-12d3-a456-426655440000}" (does not satisfy uuid.Valid): invalid UUID "{123e4567-e89b-12d3-a456-426655440000}":
./in.cue:11:5
- ./in.cue:23:12
valid.ok.e: invalid value "123e4567e89b12d3a456426655440000" (does not satisfy uuid.Valid): invalid UUID "123e4567e89b12d3a456426655440000":
./in.cue:12:5
- ./in.cue:23:12
parse.err.a: error in call to uuid.Parse: invalid UUID format:
./in.cue:35:13
parse.err.b: error in call to uuid.Parse: invalid UUID length: 30:
-- diff/todo/p2 --
Missing error positions.
-- out/uuid --
Errors:
valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401":
valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): invalid UUID format:
./in.cue:16:5
./in.cue:26:13
valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): invalid UUID "052ef62d-7223-58b6-a551-c1deee":
valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): invalid UUID length: 30:
./in.cue:17:5
./in.cue:26:13
valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID "{123e4567-e89b-12d3-a456-426655440000":
valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID length: 37:
./in.cue:18:5
./in.cue:26:13
valid.ok.b: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401":
./in.cue:9:5
./in.cue:23:12
valid.ok.c: invalid value "urn:uuid:123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): invalid UUID "urn:uuid:123e4567-e89b-12d3-a456-426655440000":
./in.cue:10:5
./in.cue:23:12
valid.ok.d: invalid value "{123e4567-e89b-12d3-a456-426655440000}" (does not satisfy uuid.Valid): invalid UUID "{123e4567-e89b-12d3-a456-426655440000}":
./in.cue:11:5
./in.cue:23:12
valid.ok.e: invalid value "123e4567e89b12d3a456426655440000" (does not satisfy uuid.Valid): invalid UUID "123e4567e89b12d3a456426655440000":
./in.cue:12:5
./in.cue:23:12
parse.err.a: error in call to uuid.Parse: invalid UUID format:
./in.cue:35:13
parse.err.b: error in call to uuid.Parse: invalid UUID length: 30:
Expand Down Expand Up @@ -212,15 +180,15 @@ invalidTable: {
valid: {
ok: {
a: "052ef62d-7223-58b6-a551-c1deee46d401"
b: _|_ // valid.ok.b: invalid value "052EF62D-7223-58B6-A551-C1DEEE46D401" (does not satisfy uuid.Valid): valid.ok.b: invalid UUID "052EF62D-7223-58B6-A551-C1DEEE46D401"
c: _|_ // valid.ok.c: invalid value "urn:uuid:123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): valid.ok.c: invalid UUID "urn:uuid:123e4567-e89b-12d3-a456-426655440000"
d: _|_ // valid.ok.d: invalid value "{123e4567-e89b-12d3-a456-426655440000}" (does not satisfy uuid.Valid): valid.ok.d: invalid UUID "{123e4567-e89b-12d3-a456-426655440000}"
e: _|_ // valid.ok.e: invalid value "123e4567e89b12d3a456426655440000" (does not satisfy uuid.Valid): valid.ok.e: invalid UUID "123e4567e89b12d3a456426655440000"
b: "052EF62D-7223-58B6-A551-C1DEEE46D401"
c: "urn:uuid:123e4567-e89b-12d3-a456-426655440000"
d: "{123e4567-e89b-12d3-a456-426655440000}"
e: "123e4567e89b12d3a456426655440000"
}
err: {
a: _|_ // valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): valid.err.a: invalid UUID "052ef62d_7223_58b6_a551_c1deee46d401"
b: _|_ // valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): valid.err.b: invalid UUID "052ef62d-7223-58b6-a551-c1deee"
c: _|_ // valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): valid.err.c: invalid UUID "{123e4567-e89b-12d3-a456-426655440000"
a: _|_ // valid.err.a: invalid value "052ef62d_7223_58b6_a551_c1deee46d401" (does not satisfy uuid.Valid): valid.err.a: invalid UUID format
b: _|_ // valid.err.b: invalid value "052ef62d-7223-58b6-a551-c1deee" (does not satisfy uuid.Valid): valid.err.b: invalid UUID length: 30
c: _|_ // valid.err.c: invalid value "{123e4567-e89b-12d3-a456-426655440000" (does not satisfy uuid.Valid): valid.err.c: invalid UUID length: 37
}
}
parse: {
Expand Down
18 changes: 4 additions & 14 deletions pkg/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,14 @@
package uuid

import (
"fmt"
"math/big"
"regexp"
"sync"

"github.com/google/uuid"
)

var valid = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
})

// TODO(mvdan): should Valid not use [uuid.Validate]?

// Valid can be used to define a valid Valid.
// Valid ensures that s is a valid UUID which would be accepted by Parse.
func Valid(s string) error {
if !valid().MatchString(string(s)) {
return fmt.Errorf("invalid UUID %q", s)
}
return nil
return uuid.Validate(s)
}

// Parse decodes s into a UUID or returns an error. Both the standard UUID forms
Expand All @@ -50,6 +38,8 @@ func Parse(s string) (string, error) {
return string(x.String()), err
}

// TODO(mvdan): what is ToString meant to do? it appears like a no-op?

// String represents a 128-bit UUID value as a string.
func ToString(x string) string {
return string(x)
Expand Down

0 comments on commit e525f29

Please sign in to comment.