diff --git a/pkg/uuid/testdata/uuid.txtar b/pkg/uuid/testdata/uuid.txtar index 3544788ff..62d26639f 100644 --- a/pkg/uuid/testdata/uuid.txtar +++ b/pkg/uuid/testdata/uuid.txtar @@ -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: @@ -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: { @@ -132,29 +124,17 @@ 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: @@ -162,27 +142,15 @@ diff old new 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: @@ -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: { diff --git a/pkg/uuid/uuid.go b/pkg/uuid/uuid.go index 4b19d59c3..d164a5259 100644 --- a/pkg/uuid/uuid.go +++ b/pkg/uuid/uuid.go @@ -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 @@ -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)