@@ -80,6 +80,13 @@ type MarshalCustomLinks interface {
8080 GetCustomLinks (string ) Links
8181}
8282
83+ // The MarshalCustomRelationshipMeta interface can be implemented if the struct should
84+ // want a custom meta in a relationship.
85+ type MarshalCustomRelationshipMeta interface {
86+ MarshalIdentifier
87+ GetCustomMeta (string ) map [string ]Meta
88+ }
89+
8390// A ServerInformation implementor can be passed to MarshalWithURLs to generate
8491// the `self` and `related` urls inside `links`.
8592type ServerInformation interface {
@@ -240,6 +247,19 @@ func isToMany(relationshipType RelationshipType, name string) bool {
240247 return relationshipType == ToManyRelationship
241248}
242249
250+ func getMetaForRelation (metaSource MarshalCustomRelationshipMeta , name string , information ServerInformation ) map [string ]interface {} {
251+ meta := make (map [string ]interface {})
252+ base := getLinkBaseURL (metaSource , information )
253+ if metaMap , ok := metaSource .GetCustomMeta (base )[name ]; ok {
254+ for k , v := range metaMap {
255+ if _ , ok := meta [k ]; ! ok {
256+ meta [k ] = v
257+ }
258+ }
259+ }
260+ return meta
261+ }
262+
243263func getStructRelationships (relationer MarshalLinkedRelations , information ServerInformation ) map [string ]Relationship {
244264 referencedIDs := relationer .GetReferencedIDs ()
245265 sortedResults := map [string ][]ReferenceID {}
@@ -282,9 +302,16 @@ func getStructRelationships(relationer MarshalLinkedRelations, information Serve
282302 // set URLs if necessary
283303 links := getLinksForServerInformation (relationer , name , information )
284304
305+ // get the custom meta for this relationship
306+ var meta map [string ]interface {}
307+ if customMetaSource , ok := relationer .(MarshalCustomRelationshipMeta ); ok {
308+ meta = getMetaForRelation (customMetaSource , name , information )
309+ }
310+
285311 relationship := Relationship {
286312 Data : & container ,
287313 Links : links ,
314+ Meta : meta ,
288315 }
289316
290317 relationships [name ] = relationship
@@ -303,8 +330,16 @@ func getStructRelationships(relationer MarshalLinkedRelations, information Serve
303330 }
304331
305332 links := getLinksForServerInformation (relationer , name , information )
333+
334+ // get the custom meta for this relationship
335+ var meta map [string ]interface {}
336+ if customMetaSource , ok := relationer .(MarshalCustomRelationshipMeta ); ok {
337+ meta = getMetaForRelation (customMetaSource , name , information )
338+ }
339+
306340 relationship := Relationship {
307341 Links : links ,
342+ Meta : meta ,
308343 }
309344
310345 // skip relationship data completely if IsNotLoaded is set
0 commit comments