Skip to content

Commit bdef1c3

Browse files
committed
add uuid support
1 parent f7ab986 commit bdef1c3

5 files changed

Lines changed: 31 additions & 1 deletion

File tree

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require github.com/stretchr/testify v1.10.0
66

77
require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/google/uuid v1.6.0
910
github.com/pmezard/go-difflib v1.0.0 // indirect
1011
gopkg.in/yaml.v3 v3.0.1 // indirect
1112
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
35
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
46
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=

helper/any.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"reflect"
77
"strconv"
88
"time"
9+
10+
"github.com/google/uuid"
911
)
1012

1113
func AnyToFloat(v any) (float64, error) {
@@ -365,6 +367,12 @@ func AnyToType(in any, expected reflect.Type) (out any, err error) {
365367
if expected.Kind() == reflect.Slice {
366368
return []byte(strValue), nil
367369
} else if expected.Kind() == reflect.Array {
370+
// First try to parse as UUID as most common use case for [16]byte
371+
parsedUUID, err := uuid.Parse(strValue)
372+
if err == nil {
373+
return parsedUUID, nil
374+
}
375+
368376
byteSlice := []byte(strValue)
369377
arrayValue := reflect.New(expected).Elem()
370378
for i := 0; i < expected.Len() && i < len(byteSlice); i++ {

helper/any_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/google/uuid"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -1240,6 +1241,24 @@ func TestAnyToType(t *testing.T) {
12401241
expected: any([3]byte{'h', 'e', 'l'}),
12411242
expectedError: false,
12421243
},
1244+
{
1245+
name: "Valid 16-byte string to uuid.UUID",
1246+
args: args{
1247+
v: "0123456789abcdef",
1248+
expected: reflect.TypeOf(uuid.UUID{}),
1249+
},
1250+
expected: uuid.UUID{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'},
1251+
expectedError: false,
1252+
},
1253+
{
1254+
name: "Valid UUID format string to uuid.UUID",
1255+
args: args{
1256+
v: "550e8400-e29b-41d4-a716-446655440001",
1257+
expected: reflect.TypeOf(uuid.UUID{}),
1258+
},
1259+
expected: func() any { u, _ := uuid.Parse("550e8400-e29b-41d4-a716-446655440001"); return u }(),
1260+
expectedError: false,
1261+
},
12431262
// Array/Slice from []any
12441263
{
12451264
name: "Valid []any to []int",

helper/map_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestMapKeysToArrayOfAny(t *testing.T) {
1111
value := map[int]string{1: "apple", 2: "banana"}
1212
mapKeys, err := MapKeysToArrayOfAny(value)
1313
assert.NoError(t, err, "Expected no error on map key extraction")
14-
assert.Equal(t, []any{1, 2}, mapKeys, "Expected array to contain map keys")
14+
assert.ElementsMatch(t, []any{1, 2}, mapKeys, "Expected array to contain map keys")
1515
})
1616

1717
t.Run("Invalid type for map key extraction", func(t *testing.T) {

0 commit comments

Comments
 (0)