Skip to content
Merged
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
18 changes: 8 additions & 10 deletions deepobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,26 +335,24 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
}

func assignSlice(dst reflect.Value, pathValues fieldOrValue) error {
// Gather up the values
nValues := len(pathValues.fields)
values := make([]string, nValues)
// We expect to have consecutive array indices in the map

// Process each array element by index
for i := 0; i < nValues; i++ {
indexStr := strconv.Itoa(i)
fv, found := pathValues.fields[indexStr]
if !found {
return errors.New("array deepObjects must have consecutive indices")
}
values[i] = fv.value
}

// This could be cleaner, but we can call into assignPathValues to
// avoid recreating this logic.
for i := 0; i < nValues; i++ {
// Get the destination element
dstElem := dst.Index(i).Addr()
err := assignPathValues(dstElem.Interface(), fieldOrValue{value: values[i]})

// assignPathValues handles both simple values (via fv.value) and
// nested objects (via fv.fields) automatically
err := assignPathValues(dstElem.Interface(), fv)
if err != nil {
return fmt.Errorf("error binding array: %w", err)
return fmt.Errorf("error binding array element %d: %w", i, err)
}
}

Expand Down
54 changes: 54 additions & 0 deletions deepobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,57 @@ func TestDeepObject(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, srcObj, dstObj)
}

// Item represents an item object for testing array of objects
type Item struct {
Name string `json:"name"`
Value string `json:"value"`
}

func TestDeepObject_ArrayOfObjects(t *testing.T) {
// Test case for:
// name: items
// style: deepObject
// required: false
// explode: true
// schema:
// type: array
// minItems: 1
// items:
// type: object

srcArray := []Item{
{Name: "first", Value: "value1"},
{Name: "second", Value: "value2"},
}

// Marshal the array to deepObject format
marshaled, err := MarshalDeepObject(srcArray, "items")
require.NoError(t, err)
t.Log("Marshaled:", marshaled)

// Expected format for array of objects with explode:true should be:
// items[0][name]=first&items[0][value]=value1&items[1][name]=second&items[1][value]=value2

// Parse the marshaled string into url.Values
params := make(url.Values)
marshaledParts := strings.Split(marshaled, "&")
for _, p := range marshaledParts {
parts := strings.Split(p, "=")
require.Equal(t, 2, len(parts))
params.Set(parts[0], parts[1])
}

// Unmarshal back to the destination array
var dstArray []Item
err = UnmarshalDeepObject(&dstArray, "items", params)
require.NoError(t, err)

// Verify the result matches the source
assert.EqualValues(t, srcArray, dstArray)
assert.Len(t, dstArray, 2)
assert.Equal(t, "first", dstArray[0].Name)
assert.Equal(t, "value1", dstArray[0].Value)
assert.Equal(t, "second", dstArray[1].Name)
assert.Equal(t, "value2", dstArray[1].Value)
}