forked from Mirrors/jsonapi
Updated: build status badge, the go get installation instruction to account for the repo move, readme examples use ID instead of Id (so that they would be linter compliant if copy/pasted). shwoodard/jsonapi => google/jsonapi (#41)
This commit is contained in:
parent
b4fad2f2e6
commit
dd46e31eb5
50
README.md
50
README.md
|
@ -1,16 +1,16 @@
|
||||||
# jsonapi
|
# jsonapi
|
||||||
|
|
||||||
[![Build Status](https://travis-ci.org/shwoodard/jsonapi.svg?branch=master)](https://travis-ci.org/shwoodard/jsonapi)
|
[![Build Status](https://travis-ci.org/google/jsonapi.svg?branch=master)](https://travis-ci.org/google/jsonapi)
|
||||||
|
|
||||||
A serailizer/deserializer for json payloads that comply to the
|
A serailizer/deserializer for json payloads that comply to the
|
||||||
[JSON API - jsonapi.org](http://jsonapi.org) spec in go.
|
[JSON API - jsonapi.org](http://jsonapi.org) spec in go.
|
||||||
|
|
||||||
Also visit, [Godoc](http://godoc.org/github.com/shwoodard/jsonapi).
|
Also visit, [Godoc](http://godoc.org/github.com/google/jsonapi).
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```
|
```
|
||||||
go get -u github.com/shwoodard/jsonapi
|
go get -u github.com/google/jsonapi
|
||||||
```
|
```
|
||||||
|
|
||||||
Or, see [Alternative Installation](#alternative-installation).
|
Or, see [Alternative Installation](#alternative-installation).
|
||||||
|
@ -42,7 +42,7 @@ that look similar to these:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Blog struct {
|
type Blog struct {
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Posts []*Post `json:"posts"`
|
Posts []*Post `json:"posts"`
|
||||||
CurrentPost *Post `json:"current_post"`
|
CurrentPost *Post `json:"current_post"`
|
||||||
|
@ -52,8 +52,8 @@ type Blog struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
Id int `json:"id"`
|
ID int `json:"id"`
|
||||||
BlogId int `json:"blog_id"`
|
BlogID int `json:"blog_id"`
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
Comments []*Comment `json:"comments"`
|
Comments []*Comment `json:"comments"`
|
||||||
|
@ -61,7 +61,7 @@ type Post struct {
|
||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
PostId int `json:"post_id"`
|
PostID int `json:"post_id"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
Likes uint `json:"likes_count,omitempty"`
|
Likes uint `json:"likes_count,omitempty"`
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ all of your data easily.
|
||||||
|
|
||||||
## Example App
|
## Example App
|
||||||
|
|
||||||
[examples/app.go](https://github.com/shwoodard/jsonapi/blob/master/examples/app.go)
|
[examples/app.go](https://github.com/google/jsonapi/blob/master/examples/app.go)
|
||||||
|
|
||||||
This runnable file demonstrates the implementation of a create, a show,
|
This runnable file demonstrates the implementation of a create, a show,
|
||||||
and a list [http.Handler](http://golang.org/pkg/net/http#Handler). It
|
and a list [http.Handler](http://golang.org/pkg/net/http#Handler). It
|
||||||
|
@ -89,10 +89,10 @@ To run,
|
||||||
* Create the following directories or similar: `~/go`
|
* Create the following directories or similar: `~/go`
|
||||||
* `cd` there
|
* `cd` there
|
||||||
* Set `GOPATH` to `PWD` in your shell session, `export GOPATH=$PWD`
|
* Set `GOPATH` to `PWD` in your shell session, `export GOPATH=$PWD`
|
||||||
* `go get github.com/shwoodard/jsonapi`. (Append `-u` after `get` if you
|
* `go get github.com/google/jsonapi`. (Append `-u` after `get` if you
|
||||||
are updating.)
|
are updating.)
|
||||||
* `go run src/github.com/shwoodard/jsonapi/examples/app.go` or `cd
|
* `go run src/github.com/google/jsonapi/examples/app.go` or `cd
|
||||||
src/github.com/shwoodard/jsonapi/examples && go run app.go`
|
src/github.com/google/jsonapi/examples && go run app.go`
|
||||||
|
|
||||||
## `jsonapi` Tag Reference
|
## `jsonapi` Tag Reference
|
||||||
|
|
||||||
|
@ -107,26 +107,26 @@ using JSON API tags:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Blog struct {
|
type Blog struct {
|
||||||
Id int `jsonapi:"primary,blogs"`
|
ID int `jsonapi:"primary,blogs"`
|
||||||
Title string `jsonapi:"attr,title"`
|
Title string `jsonapi:"attr,title"`
|
||||||
Posts []*Post `jsonapi:"relation,posts"`
|
Posts []*Post `jsonapi:"relation,posts"`
|
||||||
CurrentPost *Post `jsonapi:"relation,current_post"`
|
CurrentPost *Post `jsonapi:"relation,current_post"`
|
||||||
CurrentPostId int `jsonapi:"attr,current_post_id"`
|
CurrentPostID int `jsonapi:"attr,current_post_id"`
|
||||||
CreatedAt time.Time `jsonapi:"attr,created_at"`
|
CreatedAt time.Time `jsonapi:"attr,created_at"`
|
||||||
ViewCount int `jsonapi:"attr,view_count"`
|
ViewCount int `jsonapi:"attr,view_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
Id int `jsonapi:"primary,posts"`
|
ID int `jsonapi:"primary,posts"`
|
||||||
BlogId int `jsonapi:"attr,blog_id"`
|
BlogID int `jsonapi:"attr,blog_id"`
|
||||||
Title string `jsonapi:"attr,title"`
|
Title string `jsonapi:"attr,title"`
|
||||||
Body string `jsonapi:"attr,body"`
|
Body string `jsonapi:"attr,body"`
|
||||||
Comments []*Comment `jsonapi:"relation,comments"`
|
Comments []*Comment `jsonapi:"relation,comments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
Id int `jsonapi:"primary,comments"`
|
ID int `jsonapi:"primary,comments"`
|
||||||
PostId int `jsonapi:"attr,post_id"`
|
PostID int `jsonapi:"attr,post_id"`
|
||||||
Body string `jsonapi:"attr,body"`
|
Body string `jsonapi:"attr,body"`
|
||||||
Likes uint `jsonapi:"attr,likes-count,omitempty"`
|
Likes uint `jsonapi:"attr,likes-count,omitempty"`
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,7 @@ about the rest?
|
||||||
### Create Record Example
|
### Create Record Example
|
||||||
|
|
||||||
You can Unmarshal a JSON API payload using
|
You can Unmarshal a JSON API payload using
|
||||||
[jsonapi.UnmarshalPayload](http://godoc.org/github.com/shwoodard/jsonapi#UnmarshalPayload).
|
[jsonapi.UnmarshalPayload](http://godoc.org/github.com/google/jsonapi#UnmarshalPayload).
|
||||||
It reads from an [io.Reader](https://golang.org/pkg/io/#Reader)
|
It reads from an [io.Reader](https://golang.org/pkg/io/#Reader)
|
||||||
containing a JSON API payload for one record (but can have related
|
containing a JSON API payload for one record (but can have related
|
||||||
records). Then, it materializes a struct that you created and passed in
|
records). Then, it materializes a struct that you created and passed in
|
||||||
|
@ -195,7 +195,7 @@ the top level, in request payloads at the moment. Bulk creates and
|
||||||
updates are not supported yet.
|
updates are not supported yet.
|
||||||
|
|
||||||
After saving your record, you can use,
|
After saving your record, you can use,
|
||||||
[MarshalOnePayload](http://godoc.org/github.com/shwoodard/jsonapi#MarshalOnePayload),
|
[MarshalOnePayload](http://godoc.org/github.com/google/jsonapi#MarshalOnePayload),
|
||||||
to write the JSON API response to an
|
to write the JSON API response to an
|
||||||
[io.Writer](https://golang.org/pkg/io/#Writer).
|
[io.Writer](https://golang.org/pkg/io/#Writer).
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ to write the JSON API response to an
|
||||||
UnmarshalPayload(in io.Reader, model interface{})
|
UnmarshalPayload(in io.Reader, model interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#UnmarshalPayload)
|
Visit [godoc](http://godoc.org/github.com/google/jsonapi#UnmarshalPayload)
|
||||||
|
|
||||||
#### `MarshalOnePayload`
|
#### `MarshalOnePayload`
|
||||||
|
|
||||||
|
@ -213,7 +213,7 @@ Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#UnmarshalPayload)
|
||||||
MarshalOnePayload(w io.Writer, model interface{}) error
|
MarshalOnePayload(w io.Writer, model interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#MarshalOnePayload)
|
Visit [godoc](http://godoc.org/github.com/google/jsonapi#MarshalOnePayload)
|
||||||
|
|
||||||
Writes a JSON API response, with related records sideloaded, into an
|
Writes a JSON API response, with related records sideloaded, into an
|
||||||
`included` array. This method encodes a response for a single record
|
`included` array. This method encodes a response for a single record
|
||||||
|
@ -250,7 +250,7 @@ func CreateBlog(w http.ResponseWriter, r *http.Request) {
|
||||||
MarshalManyPayload(w io.Writer, models []interface{}) error
|
MarshalManyPayload(w io.Writer, models []interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#MarshalManyPayload)
|
Visit [godoc](http://godoc.org/github.com/google/jsonapi#MarshalManyPayload)
|
||||||
|
|
||||||
Takes an `io.Writer` and an slice of `interface{}`. Note, if you have a
|
Takes an `io.Writer` and an slice of `interface{}`. Note, if you have a
|
||||||
type safe array of your structs, like,
|
type safe array of your structs, like,
|
||||||
|
@ -306,7 +306,7 @@ func ListBlogs(w http.ResponseWriter, r *http.Request) {
|
||||||
MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error
|
MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#MarshalOnePayloadEmbedded)
|
Visit [godoc](http://godoc.org/github.com/google/jsonapi#MarshalOnePayloadEmbedded)
|
||||||
|
|
||||||
This method is not strictly meant to for use in implementation code,
|
This method is not strictly meant to for use in implementation code,
|
||||||
although feel free. It was mainly created for use in tests; in most cases,
|
although feel free. It was mainly created for use in tests; in most cases,
|
||||||
|
@ -346,13 +346,13 @@ I use git subtrees to manage dependencies rather than `go get` so that
|
||||||
the src is committed to my repo.
|
the src is committed to my repo.
|
||||||
|
|
||||||
```
|
```
|
||||||
git subtree add --squash --prefix=src/github.com/shwoodard/jsonapi https://github.com/shwoodard/jsonapi.git master
|
git subtree add --squash --prefix=src/github.com/google/jsonapi https://github.com/google/jsonapi.git master
|
||||||
```
|
```
|
||||||
|
|
||||||
To update,
|
To update,
|
||||||
|
|
||||||
```
|
```
|
||||||
git subtree pull --squash --prefix=src/github.com/shwoodard/jsonapi https://github.com/shwoodard/jsonapi.git master
|
git subtree pull --squash --prefix=src/github.com/google/jsonapi https://github.com/google/jsonapi.git master
|
||||||
```
|
```
|
||||||
|
|
||||||
This assumes that I have my repo structured with a `src` dir containing
|
This assumes that I have my repo structured with a `src` dir containing
|
||||||
|
|
2
doc.go
2
doc.go
|
@ -60,6 +60,6 @@ the key in the "relationships" hash for the record.
|
||||||
|
|
||||||
Use the methods below to Marshal and Unmarshal jsonapi.org json payloads.
|
Use the methods below to Marshal and Unmarshal jsonapi.org json payloads.
|
||||||
|
|
||||||
Visit the readme at https://github.com/shwoodard/jsonapi
|
Visit the readme at https://github.com/google/jsonapi
|
||||||
*/
|
*/
|
||||||
package jsonapi
|
package jsonapi
|
||||||
|
|
|
@ -12,7 +12,7 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/shwoodard/jsonapi"
|
"github.com/google/jsonapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
func createBlog(w http.ResponseWriter, r *http.Request) {
|
func createBlog(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -49,7 +49,7 @@ var (
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Visit https://github.com/shwoodard/jsonapi#create for more info.
|
// Visit https://github.com/google/jsonapi#create for more info.
|
||||||
//
|
//
|
||||||
// model interface{} should be a pointer to a struct.
|
// model interface{} should be a pointer to a struct.
|
||||||
func UnmarshalPayload(in io.Reader, model interface{}) error {
|
func UnmarshalPayload(in io.Reader, model interface{}) error {
|
||||||
|
|
|
@ -93,7 +93,7 @@ func MarshalOne(model interface{}) (*OnePayload, error) {
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Visit https://github.com/shwoodard/jsonapi#list for more info.
|
// Visit https://github.com/google/jsonapi#list for more info.
|
||||||
//
|
//
|
||||||
// models []interface{} should be a slice of struct pointers.
|
// models []interface{} should be a slice of struct pointers.
|
||||||
func MarshalManyPayload(w io.Writer, models []interface{}) error {
|
func MarshalManyPayload(w io.Writer, models []interface{}) error {
|
||||||
|
|
Loading…
Reference in New Issue