Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion deepobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,12 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
}
iv.Set(dstSlice)
return nil
case reflect.Array:
// Array-backed text types such as uuid.UUID.
if tu, ok := v.Interface().(encoding.TextUnmarshaler); ok {
return tu.UnmarshalText([]byte(pathValues.value))
}
return errors.New("unhandled type: " + it.String())
case reflect.Struct:
// Some special types we care about are structs. Handle them
// here. They may be redefined, so we need to do some hoop
Expand Down Expand Up @@ -338,7 +344,7 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
dst.Set(reflect.ValueOf(tm))
return nil
}
// For other struct types that implement TextUnmarshaler (e.g. uuid.UUID),
// For other struct types that implement TextUnmarshaler,
// use that for binding. This comes after the legacy Date/time.Time checks
// above which have special fallback format handling.
if tu, ok := v.Interface().(encoding.TextUnmarshaler); ok {
Expand Down
43 changes: 43 additions & 0 deletions deepobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"
"unicode"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -561,3 +562,45 @@ func TestDeepObject_InterfaceRoundTrip(t *testing.T) {
var dst map[string]interface{}
assertDeepObjectWireSafe(t, marshaled, "properties", &dst, src)
}

func TestDeepObject_UUIDFields(t *testing.T) {
type UUIDObject struct {
ID uuid.UUID `json:"id"`
Owner *uuid.UUID `json:"owner,omitempty"`
}
id := uuid.MustParse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
owner := uuid.MustParse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")

t.Run("uuid values bind", func(t *testing.T) {
params := url.Values{}
params.Set("p[id]", id.String())
params.Set("p[owner]", owner.String())

var dst UUIDObject
err := UnmarshalDeepObject(&dst, "p", params)
require.NoError(t, err)
assert.Equal(t, UUIDObject{ID: id, Owner: &owner}, dst)
})

t.Run("invalid uuid returns error", func(t *testing.T) {
params := url.Values{}
params.Set("p[id]", "not-a-uuid")

var dst UUIDObject
err := UnmarshalDeepObject(&dst, "p", params)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid UUID")
})

t.Run("array without TextUnmarshaler is unhandled", func(t *testing.T) {
params := url.Values{}
params.Set("p[pair]", "a")

var dst struct {
Pair [2]string `json:"pair"`
}
err := UnmarshalDeepObject(&dst, "p", params)
require.Error(t, err)
assert.Contains(t, err.Error(), "unhandled type")
})
}