Documentation
¶
Overview ¶
Package jsonapi is for using the JSON-API format: parsing, serialization, checking the content-type, etc.
Index ¶
- Constants
- func BindRelations(req *http.Request) ([]couchdb.DocReference, error)
- func Data(c echo.Context, statusCode int, o Object, links *LinksList) error
- func DataError(c echo.Context, err *Error) error
- func DataErrorList(c echo.Context, errs ...*Error) error
- func DataList(c echo.Context, statusCode int, objs []Object, links *LinksList) error
- func DataListWithMeta(c echo.Context, statusCode int, meta Meta, objs []Object, links *LinksList) error
- func DataRelations(c echo.Context, statusCode int, refs []couchdb.DocReference, meta *Meta, ...) error
- func ExtractPaginationCursor(c echo.Context, defaultLimit, maxLimit int) (couchdb.Cursor, error)
- func MarshalObject(o Object) (json.RawMessage, error)
- func PaginationCursorToParams(cursor couchdb.Cursor) (url.Values, error)
- func WriteData(w io.Writer, o Object, links *LinksList) error
- type Document
- type Error
- func BadGateway(err error) *Error
- func BadJSON() *Error
- func BadRequest(err error) *Error
- func Conflict(err error) *Error
- func Errorf(status int, format string, args ...interface{}) *Error
- func Forbidden(err error) *Error
- func InternalServerError(err error) *Error
- func InvalidAttribute(attribute string, err error) *Error
- func InvalidParameter(parameter string, err error) *Error
- func MethodNotAllowed(method string) *Error
- func NewError(status int, detail string) *Error
- func NotFound(err error) *Error
- func PreconditionFailed(parameter string, err error) *Error
- func Unauthorized(err error) *Error
- type ErrorList
- type LinksList
- type Meta
- type Object
- type ObjectMarshalling
- type Relationship
- type RelationshipMap
- type SourceError
Constants ¶
const ContentType = "application/vnd.api+json"
ContentType is the official mime-type for JSON-API
Variables ¶
This section is empty.
Functions ¶
func BindRelations ¶
func BindRelations(req *http.Request) ([]couchdb.DocReference, error)
BindRelations extracts a Relationships request ( a list of ResourceIdentifier)
func Data ¶
Data can be called to send an answer with a JSON-API document containing a single object as data
func DataError ¶
DataError can be called to send an error answer with a JSON-API document containing a single value error.
func DataErrorList ¶
DataErrorList can be called to send an error answer with a JSON-API document containing multiple errors.
func DataList ¶
DataList can be called to send an multiple-value answer with a JSON-API document contains multiple objects.
func DataListWithMeta ¶
func DataListWithMeta(c echo.Context, statusCode int, meta Meta, objs []Object, links *LinksList) error
DataListWithMeta can be called to send a list of Objects with meta like a count, useful to indicate total number of results with pagination.
func DataRelations ¶
func DataRelations(c echo.Context, statusCode int, refs []couchdb.DocReference, meta *Meta, links *LinksList, included []Object) error
DataRelations can be called to send a Relations page, a list of ResourceIdentifier
func ExtractPaginationCursor ¶
ExtractPaginationCursor creates a Cursor from context Query.
func MarshalObject ¶
func MarshalObject(o Object) (json.RawMessage, error)
MarshalObject serializes an Object to JSON. It returns a json.RawMessage that can be used a in Document.
func PaginationCursorToParams ¶
PaginationCursorToParams transforms a Cursor into url.Values the url.Values contains only keys page[limit] & page[cursor] if the cursor is Done, the values will be empty.
Types ¶
type Document ¶
type Document struct {
Data *json.RawMessage `json:"data,omitempty"`
Errors ErrorList `json:"errors,omitempty"`
Links *LinksList `json:"links,omitempty"`
Meta *Meta `json:"meta,omitempty"`
Included []interface{} `json:"included,omitempty"`
}
Document is JSON-API document, identified by the mediatype application/vnd.api+json See http://jsonapi.org/format/#document-structure
type Error ¶
type Error struct {
Status int `json:"status,string"`
Title string `json:"title"`
Code string `json:"code,omitempty"`
Detail string `json:"detail,omitempty"`
Source SourceError `json:"source,omitempty"`
Links *LinksList `json:"links,omitempty"`
}
Error objects provide additional information about problems encountered while performing an operation. See http://jsonapi.org/format/#error-objects
func BadJSON ¶
func BadJSON() *Error
BadJSON returns a 400 formatted error meaning the json input is malformed.
func InternalServerError ¶
InternalServerError returns a 500 formatted error
func InvalidAttribute ¶
InvalidAttribute returns a 422 formatted error when an attribute is invalid
func InvalidParameter ¶
InvalidParameter returns a 422 formatted error when an HTTP or Query-String parameter is invalid
func MethodNotAllowed ¶
MethodNotAllowed returns a 405 formatted error
func PreconditionFailed ¶
PreconditionFailed returns a 412 formatted error when an expectation from an HTTP header is not matched
type LinksList ¶
type LinksList struct {
Self string `json:"self,omitempty"`
Related string `json:"related,omitempty"`
Prev string `json:"prev,omitempty"`
Next string `json:"next,omitempty"`
Icon string `json:"icon,omitempty"`
Perms string `json:"permissions,omitempty"`
Webhook string `json:"webhook,omitempty"`
// Thumbnails
Tiny string `json:"tiny,omitempty"`
Small string `json:"small,omitempty"`
Medium string `json:"medium,omitempty"`
Large string `json:"large,omitempty"`
// Preview for PDF
Preview string `json:"preview,omitempty"`
}
LinksList is the common links used in JSON-API for the top-level or a resource object See http://jsonapi.org/format/#document-links
type Meta ¶
type Meta struct {
Rev string `json:"rev,omitempty"`
Warning string `json:"warning,omitempty"`
Count *int `json:"count,omitempty"`
ExecutionStats *couchdb.ExecutionStats `json:"execution_stats,omitempty"`
}
Meta is a container for the couchdb revision and the total number of items, in JSON-API land
type Object ¶
type Object interface {
couchdb.Doc
Links() *LinksList
Relationships() RelationshipMap
Included() []Object
}
Object is an interface to serialize something to a JSON-API Object
type ObjectMarshalling ¶
type ObjectMarshalling struct {
Type string `json:"type"`
ID string `json:"id"`
Attributes *json.RawMessage `json:"attributes"`
Meta Meta `json:"meta"`
Links *LinksList `json:"links,omitempty"`
Relationships RelationshipMap `json:"relationships,omitempty"`
}
ObjectMarshalling is a JSON-API object See http://jsonapi.org/format/#document-resource-objects
func Bind ¶
func Bind(body io.Reader, attrs interface{}) (*ObjectMarshalling, error)
Bind is used to unmarshal an input JSONApi document. It binds an incoming request to a attribute type.
func BindCompound ¶
func BindCompound(body io.Reader) ([]*ObjectMarshalling, error)
BindCompound is used to unmarshal an compound input JSONApi document.
func (*ObjectMarshalling) GetRelationship ¶
func (o *ObjectMarshalling) GetRelationship(name string) (*Relationship, bool)
GetRelationship returns the relationship with the given name from the relationships map.
type Relationship ¶
type Relationship struct {
Links *LinksList `json:"links,omitempty"`
Meta *Meta `json:"meta,omitempty"`
Data interface{} `json:"data"`
}
Relationship is a resource linkage, as described in JSON-API See http://jsonapi.org/format/#document-resource-object-relationships
Data can be a single ResourceIdentifier for to-one relationships, or an array of them for to-many relationships.
func (*Relationship) ResourceIdentifier ¶
func (r *Relationship) ResourceIdentifier() (*couchdb.DocReference, bool)
ResourceIdentifier returns the resource identifier of the relationship.
type RelationshipMap ¶
type RelationshipMap map[string]Relationship
RelationshipMap is a map of relationships See http://jsonapi.org/format/#document-resource-object-relationships
type SourceError ¶
type SourceError struct {
Pointer string `json:"pointer,omitempty"`
Parameter string `json:"parameter,omitempty"`
}
SourceError contains references to the source of the error