Skip to content

Commit 7e5c901

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents fc6968d + e0fc4ee commit 7e5c901

File tree

9 files changed

+85
-44
lines changed

9 files changed

+85
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/bin/
22
/pkg/
33
/src/
4+
/.idea

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ language: go
22
go:
33
- 1.7.x
44
- 1.8.x
5+
- 1.9.x
56
- tip
67
script: script/test -v

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# jsonapi
22

3-
[![Build Status](https://travis-ci.org/google/jsonapi.svg?branch=master)](https://travis-ci.org/google/jsonapi) [![GoDoc](https://godoc.org/github.com/google/jsonapi?status.svg)](http://godoc.org/github.com/google/jsonapi)
3+
[![Build Status](https://travis-ci.org/google/jsonapi.svg?branch=master)](https://travis-ci.org/google/jsonapi)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/google/jsonapi)](https://goreportcard.com/report/github.com/google/jsonapi)
5+
[![GoDoc](https://godoc.org/github.com/google/jsonapi?status.svg)](http://godoc.org/github.com/google/jsonapi)
46

57
A serializer/deserializer for JSON payloads that comply to the
68
[JSON API - jsonapi.org](http://jsonapi.org) spec in go.

examples/fixtures.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,31 @@ func fixtureBlogCreate(i int) *Blog {
88
Title: "Title 1",
99
CreatedAt: time.Now(),
1010
Posts: []*Post{
11-
&Post{
11+
{
1212
ID: 1 * i,
1313
Title: "Foo",
1414
Body: "Bar",
1515
Comments: []*Comment{
16-
&Comment{
16+
{
1717
ID: 1 * i,
1818
Body: "foo",
1919
},
20-
&Comment{
20+
{
2121
ID: 2 * i,
2222
Body: "bar",
2323
},
2424
},
2525
},
26-
&Post{
26+
{
2727
ID: 2 * i,
2828
Title: "Fuubar",
2929
Body: "Bas",
3030
Comments: []*Comment{
31-
&Comment{
31+
{
3232
ID: 1 * i,
3333
Body: "foo",
3434
},
35-
&Comment{
35+
{
3636
ID: 3 * i,
3737
Body: "bas",
3838
},
@@ -44,11 +44,11 @@ func fixtureBlogCreate(i int) *Blog {
4444
Title: "Foo",
4545
Body: "Bar",
4646
Comments: []*Comment{
47-
&Comment{
47+
{
4848
ID: 1 * i,
4949
Body: "foo",
5050
},
51-
&Comment{
51+
{
5252
ID: 2 * i,
5353
Body: "bar",
5454
},

examples/handler_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,35 @@ func TestExampleHandler_get_list(t *testing.T) {
8484
t.Fatalf("Expected a status of %d, got %d", e, a)
8585
}
8686
}
87+
88+
func TestHttpErrorWhenHeaderDoesNotMatch(t *testing.T) {
89+
r, err := http.NewRequest(http.MethodGet, "/blogs", nil)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
r.Header.Set(headerAccept, "application/xml")
94+
95+
rr := httptest.NewRecorder()
96+
handler := &ExampleHandler{}
97+
handler.ServeHTTP(rr, r)
98+
99+
if rr.Code != http.StatusUnsupportedMediaType {
100+
t.Fatal("expected Unsupported Media Type staus error")
101+
}
102+
}
103+
104+
func TestHttpErrorWhenMethodDoesNotMatch(t *testing.T) {
105+
r, err := http.NewRequest(http.MethodPatch, "/blogs", nil)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
r.Header.Set(headerAccept, jsonapi.MediaType)
110+
111+
rr := httptest.NewRecorder()
112+
handler := &ExampleHandler{}
113+
handler.ServeHTTP(rr, r)
114+
115+
if rr.Code != http.StatusNotFound {
116+
t.Fatal("expected HTTP Status Not Found status error")
117+
}
118+
}

examples/models.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/jsonapi"
88
)
99

10+
// Blog is a model representing a blog site
1011
type Blog struct {
1112
ID int `jsonapi:"primary,blogs"`
1213
Title string `jsonapi:"attr,title"`
@@ -17,6 +18,7 @@ type Blog struct {
1718
ViewCount int `jsonapi:"attr,view_count"`
1819
}
1920

21+
// Post is a model representing a post on a blog
2022
type Post struct {
2123
ID int `jsonapi:"primary,posts"`
2224
BlogID int `jsonapi:"attr,blog_id"`
@@ -25,19 +27,21 @@ type Post struct {
2527
Comments []*Comment `jsonapi:"relation,comments"`
2628
}
2729

30+
// Comment is a model representing a user submitted comment
2831
type Comment struct {
2932
ID int `jsonapi:"primary,comments"`
3033
PostID int `jsonapi:"attr,post_id"`
3134
Body string `jsonapi:"attr,body"`
3235
}
3336

34-
// Blog Links
37+
// JSONAPILinks implements the Linkable interface for a blog
3538
func (blog Blog) JSONAPILinks() *jsonapi.Links {
3639
return &jsonapi.Links{
3740
"self": fmt.Sprintf("https://example.com/blogs/%d", blog.ID),
3841
}
3942
}
4043

44+
// JSONAPIRelationshipLinks implements the RelationshipLinkable interface for a blog
4145
func (blog Blog) JSONAPIRelationshipLinks(relation string) *jsonapi.Links {
4246
if relation == "posts" {
4347
return &jsonapi.Links{
@@ -52,13 +56,14 @@ func (blog Blog) JSONAPIRelationshipLinks(relation string) *jsonapi.Links {
5256
return nil
5357
}
5458

55-
// Blog Meta
59+
// JSONAPIMeta implements the Metable interface for a blog
5660
func (blog Blog) JSONAPIMeta() *jsonapi.Meta {
5761
return &jsonapi.Meta{
5862
"detail": "extra details regarding the blog",
5963
}
6064
}
6165

66+
// JSONAPIRelationshipMeta implements the RelationshipMetable interface for a blog
6267
func (blog Blog) JSONAPIRelationshipMeta(relation string) *jsonapi.Meta {
6368
if relation == "posts" {
6469
return &jsonapi.Meta{

request_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -753,15 +753,15 @@ func samplePayload() io.Reader {
753753
Relationships: map[string]interface{}{
754754
"posts": &RelationshipManyNode{
755755
Data: []*Node{
756-
&Node{
756+
{
757757
Type: "posts",
758758
Attributes: map[string]interface{}{
759759
"title": "Foo",
760760
"body": "Bar",
761761
},
762762
ClientID: "1",
763763
},
764-
&Node{
764+
{
765765
Type: "posts",
766766
Attributes: map[string]interface{}{
767767
"title": "X",
@@ -782,14 +782,14 @@ func samplePayload() io.Reader {
782782
Relationships: map[string]interface{}{
783783
"comments": &RelationshipManyNode{
784784
Data: []*Node{
785-
&Node{
785+
{
786786
Type: "comments",
787787
Attributes: map[string]interface{}{
788788
"body": "Great post!",
789789
},
790790
ClientID: "4",
791791
},
792-
&Node{
792+
{
793793
Type: "comments",
794794
Attributes: map[string]interface{}{
795795
"body": "Needs some work!",
@@ -866,16 +866,16 @@ func testModel() *Blog {
866866
Title: "Title 1",
867867
CreatedAt: time.Now(),
868868
Posts: []*Post{
869-
&Post{
869+
{
870870
ID: 1,
871871
Title: "Foo",
872872
Body: "Bar",
873873
Comments: []*Comment{
874-
&Comment{
874+
{
875875
ID: 1,
876876
Body: "foo",
877877
},
878-
&Comment{
878+
{
879879
ID: 2,
880880
Body: "bar",
881881
},
@@ -885,16 +885,16 @@ func testModel() *Blog {
885885
Body: "foo",
886886
},
887887
},
888-
&Post{
888+
{
889889
ID: 2,
890890
Title: "Fuubar",
891891
Body: "Bas",
892892
Comments: []*Comment{
893-
&Comment{
893+
{
894894
ID: 1,
895895
Body: "foo",
896896
},
897-
&Comment{
897+
{
898898
ID: 3,
899899
Body: "bas",
900900
},
@@ -910,11 +910,11 @@ func testModel() *Blog {
910910
Title: "Foo",
911911
Body: "Bar",
912912
Comments: []*Comment{
913-
&Comment{
913+
{
914914
ID: 1,
915915
Body: "foo",
916916
},
917-
&Comment{
917+
{
918918
ID: 2,
919919
Body: "bar",
920920
},

response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var (
1919
// was not a valid numeric type.
2020
ErrBadJSONAPIID = errors.New(
2121
"id should be either string, int(8,16,32,64) or uint(8,16,32,64)")
22-
// ErrExpectedSlice is returned when a variable or arugment was expected to
22+
// ErrExpectedSlice is returned when a variable or argument was expected to
2323
// be a slice of *Structs; MarshalMany will return this error when its
2424
// interface{} argument is invalid.
2525
ErrExpectedSlice = errors.New("models should be a slice of struct pointers")

0 commit comments

Comments
 (0)