Documentation ¶
Overview ¶
Package api2go enables building REST servers for the JSONAPI.org standard.
See https://github.com/univedo/api2go for usage instructions.
Index ¶
- func Marshal(data interface{}) (interface{}, error)
- func MarshalToJSON(val interface{}) ([]byte, error)
- func Unmarshal(ctx unmarshalContext, values interface{}) error
- func UnmarshalFromJSON(data []byte, values interface{}) error
- type API
- type Controller
- type DataSource
- type Error
- type HTTPError
- type Request
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
func Marshal(data interface{}) (interface{}, error)
Marshal takes a struct (or slice of structs) and marshals them to a json encodable interface{} value
func MarshalToJSON ¶
MarshalToJSON takes a struct and marshals it to JSONAPI compliant JSON
func Unmarshal ¶
func Unmarshal(ctx unmarshalContext, values interface{}) error
Unmarshal reads a JSONAPI map to a model struct
func UnmarshalFromJSON ¶
UnmarshalFromJSON reads a JSONAPI compatible JSON document to a model struct
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API is a REST JSONAPI.
func NewAPI ¶
NewAPI returns an initialized API instance `prefix` is added in front of all endpoints.
func (*API) AddResource ¶
func (api *API) AddResource(prototype interface{}, source DataSource)
AddResource registers a data source for the given resource `resource` should by an empty struct instance such as `Post{}`. The same type will be used for constructing new elements.
func (*API) AddResourceWithController ¶
func (api *API) AddResourceWithController(prototype interface{}, source DataSource, controller Controller)
AddResourceWithController does the same as `AddResource` but also couples a custom `Controller` Use this controller to implement access control and other things that depend on the request
type Controller ¶
type Controller interface { // FindAll gets called after resource was called FindAll(r *http.Request, objs *interface{}) error // FindOne gets called after resource was called FindOne(r *http.Request, obj *interface{}) error // Create gets called before resource was called Create(r *http.Request, obj *interface{}) error // Delete gets called before resource was called Delete(r *http.Request, id string) error // Update gets called before resource was called Update(r *http.Request, obj *interface{}) error }
Controller provides more customization of each route. You can define a controller for every DataSource if needed
type DataSource ¶
type DataSource interface { // FindAll returns all objects FindAll(req Request) (interface{}, error) // FindOne returns an object by its ID FindOne(ID string, req Request) (interface{}, error) // FindMultiple returns all objects for the specified IDs FindMultiple(IDs []string, req Request) (interface{}, error) // Create a new object and return its ID Create(interface{}) (string, error) // Delete an object Delete(id string) error // Update an object Update(obj interface{}) error }
DataSource provides methods needed for CRUD.
type Error ¶
type Error struct { ID string `json:"id,omitempty"` Href string `json:"href,omitempty"` Status string `json:"status,omitempty"` Code string `json:"code,omitempty"` Title string `json:"title,omitempty"` Detail string `json:"detail,omitempty"` Path string `json:"path,omitempty"` }
Error can be used for all kind of application errors e.g. you would use it to define form errors or any other semantical application problems for more information see http://jsonapi.org/format/#errors
type HTTPError ¶
type HTTPError struct { Errors []Error `json:"errors,omitempty"` // contains filtered or unexported fields }
HTTPError is used for errors
func NewHTTPError ¶
NewHTTPError creates a new error with message and status code. `err` will be logged (but never sent to a client), `msg` will be sent and `status` is the http status code. `err` can be nil.