From e525f29af6fb0c4a53e99443f00f207b40c49f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 22 Aug 2024 13:10:53 +0100 Subject: [PATCH] pkg/uuid: use google/uuid.Validate in Valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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í Change-Id: Ib7b44247b1ff2efe3fe90b916211b268ad8d856a Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199881 TryBot-Result: CUEcueckoo Reviewed-by: Matthew Sackman Unity-Result: CUE porcuepine --- pkg/uuid/testdata/uuid.txtar | 80 +++++++++++------------------------- pkg/uuid/uuid.go | 18 ++------ 2 files changed, 28 insertions(+), 70 deletions(-) diff --git a/pkg/uuid/testdata/uuid.txtar b/pkg/uuid/testdata/uuid.txtar index 3544788ffe4..62d26639feb 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 4b19d59c312..d164a5259c7 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)