optic

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2021 License: MIT Imports: 14 Imported by: 0

README

optic

Tonic handler style for Fiber.

The code base is from Tonic and then modified to support Fiber handler.

Documentation

Index

Constants

View Source
const (
	QueryTag      = "query"
	PathTag       = "path"
	HeaderTag     = "header"
	EnumTag       = "enum"
	RequiredTag   = "required"
	DefaultTag    = "default"
	ValidationTag = "validate"
	ExplodeTag    = "explode"
)

Fields tags used by optic.

View Source
const DefaultMaxBodyBytes = 256 * 1024

DefaultMaxBodyBytes is the maximum allowed size of a request body in bytes.

Variables

This section is empty.

Functions

func DefaultBindHeaderHook

func DefaultBindHeaderHook(c *fiber.Ctx, v reflect.Value) error

func DefaultBindPathHook

func DefaultBindPathHook(c *fiber.Ctx, v reflect.Value) error

func DefaultBindQueryHook

func DefaultBindQueryHook(c *fiber.Ctx, v reflect.Value) error

func DefaultBindingHook

func DefaultBindingHook(c *fiber.Ctx, v reflect.Value) error

DefaultBindingHook is the default binding hook. It uses Gin JSON binding to bind the body parameters of the request to the input object of the handler. Ir teturns an error if Gin binding fails.

func DefaultErrorHook

func DefaultErrorHook(c *fiber.Ctx, e error) (int, interface{})

DefaultErrorHook is the default error hook. It returns a StatusBadRequest with a payload containing the error message.

func DefaultExecHook

func DefaultExecHook(c *fiber.Ctx, h fiber.Handler, fname string) error

DefaultExecHook is the default exec hook. It simply executes the wrapping gin-handler with the given context.

func DefaultRenderHook

func DefaultRenderHook(c *fiber.Ctx, statusCode int, payload interface{})

DefaultRenderHook is the default render hook. It marshals the payload to JSON, or returns an empty body if the payload is nil. If Gin is running in debug mode, the marshalled JSON is indented.

func Deprecated

func Deprecated(b bool) func(*Route)

Deprecated set the deprecated flag of a route.

func Description

func Description(s string) func(*Route)

Description set the description of a route.

func GetRoutes

func GetRoutes() map[string]*Route

GetRoutes returns the routes handled by a optic-enabled handler.

func Handler

func Handler(h interface{}, status int, options ...func(*Route)) fiber.Handler

Handler returns a Fiber HandlerFunc that wraps the handler passed in parameters. The handler may use the following signature:

func(*fiber.Ctx, [input object ptr]) ([output object], error)

Input and output objects are both optional. As such, the minimal accepted signature is:

func(*fiber.Ctx) error

The wrapping fiber-handler will bind the parameters from the query-string, path, body and headers, and handle the errors.

Handler will panic if the handler or its input/output values are of incompatible type.

func MediaType

func MediaType() string

MediaType returns the current media type (MIME) used by the actual render hook.

func ParseTagKey

func ParseTagKey(tag string) (string, error)

ParseTagKey parses the given struct tag key and return the name of the field

func RegisterValidation

func RegisterValidation(tagName string, validationFunc validator.Func) error

RegisterValidation registers a custom validation on the validator.Validate instance of the package NOTE: calling this function may instantiate the validator itself. NOTE: this function is not thread safe, since the validator validation registration isn't

func SetBindHeaderHook

func SetBindHeaderHook(bh BindHook)

SetBindQueryHook sets the given hook as the default binding hook.

func SetBindHook

func SetBindHook(bh BindHook)

SetBindHook sets the given hook as the default binding hook.

func SetBindPathHook

func SetBindPathHook(bh BindHook)

SetBindQueryHook sets the given hook as the default binding hook.

func SetBindQueryHook

func SetBindQueryHook(bh BindHook)

SetBindQueryHook sets the given hook as the default binding hook.

func SetErrorHook

func SetErrorHook(eh ErrorHook)

SetErrorHook sets the given hook as the default error handling hook.

func SetExecHook

func SetExecHook(eh ExecHook)

SetExecHook sets the given hook as the default execution hook.

func SetRenderHook

func SetRenderHook(rh RenderHook, mt string)

SetRenderHook sets the given hook as the default rendering hook. The media type is used to generate the OpenAPI specification.

func Summary

func Summary(s string) func(*Route)

Summary set the summary of a route.

func Tags

func Tags(tags []string) func(*Route)

Tags sets the tags of a route.

Types

type BindError

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

BindError is an error type returned when optic fails to bind parameters, to differentiate from errors returned by the handlers.

func (BindError) Error

func (be BindError) Error() string

Error implements the builtin error interface for BindError.

func (BindError) ValidationErrors

func (be BindError) ValidationErrors() validator.ValidationErrors

ValidationErrors returns the errors from the validate process.

type BindHook

type BindHook func(*fiber.Ctx, reflect.Value) error

BindHook is the hook called by the wrapping gin-handler when binding an incoming request to the optic-handler's input object.

func GetBindHeaderHook

func GetBindHeaderHook() BindHook

GetBindQueryHook returns the current bind hook.

func GetBindHook

func GetBindHook() BindHook

GetBindHook returns the current bind hook.

func GetBindPathHook

func GetBindPathHook() BindHook

GetBindQueryHook returns the current bind hook.

func GetBindQueryHook

func GetBindQueryHook() BindHook

GetBindQueryHook returns the current bind hook.

type ErrorHook

type ErrorHook func(*fiber.Ctx, error) (int, interface{})

ErrorHook lets you interpret errors returned by your handlers. After analysis, the hook should return a suitable http status code and and error payload. This lets you deeply inspect custom error types.

func GetErrorHook

func GetErrorHook() ErrorHook

GetErrorHook returns the current error hook.

type ExecHook

type ExecHook func(*fiber.Ctx, fiber.Handler, string) error

An ExecHook is the func called to handle a request. The default ExecHook simply calle the wrapping gin-handler with the gin context.

func GetExecHook

func GetExecHook() ExecHook

GetExecHook returns the current execution hook.

type RenderHook

type RenderHook func(*fiber.Ctx, int, interface{})

RenderHook is the last hook called by the wrapping gin-handler before returning. It takes the Gin context, the HTTP status code and the response payload as parameters. Its role is to render the payload to the client to the proper format.

func GetRenderHook

func GetRenderHook() RenderHook

GetRenderHook returns the current render hook.

type Route

type Route struct {
	fiber.Route
	// contains filtered or unexported fields
}

A Route contains information about a optic-enabled route.

func GetRouteByHandler

func GetRouteByHandler(h fiber.Handler, ctx *fiber.Ctx) (*Route, error)

GetRouteByHandler returns the route information of the given wrapped handler.

func (*Route) GetDefaultStatusCode

func (r *Route) GetDefaultStatusCode() int

GetDefaultStatusCode returns the default status code of the route.

func (*Route) GetDeprecated

func (r *Route) GetDeprecated() bool

GetDeprecated returns the deprecated flag of the route.

func (*Route) GetDescription

func (r *Route) GetDescription() string

GetDescription returns the description of the route.

func (*Route) GetHandler

func (r *Route) GetHandler() reflect.Value

GetHandler returns the handler of the route.

func (*Route) GetPath

func (r *Route) GetPath() string

GetPath returns the path of the route.

func (*Route) GetSummary

func (r *Route) GetSummary() string

GetSummary returns the summary of the route.

func (*Route) GetTags

func (r *Route) GetTags() []string

GetTags generates a list of tags for the swagger spec from one route definition. It uses the first chunk of the path of the route as the tag (for example, in /foo/bar it will return the "foo" tag), unless specific tags have been defined with optic.Tags

func (*Route) GetVerb

func (r *Route) GetVerb() string

GetVerb returns the HTTP verb of the route.

func (*Route) HandlerName

func (r *Route) HandlerName() string

HandlerName returns the name of the route handler.

func (*Route) HandlerNameWithPackage

func (r *Route) HandlerNameWithPackage() string

HandlerNameWithPackage returns the full name of the rout handler with its package path.

func (*Route) InputType

func (r *Route) InputType() reflect.Type

InputType returns the input type of the handler. If the type is a pointer to a concrete type, it is dereferenced.

func (*Route) OutputType

func (r *Route) OutputType() reflect.Type

OutputType returns the output type of the handler. If the type is a pointer to a concrete type, it is dereferenced.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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