more docs for MarshalMany

This commit is contained in:
Sam Woodard 2015-07-13 10:44:47 -07:00
parent d9a31fb056
commit e7bd3d1fbc
1 changed files with 36 additions and 5 deletions

View File

@ -95,6 +95,9 @@ type Comment struct {
## Handler Examples ## Handler Examples
**All `Marshal` and `Unmarshal` methods except pointers or slices of
pointers contained with the `interface{}`s**
Now you have your structs are prepared to be seralized or materialized. Now you have your structs are prepared to be seralized or materialized.
What about the rest? What about the rest?
@ -114,16 +117,16 @@ Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#UnmarshalPayload)
#### `MarshalOnePayload` #### `MarshalOnePayload`
This method encodes a response for a single record only. If you want to serialize many
records, see, [MarshalManyPayload](#marshalmanypayload). Wrties a jsonapi response, with
related records sideloaded, into `included` array.
```go ```go
MarshalOnePayloadEmbedded(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/shwoodard/jsonapi#MarshalOnePayload)
This method encodes a response for a single record only. If you want to serialize many
records, see, [MarshalManyPayload](#marshalmanypayload). Wrties a jsonapi response, with
related records sideloaded, into `included` array.
#### Example #### Example
```go ```go
@ -147,6 +150,7 @@ func CreateBlog(w http.ResponseWriter, r *http.Request) {
``` ```
### List ### List
#### `MarshalManyPayload` #### `MarshalManyPayload`
```go ```go
@ -155,6 +159,33 @@ MarshalManyPayload(w io.Writer, models []interface{}) error
Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#MashalManyPayload) Visit [godoc](http://godoc.org/github.com/shwoodard/jsonapi#MashalManyPayload)
Takes an `io.Writer` and an slice of `interface{}`. Note, if you have a
type safe array of your structs, like,
```go
var blogs []*Blog
```
you will need to interate over the slice of `Blog` pointers and append
them to an interface array, like,
```go
blogInterface := make([]interface{}, len(blogs))
for i, blog := range blogs {
blogInterface[i] = blog
}
```
Alternatively, you can insert your `Blog`s into a slice of `interface{}`
the first time. For example when you fetch the `Blog`s from the db
`append` them to an `[]interface{}` rather than a `[]*Blog`,
```go
FetchBlogs() ([]interface{}, error)
```
#### Example #### Example
```go ```go