-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from jkongie/jsonapi-relationship-unmarshal
Update Relationship data structures to follow the spec.
- Loading branch information
Showing
4 changed files
with
114 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package jsh | ||
|
||
import ( | ||
"fmt" | ||
|
||
"encoding/json" | ||
) | ||
|
||
// Relationship represents a reference from the resource object in which it's | ||
// defined to other resource objects. | ||
type Relationship struct { | ||
Links *Links `json:"links,omitempty"` | ||
Data ResourceLinkage `json:"data,omitempty"` | ||
Meta map[string]interface{} `json:"meta,omitempty"` | ||
} | ||
|
||
// ResourceLinkage is a typedef around a slice of resource identifiers. This | ||
// allows us to implement a custom UnmarshalJSON. | ||
type ResourceLinkage []*ResourceIdentifier | ||
|
||
// ResourceIdentifier identifies an individual resource. | ||
type ResourceIdentifier struct { | ||
Type string `json:"type" valid:"required"` | ||
ID string `json:"id" valid:"required"` | ||
} | ||
|
||
/* | ||
UnmarshalJSON allows us to manually decode a the resource linkage via the | ||
json.Unmarshaler interface. | ||
*/ | ||
func (rl *ResourceLinkage) UnmarshalJSON(data []byte) error { | ||
// Create a sub-type here so when we call Unmarshal below, we don't recursively | ||
// call this function over and over | ||
type UnmarshalLinkage ResourceLinkage | ||
|
||
// if our "List" is a single object, modify the JSON to make it into a list | ||
// by wrapping with "[ ]" | ||
if data[0] == '{' { | ||
data = []byte(fmt.Sprintf("[%s]", data)) | ||
} | ||
|
||
newLinkage := UnmarshalLinkage{} | ||
|
||
err := json.Unmarshal(data, &newLinkage) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
convertedLinkage := ResourceLinkage(newLinkage) | ||
*rl = convertedLinkage | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package jsh | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
) | ||
|
||
func TestRelationship(t *testing.T) { | ||
Convey("ResourceLinkage Tests", t, func() { | ||
Convey("->UnmarshalJSON()", func() { | ||
|
||
Convey("should handle a linkage object", func() { | ||
jObj := `{"type": "testRelationship", "id": "ID456"}` | ||
|
||
rl := ResourceLinkage{} | ||
err := rl.UnmarshalJSON([]byte(jObj)) | ||
So(err, ShouldBeNil) | ||
So(len(rl), ShouldEqual, 1) | ||
}) | ||
|
||
Convey("should handle a linkage list", func() { | ||
jList := `[ | ||
{"type": "testRelationship", "id": "ID456"}, | ||
{"type": "testRelationship", "id": "ID789"} | ||
]` | ||
|
||
rl := ResourceLinkage{} | ||
err := rl.UnmarshalJSON([]byte(jList)) | ||
So(err, ShouldBeNil) | ||
So(len(rl), ShouldEqual, 2) | ||
}) | ||
}) | ||
}) | ||
} |