-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from nyaruka/easier_uuids_validation
Replace `uuids.IsV4` and `uuids.IsV7` with `uuids.Is` and `uuids.Version`
- Loading branch information
Showing
3 changed files
with
38 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,26 @@ | ||
package uuids | ||
|
||
import "regexp" | ||
import ( | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
var ( | ||
V4Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`) | ||
V7Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`) | ||
Regex = regexp.MustCompile(`[0-9a-f]{8}-[0-9a-f]{4}-([1-7])[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}`) | ||
|
||
V4OnlyRegex = regexp.MustCompile(`^` + V4Regex.String() + `$`) | ||
V7OnlyRegex = regexp.MustCompile(`^` + V7Regex.String() + `$`) | ||
onlyRegex = regexp.MustCompile(`^` + Regex.String() + `$`) | ||
) | ||
|
||
// IsV4 returns whether the given string contains only a valid v4 UUID | ||
func IsV4(s string) bool { | ||
return V4OnlyRegex.MatchString(s) | ||
// Is returns whether the given string contains only a valid v4 UUID | ||
func Is(s string) bool { | ||
return onlyRegex.MatchString(s) | ||
} | ||
|
||
// IsV7 returns whether the given string contains only a valid v7 UUID | ||
func IsV7(s string) bool { | ||
return V7OnlyRegex.MatchString(s) | ||
func Version(s string) int { | ||
m := onlyRegex.FindStringSubmatch(s) | ||
if m == nil { | ||
return 0 | ||
} | ||
v, _ := strconv.Atoi(m[1]) | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters