From 46d3ced0434461be12e555852e2f1a9ed382e139 Mon Sep 17 00:00:00 2001 From: Aren Patel Date: Fri, 7 Jul 2017 17:58:51 -0700 Subject: [PATCH] Added support for implementing the Linkable and Metable interface on collections (slices) being Marshalled. (#94) --- response.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/response.go b/response.go index 1e9ca45..76c3a3d 100644 --- a/response.go +++ b/response.go @@ -84,7 +84,25 @@ func Marshal(models interface{}) (Payloader, error) { if err != nil { return nil, err } - return marshalMany(m) + + payload, err := marshalMany(m) + if err != nil { + return nil, err + } + + if linkableModels, isLinkable := models.(Linkable); isLinkable { + jl := linkableModels.JSONAPILinks() + if er := jl.validate(); er != nil { + return nil, er + } + payload.Links = linkableModels.JSONAPILinks() + } + + if metableModels, ok := models.(Metable); ok { + payload.Meta = metableModels.JSONAPIMeta() + } + + return payload, nil case reflect.Ptr: // Check that the pointer was to a struct if reflect.Indirect(vals).Kind() != reflect.Struct {