jsonapi

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2016 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package jsonapi implements components to build JSON APIs with fire.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fatal

func Fatal(err error) error

Fatal wraps an error and marks it as fatal.

Types

type Action

type Action int

An Action describes the currently called action on the API.

const (
	List Action
	Find
	Create
	Update
	Delete
)

All the available actions.

func (Action) Read

func (a Action) Read() bool

Read will return true when this action does only read data.

func (Action) Write

func (a Action) Write() bool

Write will return true when this action does write data.

type Callback

type Callback func(*Context) error

A Callback can be an Authorizer or Validator an is called during execution of a controller.

Note: If the callback returns an error wrapped using Fatal() the API returns an InternalServerError status and the error will be logged. All other errors are serialized to an error object and returned.

func Combine

func Combine(callbacks ...Callback) Callback

Combine combines multiple callbacks to one.

Note: Execution will be stopped if a callback returns and error.

func DependentResourcesValidator

func DependentResourcesValidator(resources fire.Map) Callback

DependentResourcesValidator counts documents in the supplied collections and returns an error if some get found. This callback is meant to protect resources from breaking relations when requested to be deleted.

Resources are defined by passing pairs of collections and fields where the field must be a database field of the target resource model:

DependentResourcesValidator(fire.Map{
	"posts": "user_id",
	"comments": "user_id",
})

func MatchingReferencesValidator

func MatchingReferencesValidator(collection, reference string, matcher fire.Map) Callback

MatchingReferencesValidator compares the model with a related model and checks if certain references are shared.

The target model is defined by passing its collection and the referencing field on the current model. The matcher is defined by passing pairs of database fields on the target and current model:

MatchingReferencesValidator("posts", "post_id", fire.Map{
	"user_id": "user_id",
})

func ModelValidator

func ModelValidator() Callback

ModelValidator uses the govalidator package to validate the model based on the "valid" struct tags.

func ProtectedAttributesValidator

func ProtectedAttributesValidator(attributes fire.Map) Callback

ProtectedAttributesValidator compares protected attributes against their default (during Create) or stored value (during Update) and returns and error if they have been changed.

Attributes are defined by passing pairs of fields and default values:

ProtectedAttributesValidator(fire.Map{
	"title": "A fixed title",
})

func VerifyReferencesValidator

func VerifyReferencesValidator(references fire.Map) Callback

VerifyReferencesValidator makes sure all references in the document are existing by counting on the related collections.

References are defined by passing pairs of fields and collections where the field must be a database field on the resource model:

VerifyReferencesValidator(fire.Map{
	"post_id": "posts",
	"user_id": "users",
})

type Context

type Context struct {
	// The current action in process.
	Action Action

	// The query that will be used during FindAll, FindOne, Update or Delete.
	// On FindOne, Update and Delete, the "_id" key is preset to the document ID.
	// On FindAll all field filters and relationship filters are preset.
	Query bson.M

	// The Model that will be saved during Create or Update.
	Model model.Model

	// The sorting that will be used during FindAll.
	Sorting []string

	// The store that is used to retrieve and persist the model.
	Store *model.Store

	// The underlying JSON API request.
	Request *jsonapi.Request

	// The underlying echo context.
	Echo echo.Context
	// contains filtered or unexported fields
}

A Context provides useful contextual information.

func (*Context) Original

func (c *Context) Original() (model.Model, error)

Original will return the stored version of the model. This method is intended to be used to calculate the changed fields during an Update action.

Note: The method will directly return any mgo errors and panic if being used during any other action than Update.

type Controller

type Controller struct {
	// The model that this controller should provide (e.g. &Foo{}).
	Model model.Model

	// FilterableFields defines the attributes that are filterable.
	FilterableFields []string

	// SortableFields defines the attributes that are sortable.
	SortableFields []string

	// The store that is used to retrieve and persist the model.
	Store *model.Store

	// The Authorizer is run on all actions. Will return an Unauthorized status
	// if an user error is returned.
	Authorizer Callback

	// The Validator is run to validate Create, Update and Delete actions. Will
	// return a Bad Request status if an user error is returned.
	Validator Callback

	// The NoList property can be set to true if the resource is only listed
	// through relationships from other resources. This is useful for
	// resources like comments that should never listed without a relationship.
	NoList bool

	// The ListLimit can be set to a value higher than 1 to enforce paginated
	// responses and restrain the page size to be within one and the limit.
	ListLimit int
	// contains filtered or unexported fields
}

A Controller provides a JSON API based interface to a model.

Note: Controllers must not be modified after adding to an application.

type Group

type Group struct {
	// contains filtered or unexported fields
}

A Group manages access to multiple controllers and their interconnections.

func NewGroup

func NewGroup(prefix string) *Group

NewGroup creates and returns a new group.

Note: You should pass the full URL prefix of the API to allow proper generation of resource links.

func (*Group) Add

func (g *Group) Add(controllers ...*Controller)

Add will add a controller to the group.

func (*Group) Describe

func (g *Group) Describe() fire.ComponentInfo

Describe implements the fire.Component interface.

func (*Group) Register

func (g *Group) Register(router *echo.Echo)

Register implements the fire.RoutableComponent interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL