+ IsValidUUID

This commit is contained in:
Andreas Schulte 2023-11-03 20:43:13 +01:00
parent da8c963b7a
commit 47cbbb68e8
Signed by: andreas
GPG Key ID: DCD1B6A247B69DB6
2 changed files with 32 additions and 4 deletions

View File

@ -3,6 +3,8 @@ package utils
import (
"net/url"
"os"
"github.com/google/uuid"
)
// DoesFileExist returns true, if a file exists (and actually is a file)
@ -56,8 +58,8 @@ func IsStringInSliceOfStrings(slice []string, target string) bool {
return false
}
// IsValidUrl returns true, if s is a valid URL
func IsValidUrl(s string) bool {
// IsValidURL returns true, if s is a valid URL
func IsValidURL(s string) bool {
u, err := url.Parse(s)
if err != nil || u.Scheme == "" || u.Host == "" {
return false
@ -65,3 +67,9 @@ func IsValidUrl(s string) bool {
return true
}
// IsValidUUID returns true, if s is a valid UUID
func IsValidUUID(s string) bool {
_, err := uuid.Parse(s)
return err == nil
}

View File

@ -38,7 +38,7 @@ func TestIsStringInSliceOfStrings(t *testing.T) {
}
}
func TestIsValidUrl(t *testing.T) {
func TestIsValidURL(t *testing.T) {
data := map[string]bool{
"": false,
"0x0001f346": false,
@ -49,7 +49,27 @@ func TestIsValidUrl(t *testing.T) {
}
for d, expectedResult := range data {
result := IsValidUrl(d)
result := IsValidURL(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}
}
}
func TestIsValidUUID(t *testing.T) {
data := map[string]bool{
"": false,
"0x0001f346": false,
"0179eb94-7a81-11ee-b962-0242ac120002": true,
"c3bcd438-7a80-11ee-b962-0242ac120002": true,
"0a245e0a-7a81-11ee-b962-0242ac120002": true,
"9d90b107-fc59-45f8-a622-f55b0a1396d6": true,
"f327c2b7-15a5-43c4-aefb-c66c6f34a32e": true,
"02cb18ec-5d6a-428d-8159-643bde3f43bb": true,
}
for d, expectedResult := range data {
result := IsValidUUID(d)
if result != expectedResult {
t.Errorf("\ngot: %t\nwanted: %t\nfor: %q", result, expectedResult, d)
}