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
13 changes: 13 additions & 0 deletions pkg/runtime/deepobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ func assignPathValues(dst interface{}, pathValues fieldOrValue) error {
it := iv.Type()

switch it.Kind() {
case reflect.Map:
dstMap := reflect.MakeMap(iv.Type())
for key, value := range pathValues.fields {
dstKey := reflect.ValueOf(key)
dstVal := reflect.New(iv.Type().Elem())
err := assignPathValues(dstVal.Interface(), value)
if err != nil {
return fmt.Errorf("error binding map: %w", err)
}
dstMap.SetMapIndex(dstKey, dstVal.Elem())
}
iv.Set(dstMap)
return nil
case reflect.Slice:
sliceLength := len(pathValues.fields)
dstSlice := reflect.MakeSlice(it, sliceLength, sliceLength)
Expand Down
31 changes: 19 additions & 12 deletions pkg/runtime/deepobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ type InnerObject struct {

// These are all possible field types, mandatory and optional.
type AllFields struct {
I int `json:"i"`
Oi *int `json:"oi,omitempty"`
F float32 `json:"f"`
Of *float32 `json:"of,omitempty"`
B bool `json:"b"`
Ob *bool `json:"ob,omitempty"`
As []string `json:"as"`
Oas *[]string `json:"oas,omitempty"`
O InnerObject `json:"o"`
Oo *InnerObject `json:"oo,omitempty"`
D MockBinder `json:"d"`
Od *MockBinder `json:"od,omitempty"`
I int `json:"i"`
Oi *int `json:"oi,omitempty"`
F float32 `json:"f"`
Of *float32 `json:"of,omitempty"`
B bool `json:"b"`
Ob *bool `json:"ob,omitempty"`
As []string `json:"as"`
Oas *[]string `json:"oas,omitempty"`
O InnerObject `json:"o"`
Oo *InnerObject `json:"oo,omitempty"`
D MockBinder `json:"d"`
Od *MockBinder `json:"od,omitempty"`
M map[string]int `json:"m"`
Om *map[string]int `json:"om,omitempty"`
}

func TestDeepObject(t *testing.T) {
Expand All @@ -40,6 +42,9 @@ func TestDeepObject(t *testing.T) {
Name: "Marcin Romaszewicz",
ID: 123,
}
om := map[string]int{
"additional": 1,
}
d := MockBinder{Time: time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC)}

srcObj := AllFields{
Expand All @@ -58,6 +63,8 @@ func TestDeepObject(t *testing.T) {
Oo: &oo,
D: d,
Od: &d,
M: om,
Om: &om,
}

marshaled, err := MarshalDeepObject(srcObj, "p")
Expand Down