Documentation
¶
Index ¶
- Constants
- type Chain
- type Definition
- type Descriptor
- type Destination
- type Example
- type Method
- type Middleware
- type Operator
- type Parameter
- func AutoParameterFor(description string) Parameter
- func BodyParameterFor(description string) Parameter
- func FileParameterFor(name string, description string) Parameter
- func FormParameterFor(name string, description string) Parameter
- func HeaderParameterFor(name string, description string) Parameter
- func ParameterFor(source Source, name string, description string) Parameter
- func PathParameterFor(name string, description string) Parameter
- func PrefabParameterFor(name string, description string) Parameter
- func QueryParameterFor(name string, description string) Parameter
- type Result
- type Source
Constants ¶
const ( // acceptTypeAll indicates a accept type from http request. // It means client can receive any content. // Request content type in header "Content-Type" must not set to "*/*". // It only can exist in request header "Accept". // In most time, it locate at the last element of "Accept". // It's default value if client have not set "Accept" header. MIMEAll = "*/*" MIMENone = "" MIMEText = "text/plain" MIMEJSON = "application/json" MIMEXML = "application/xml" MIMEOctetStream = "application/octet-stream" MIMEURLEncoded = "application/x-www-form-urlencoded" MIMEFormData = "multipart/form-data" )
MIME types
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain interface {
// Continue continues to execute the next subsequent actions.
Continue(context.Context) error
}
Chain contains all subsequent actions.
type Definition ¶
type Definition struct {
// Method is definition method.
Method Method
// Consumes indicates how many content types the handler can consume.
// It will override parent descriptor's consumes.
Consumes []string
// Produces indicates how many content types the handler can produce.
// It will override parent descriptor's produces.
Produces []string
// ErrorProduces is used to generate data for error. If this field is empty,
// it means that this field equals to Produces.
// In some cases, succeessful data and error data should be generated in
// different ways.
ErrorProduces []string
// Function is a function handler. It must be func type.
Function interface{}
// Parameters describes function parameters.
Parameters []Parameter
// Results describes function retrun values.
Results []Result
// Summary is a one-line brief description of this definition.
Summary string
// Description describes the API handler.
Description string
// Examples contains many examples for the API handler.
Examples []Example
}
Definition defines an API handler.
type Descriptor ¶
type Descriptor struct {
// Path is the url path. It will inherit parent's path.
//
// If parent path is "/api/v1", current is "/some",
// It means current definitions handles "/api/v1/some".
Path string
// Consumes indicates content types that current definitions
// and child definitions can consume.
// It will override parent descriptor's consumes.
Consumes []string
// Produces indicates content types that current definitions
// and child definitions can produce.
// It will override parent descriptor's produces.
Produces []string
// Middlewares contains path middlewares.
Middlewares []Middleware
// Definitions contains definitions for current path.
Definitions []Definition
// Children is used to place sub-descriptors.
Children []Descriptor
// Description describes the usage of the path.
Description string
}
Descriptor describes a descriptor for API definitions.
func SimpleDescriptor ¶
func SimpleDescriptor(method Method, path string, f interface{}) Descriptor
SimpleDescriptor creates a simple descriptor for handler. The descriptor consumes all content types and produces all accept types.
type Destination ¶
type Destination string
Destination indicates the target type to place function results.
const ( // Meta means result will be set into the header of response. Meta Destination = "Meta" // Data means result will be set into the body of response. Data Destination = "Data" // Error means the result is an error and should be treated specially. // An error occurs indicates that there is no data to return. So the // error should be treated as data and be writed back to client. Error Destination = "Error" )
type Example ¶
type Example struct {
// Description describes the example.
Description string
// Instance is a custom data.
Instance interface{}
}
Example is just an example.
type Method ¶
type Method string
Method is an alternative of HTTP method. It's more clearer than HTTP method. A definition method binds a certain HTTP method and a success status code.
const ( // List binds to http.MethodGet and code http.StatusOK(200). List Method = "List" // Get binds to http.MethodGet and code http.StatusOK(200). Get Method = "Get" // Create binds to http.MethodPost and code http.StatusCreated(201). Create Method = "Create" // Update binds to http.MethodPut and code http.StatusOK(200). Update Method = "Update" // Patch binds to http.MethodPatch and code http.StatusOK(200). Patch Method = "Patch" // Delete binds to http.MethodDelete and code http.StatusNoContent(204). Delete Method = "Delete" // AsyncCreate binds to http.MethodPost and code http.StatusAccepted(202). AsyncCreate Method = "AsyncCreate" // AsyncUpdate binds to http.MethodPut and code http.StatusAccepted(202). AsyncUpdate Method = "AsyncUpdate" // AsyncPatch binds to http.MethodPatch and code http.StatusAccepted(202). AsyncPatch Method = "AsyncPatch" // AsyncDelete binds to http.MethodDelete and code http.StatusAccepted(202). AsyncDelete Method = "AsyncDelete" )
type Middleware ¶
Middleware describes the form of middlewares. If you want to carry on, call Chain.Continue() and pass the context.
type Operator ¶
type Operator interface {
// Kind indicates operator type.
Kind() string
// In returns the type of the only object parameter of operator.
// The type must be a concrete struct or built-in type. It should
// not be interface unless it's a file or stream reader.
In() reflect.Type
// Out returns the type of the only object result of operator.
// The type must be a concrete struct or built-in type. It should
// not be interface unless it's a file or stream reader.
Out() reflect.Type
// Operate operates an object and return one.
Operate(ctx context.Context, field string, object interface{}) (interface{}, error)
}
Operator is used to operate an object and return an replacement object.
For example:
A converter:
type ConverterForAnObject struct{}
func (c *ConverterForAnObject) Kind() {return "converter"}
func (c *ConverterForAnObject) In() reflect.Type {return definition.TypeOf(&ObjectV1{})}
func (c *ConverterForAnObject) Out() reflect.Type {return definition.TypeOf(&ObjectV2{})}
func (c *ConverterForAnObject) Operate(ctx context.Context, object interface{}) (interface{}, error) {
objV2, err := convertObjectV1ToObjectV2(object.(*ObjectV1))
return objV2, err
}
A validator:
type ValidatorForAnObject struct{}
func (c *ValidatorForAnObject) Kind() {return "validator"}
func (c *ValidatorForAnObject) In() reflect.Type {return definition.TypeOf(&Object{})}
func (c *ValidatorForAnObject) Out() reflect.Type {return definition.TypeOf(&Object{})}
func (c *ValidatorForAnObject) Operate(ctx context.Context, object interface{}) (interface{}, error) {
if err := validate(object.(*Object)); err != nil {
return nil, err
}
return object, nil
}
func NewOperator ¶
func NewOperator(kind string, in, out reflect.Type, f func(ctx context.Context, field string, object interface{}) (interface{}, error)) Operator
NewOperator creates operator by function. function must has signature:
func f(context.Context, AnyType) (AnyType, error)
AnyType can be any type in go. But struct type and built-in data type is recommended.
func OperatorFunc ¶
OperatorFunc creates operator by function. function must has signature:
func f(context.Context, string, AnyType) (AnyType, error)
The second parameter is a string that is used to identify field. AnyType can be any type in go. But struct type and built-in data type is recommended.
type Parameter ¶
type Parameter struct {
// Source is the parameter value generated from.
Source Source
// Name is the name to get value from a request.
// ex. a query name, a header key, etc.
Name string
// Default value is used when a request does not provide a value
// for the parameter.
Default interface{}
// Operators can modify and validate the target value.
// Parameter value is passed to the first operator, then
// previous operator's result is as next operator's parameter.
// The result of last operator will be passed to target function.
Operators []Operator
// Description describes the parameter.
Description string
}
Parameter describes a function parameter.
func AutoParameterFor ¶
AutoParameterFor creates a path parameter
func BodyParameterFor ¶
BodyParameterFor creates a path parameter
func FileParameterFor ¶
FileParameterFor creates a path parameter
func FormParameterFor ¶
FormParameterFor creates a path parameter
func HeaderParameterFor ¶
HeaderParameterFor creates a path parameter
func ParameterFor ¶
ParameterFor creates a simple parameter.
func PathParameterFor ¶
PathParameterFor creates a path parameter
func PrefabParameterFor ¶
PrefabParameterFor creates a path parameter
func QueryParameterFor ¶
QueryParameterFor creates a path parameter
type Result ¶
type Result struct {
// Destination is the target for the result. Different types make different behavior.
Destination Destination
// Operators can modify the result value.
// Result value is passed to the first operator, then
// previous operator's result is as next operator's parameter.
// The result of last operator will be passed to destination handler.
Operators []Operator
// Description describes the result.
Description string
}
Result describes how to handle a result from function results.
func DataErrorResults ¶
DataErrorResults returns the most frequently-used results. Definition function should have two results. The first is any type for data, and the last is error.
func DataResultFor ¶
DataResultFor creates data result.
func MetaResultFor ¶
MetaResultFor creates meta result.
func ResultFor ¶
func ResultFor(dest Destination, description string) Result
ResultFor creates a simple result.
type Source ¶
type Source string
Source indicates which place a value is from.
const ( // Path means value is from URL path. Path Source = "Path" // Query means value is from URL query string. Query Source = "Query" // Header means value is from request header. Header Source = "Header" // Form means value is from request body and content type must be // "application/x-www-form-urlencoded" and "multipart/form-data". Form Source = "Form" // File means value is from request body and content type must be // "multipart/form-data". File Source = "File" // Body means value is from request body. Body Source = "Body" // Auto identifies a struct and generate field values by field tag. // // Tag name is "source". Its value format is "Source,Name". // // ex. // type Example struct { // Start int `source:"Query,start"` // ContentType string `source:"Header,Content-Type"` // } Auto Source = "Auto" // Prefab means value is from a prefab generator. // A prefab combines data to generate value. Prefab Source = "Prefab" )