-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* move gin example to dedicated example folder * included tests * add ContentType middleware function * update comments and func names * createResourceIdentifier unit tests * more unit tests
- Loading branch information
Showing
13 changed files
with
624 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package examples | ||
|
||
import ( | ||
"github.com/alehechka/go-jsonapi/jsonapi" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
engine := gin.Default() | ||
|
||
engine.GET("/record", getRecords) | ||
engine.GET("/record/:id", getRecord) | ||
|
||
engine.Run() | ||
} | ||
|
||
type Record struct { | ||
RecordID string `json:"-"` | ||
FirstName string `json:"firstName"` | ||
Age int `json:"age"` | ||
IsAdmin bool `json:"isAdmin"` | ||
} | ||
|
||
func (record Record) ID() string { | ||
return record.RecordID | ||
} | ||
|
||
func (record Record) Type() string { | ||
return "records" | ||
} | ||
|
||
func getRecords(ctx *gin.Context) { | ||
|
||
ctx.JSON(200, jsonapi.CreateCollectionResponse(ctx.Request)(jsonapi.CollectionResponse{ | ||
Nodes: records(), | ||
})) | ||
} | ||
|
||
func getRecord(ctx *gin.Context) { | ||
records := records() | ||
recordID := ctx.Param("id") | ||
|
||
var record Record | ||
for _, r := range records { | ||
if r.RecordID == recordID { | ||
record = r | ||
} | ||
} | ||
|
||
ctx.JSON(200, jsonapi.CreateResponse(ctx.Request)(jsonapi.Response{ | ||
Node: record, | ||
})) | ||
} | ||
|
||
func records() []Record { | ||
return []Record{{ | ||
RecordID: "1234", | ||
FirstName: "Joe", | ||
Age: 25, | ||
IsAdmin: true, | ||
}, | ||
{ | ||
RecordID: "4321", | ||
FirstName: "Sally", | ||
Age: 30, | ||
IsAdmin: true, | ||
}} | ||
} |
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,45 @@ | ||
package jsonapi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_transformIncluded_Nil(t *testing.T) { | ||
included := transformIncluded([]Node{}, nil, baseURL) | ||
|
||
assert.Nil(t, included) | ||
} | ||
|
||
func Test_transformIncluded(t *testing.T) { | ||
included := transformIncluded([]Node{testObject}, []Node{}, baseURL) | ||
|
||
assert.NotNil(t, included) | ||
assert.Equal(t, 1, len(included)) | ||
|
||
include := included[0] | ||
|
||
assert.Equal(t, testObject.ID(), include.ID) | ||
assert.Equal(t, testObject.Type(), include.Type) | ||
assert.Equal(t, testObject, include.Attributes) | ||
assert.Equal(t, testObject.Meta(), include.Meta) | ||
|
||
assert.NotNil(t, include.Links) | ||
assert.NotNil(t, include.Links[SelfKey]) | ||
assert.Equal(t, baseURL+testObject.Links()[SelfKey].Href, include.Links[SelfKey]) | ||
} | ||
|
||
func Test_transformIncludedNode(t *testing.T) { | ||
include := transformIncludedNode(testObject, baseURL) | ||
|
||
assert.NotNil(t, include) | ||
assert.Equal(t, testObject.ID(), include.ID) | ||
assert.Equal(t, testObject.Type(), include.Type) | ||
assert.Equal(t, testObject, include.Attributes) | ||
assert.Equal(t, testObject.Meta(), include.Meta) | ||
|
||
assert.NotNil(t, include.Links) | ||
assert.NotNil(t, include.Links[SelfKey]) | ||
assert.Equal(t, baseURL+testObject.Links()[SelfKey].Href, include.Links[SelfKey]) | ||
} |
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
Oops, something went wrong.