@@ -8,6 +8,18 @@ import (
88 "strings"
99)
1010
11+ // RelationshipType is used to specify whether the relationship with referenced objects are to-one or to-many
12+ type RelationshipType int
13+
14+ const (
15+ // DefaultRelationship guesses the relationship type based on the pluralization of the reference name
16+ DefaultRelationship RelationshipType = iota
17+ // ToOneRelationship forces the relationship to be to-one
18+ ToOneRelationship
19+ // ToManyRelationship forces the relationship to be to-many
20+ ToManyRelationship
21+ )
22+
1123// MarshalIdentifier interface is necessary to give an element
1224// a unique ID. This interface must be implemented for
1325// marshal and unmarshal in order to let them store
@@ -19,9 +31,10 @@ type MarshalIdentifier interface {
1931// ReferenceID contains all necessary information in order
2032// to reference another struct in jsonapi
2133type ReferenceID struct {
22- ID string
23- Type string
24- Name string
34+ ID string
35+ Type string
36+ Name string
37+ Relationship RelationshipType
2538}
2639
2740// Reference information about possible references of a struct
@@ -30,9 +43,10 @@ type ReferenceID struct {
3043// Otherwise, if IsNotLoaded is false and GetReferencedIDs() returns no IDs for this reference name, an
3144// empty `data` field will be added which means that there are no references.
3245type Reference struct {
33- Type string
34- Name string
35- IsNotLoaded bool
46+ Type string
47+ Name string
48+ IsNotLoaded bool
49+ Relationship RelationshipType
3650}
3751
3852// MarshalReferences must be implemented if the struct to be serialized has relations. This must be done
@@ -191,6 +205,13 @@ func marshalData(element MarshalIdentifier, information ServerInformation) (*Dat
191205 return result , err
192206}
193207
208+ func isToMany (relationshipType RelationshipType , name string ) bool {
209+ if relationshipType == DefaultRelationship {
210+ return Pluralize (name ) == name
211+ }
212+ return relationshipType == ToManyRelationship
213+ }
214+
194215// getStructRelationships returns the relationships struct with ids
195216func getStructRelationships (relationer MarshalLinkedRelations , information ServerInformation ) * map [string ]Relationship {
196217 referencedIDs := relationer .GetReferencedIDs ()
@@ -213,7 +234,7 @@ func getStructRelationships(relationer MarshalLinkedRelations, information Serve
213234 relationships [name ] = Relationship {}
214235 // if referenceType is plural, we need to use an array for data, otherwise it's just an object
215236 container := RelationshipDataContainer {}
216- if Pluralize ( name ) == name {
237+ if isToMany ( referenceIDs [ 0 ]. Relationship , referenceIDs [ 0 ]. Name ) {
217238 // multiple elements in links
218239 container .DataArray = []RelationshipData {}
219240 for _ , referenceID := range referenceIDs {
@@ -247,7 +268,7 @@ func getStructRelationships(relationer MarshalLinkedRelations, information Serve
247268 for name , reference := range notIncludedReferences {
248269 container := RelationshipDataContainer {}
249270 // Plural empty relationships need an empty array and empty to-one need a null in the json
250- if ! reference .IsNotLoaded && Pluralize ( name ) == name {
271+ if ! reference .IsNotLoaded && isToMany ( reference . Relationship , reference . Name ) {
251272 container .DataArray = []RelationshipData {}
252273 }
253274
0 commit comments