Skip to content

Commit 166ae20

Browse files
authored
Add handler for map slice type (#2)
* Add map string slice type
1 parent ad6e35b commit 166ae20

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

request.go

+19
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ func unmarshalAttribute(
396396
return
397397
}
398398

399+
// Handle field of type map[string][]string
400+
if fieldValue.Type() == reflect.TypeOf(map[string][]string{}) {
401+
value, err = handleMapStringSlice(attribute, fieldValue)
402+
return
403+
}
404+
399405
// Handle field of type time.Time
400406
if fieldValue.Type() == reflect.TypeOf(time.Time{}) ||
401407
fieldValue.Type() == reflect.TypeOf(new(time.Time)) {
@@ -447,6 +453,19 @@ func handleStringSlice(attribute interface{}) (reflect.Value, error) {
447453
return reflect.ValueOf(values), nil
448454
}
449455

456+
func handleMapStringSlice(attribute interface{}, fieldValue reflect.Value) (reflect.Value, error) {
457+
values := map[string][]string{}
458+
for key, valSlice := range attribute.(map[string]interface{}) {
459+
sliceValues := make([]string, 0)
460+
for _, v := range valSlice.([]interface{}) {
461+
sliceValues = append(sliceValues, v.(string))
462+
}
463+
values[key] = sliceValues
464+
}
465+
466+
return reflect.ValueOf(values), nil
467+
}
468+
450469
func handleTime(attribute interface{}, args []string, fieldValue reflect.Value) (reflect.Value, error) {
451470
var isIso8601 bool
452471
var isRFC3339 bool

request_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -1387,3 +1387,54 @@ func TestUnmarshalNestedStructSlice(t *testing.T) {
13871387
out.Teams[0].Members[0].Firstname)
13881388
}
13891389
}
1390+
1391+
type HeaderData struct {
1392+
ID string `jsonapi:"primary,header-data"`
1393+
Name string `jsonapi:"attr,name"`
1394+
Headers map[string][]string `jsonapi:"attr,headers"`
1395+
}
1396+
1397+
func TestUnmarshalMapStringSlice(t *testing.T) {
1398+
headers := map[string][]string{
1399+
"cache-control": {"private"},
1400+
"content-length": {"129"},
1401+
}
1402+
sample := map[string]interface{}{
1403+
"data": map[string]interface{}{
1404+
"type": "header-data",
1405+
"id": "123",
1406+
"attributes": map[string]interface{}{
1407+
"name": "Planet Express",
1408+
"headers": headers,
1409+
},
1410+
},
1411+
}
1412+
1413+
data, err := json.Marshal(sample)
1414+
if err != nil {
1415+
t.Fatal(err)
1416+
}
1417+
in := bytes.NewReader(data)
1418+
out := new(HeaderData)
1419+
1420+
if err := UnmarshalPayload(in, out); err != nil {
1421+
t.Fatal(err)
1422+
}
1423+
1424+
if len(out.Headers["cache-control"]) == 0 {
1425+
t.Fatalf("Expected Headers to have Cache Control values, but got:`%s`", out.Headers["cache-control"])
1426+
}
1427+
1428+
if len(out.Headers["content-length"]) == 0 {
1429+
t.Fatalf("Expected Headers to have Content Length values, but got:`%s`", out.Headers["content-length"])
1430+
}
1431+
1432+
if out.Headers["cache-control"][0] != "private" {
1433+
t.Fatalf("Expected Cache Control Header to have 'private' value, but got:`%s`", out.Headers["cache-control"])
1434+
}
1435+
1436+
if out.Headers["content-length"][0] != "129" {
1437+
t.Fatalf("Expected Content Length Header to have '129' value, but got:`%s`", out.Headers["content-length"])
1438+
}
1439+
1440+
}

0 commit comments

Comments
 (0)