Documentation
¶
Overview ¶
Package jsonapi is for using the JSON-API format: parsing, serialization, checking the content-type, etc.
Index ¶
- Constants
- 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 MarshalObject(o Object) (json.RawMessage, error)
- type Document
- type Error
- func BadJSON() *Error
- func BadRequest(err error) *Error
- func Conflict(err error) *Error
- func InternalServerError(err error) *Error
- func InvalidAttribute(attribute string, err error) *Error
- func InvalidParameter(parameter string, err error) *Error
- func NewError(status int, msg ...interface{}) *Error
- func NotFound(err error) *Error
- func PreconditionFailed(parameter string, err error) *Error
- type ErrorList
- type LinksList
- type Meta
- type Object
- type ObjectMarshalling
- type Relationship
- type RelationshipMap
- type ResourceIdentifier
- 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 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 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.
Types ¶
type Document ¶
type Document struct {
Data *json.RawMessage `json:"data,omitempty"`
Errors ErrorList `json:"errors,omitempty"`
Links *LinksList `json:"links,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"`
Detail string `json:"detail"`
Source SourceError `json:"source,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 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"`
}
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"`
}
Meta is a container for the couchdb revision, 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(req *http.Request, attrs interface{}) (*ObjectMarshalling, error)
Bind is used to unmarshal an input JSONApi document. It binds an incoming request to a attribute type.
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"`
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() (*ResourceIdentifier, 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 ResourceIdentifier ¶
ResourceIdentifier is an object, used in relationships, to identify an individual resource See http://jsonapi.org/format/#document-resource-identifier-objects
func BindRelations ¶
func BindRelations(req *http.Request) ([]ResourceIdentifier, error)
BindRelations extracts a Relationships request ( a list of ResourceIdentifier)
type SourceError ¶
type SourceError struct {
Pointer string `json:"pointer,omitempty"`
Parameter string `json:"parameter,omitempty"`
}
SourceError contains references to the source of the error