hateoas

package
v0.0.0-...-6899a91 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2019 License: BSD-3-Clause Imports: 12 Imported by: 20

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorCreated = errorCreated("created")

ErrorCreated is raised when no error occurs but a resource has been created (tonic single-status code workaround)

View Source
var ErrorGone = errorGone("gone")

ErrorGone is raised when a former resource has been requested but no longer exist

Functions

func AddToBasePath

func AddToBasePath(basePath string) gin.HandlerFunc

AddToBasePath add a subpath to the BasePath stored in the gin context, in order to build hateoas links

func BaseURL

func BaseURL(c *gin.Context) string

BaseURL returns the base path that has been used to access current resource

func CheckFilter

func CheckFilter(criteria map[string]interface{}) (map[string]interface{}, map[string]interface{}, map[string]interface{})

CheckFilter analyse filters

func ErrorHook

func ErrorHook(tonicErrorHook tonic.ErrorHook) tonic.ErrorHook

ErrorHook Convert repository errors in juju errors

func HandlerFindBy

func HandlerFindBy(repository ListableRepository) gin.HandlerFunc

HandlerFindBy returns all resources matching path params

func HandlerFindByPage

func HandlerFindByPage(repository PageableRepository) gin.HandlerFunc

HandlerFindByPage returns a filtered and paginated resource list

func HandlerFindOneBy

func HandlerFindOneBy(repository ListableRepository) gin.HandlerFunc

HandlerFindOneBy returns the first resource matching path params

func HandlerIndex

func HandlerIndex(links ...ResourceLink) gin.HandlerFunc

HandlerIndex generates a simple hateoas index

func HandlerRemoveAll

func HandlerRemoveAll(repository TruncatableRepository) gin.HandlerFunc

HandlerRemoveAll removes a whole collection

func HandlerRemoveOneBy

func HandlerRemoveOneBy(repository SavableRepository) gin.HandlerFunc

HandlerRemoveOneBy removes a given resource

func InlineFilter

func InlineFilter(db *gorm.DB, criteria map[string]interface{}) *gorm.DB

InlineFilter filter inline filters

func IsEntityDoesNotExistError

func IsEntityDoesNotExistError(err error) bool

IsEntityDoesNotExistError returns true if err is an EntityDoesNotExistError

func JSONBFilter

func JSONBFilter(db *gorm.DB, criteria map[string]interface{}) *gorm.DB

JSONBFilter filter jsonb

func NewEntityDoesNotExistError

func NewEntityDoesNotExistError(entity interface{}, criteria map[string]interface{}) error

NewEntityDoesNotExistError is a helper to create an EntityDoesNotExistError

func NewUnsupportedEntityError

func NewUnsupportedEntityError(expected, actual interface{}) error

NewUnsupportedEntityError is a helper to create an UnsupportedEntityError

func NewUnsupportedIndexError

func NewUnsupportedIndexError(field string, supported ...string) error

NewUnsupportedIndexError is a helper to create an UnsupportedIndexError

func RenderHookWrapper

func RenderHookWrapper(hook tonic.RenderHook) tonic.RenderHook

RenderHookWrapper handles hateoas entity conversion

func ToJSON

func ToJSON(data interface{}) string

ToJSON simple indented json

Types

type BaseRepository

type BaseRepository interface {
	GetType() reflect.Type
}

BaseRepository defines a normal repository

type Entity

type Entity interface {
	GetID() string
	SetID(string) error
}

Entity defines and identifiable REST entity

func FindByPath

func FindByPath(c *gin.Context, repository ListableRepository) (Entity, error)

FindByPath find one entity in the given repository, using paths parameters as matching criteria

type EntityDoesNotExistError

type EntityDoesNotExistError struct {
	EntityName string
	Criteria   map[string]interface{}
}

EntityDoesNotExistError is raised when a repository try to read a non existing entity

func (EntityDoesNotExistError) Error

func (err EntityDoesNotExistError) Error() string

Error implements error interface for EntityDoesNotExistError

type InternalError

type InternalError struct {
	Message string
	Detail  string
}

InternalError all internal error

func (*InternalError) Error

func (err *InternalError) Error() string

Error implements error interface

type ListableRepository

type ListableRepository interface {
	BaseRepository
	FindBy(map[string]interface{}) (interface{}, error)
	FindOneBy(map[string]interface{}) (Entity, error)
}

ListableRepository defines a repository where one can list entities

type Page

type Page struct {
	Content       interface{}
	TotalElements int      `json:"totalElements"`
	IDs           []string `json:"ids,omitempty"`
	BasePath      string   `json:"-"`
	Pageable      Pageable
}

Page defines a page

func NewPage

func NewPage(pageable Pageable, defaultPageSize int, basePath string) Page

NewPage initialize an empty resource page

func (Page) ToResources

func (page Page) ToResources(baseURL string) PagedResources

ToResources convert receiver page to a hateoas representation

type Pageable

type Pageable struct {
	Page      int    `json:"page" form:"page"`
	Size      int    `json:"size" form:"size"`
	Sort      string `json:"sort" form:"sort"`
	IndexedBy string `json:"indexedBy" form:"indexedBy" default:""`
}

Pageable defines pagination criteria

func (Pageable) GetGormSortClause

func (p Pageable) GetGormSortClause() interface{}

GetGormSortClause returns the SQL-escaped sort clause

func (Pageable) GetOffset

func (p Pageable) GetOffset() int

GetOffset returns the page offset

func (Pageable) GetSortClause

func (p Pageable) GetSortClause() string

GetSortClause returns the SQL-escaped sort clause

type PageableRepository

type PageableRepository interface {
	BaseRepository
	FindPageBy(Pageable, map[string]interface{}) (Page, error)
}

PageableRepository defines a repository that handles pagination

type PagedResources

type PagedResources struct {
	Content      interface{}    `json:"content" binding:"-"`
	PageMetadata pageMetadata   `json:"pageMetadata" binding:"-"`
	Links        []ResourceLink `json:"_links" binding:"-"`
}

PagedResources defines a REST representation of paged contents

type Resource

type Resource struct {
	Links []ResourceLink `json:"_links,omitempty" gorm:"-" binding:"-"`
}

Resource defines a REST resources links

type ResourceLink struct {
	Rel  string `json:"rel"`
	Href string `json:"href"`
}

ResourceLink defines relationship between resources

type Resourceable

type Resourceable interface {
	ToResource(baseURL string)
	GetSelfURL(baseURL string) string
}

Resourceable defines a linkable hateoas resource

type SavableRepository

type SavableRepository interface {
	ListableRepository
	Save(Entity) error
	Remove(interface{}) error
}

SavableRepository defines a repository where one can persist or remove entities

type SoftDeletableEntity

type SoftDeletableEntity interface {
	Entity
	GetDeletedAt() *time.Time
}

SoftDeletableEntity defines a soft deletable REST entity

type SoftDeletableRepository

type SoftDeletableRepository interface {
	BaseRepository
	FindOneByUnscoped(criteria map[string]interface{}) (SoftDeletableEntity, error)
}

SoftDeletableRepository defines a repository that handles soft delete

type TruncatableRepository

type TruncatableRepository interface {
	BaseRepository
	Truncate() error
}

TruncatableRepository defines a repository that handles truncation

type UnsupportedEntityError

type UnsupportedEntityError struct {
	Expected string
	Actual   string
}

UnsupportedEntityError is raised when a repository method is called with a wrong type

func (UnsupportedEntityError) Error

func (err UnsupportedEntityError) Error() string

Error implements error interface for UnsupportedEntityError

type UnsupportedIndexError

type UnsupportedIndexError struct {
	Field     string
	Supported []string
}

UnsupportedIndexError is raised when a impossible indexation was requested

func (UnsupportedIndexError) Error

func (err UnsupportedIndexError) Error() string

Error implements error interface for UnsupportedIndexError

Jump to

Keyboard shortcuts

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