Documentation
¶
Overview ¶
Package swipe is a code generation tool that automates the creation of repetitively used code. Configuration parameters are presented in Swipe as parameters of the Golang function, using explicit initialization instead of global variables or reflections.
Swipe generates code using an option: a function that calls functions that define the generation parameters. Using Swipe, you describe the generation parameters in the option, and then Swipe generates the code.
1. The "function as an option" approach is used to configure generation.
2. All code that is not associated with the generation parameters will not be copied to the generated file.
3. Function with a `swipe.Build` option inserted in the body. `swipe.Build` will not be transferred to the generated code.
If you want the generate code, you can run:
swipe ./pkg/...
Full example:
// +build swipe package jsonrpc import ( "github.com/swipe-io/swipe/v2/fixtures/service" . "github.com/swipe-io/swipe/v2/pkg/swipe" ) func Swipe() { Build( Service( (*service.Interface)(nil), Transport("http", ClientEnable(), Openapi( OpenapiOutput("/../../docs"), OpenapiVersion("1.0.0"), ), ), Logging(), Instrumenting(), ), ) }
Index ¶
- Constants
- func Build(Option)
- type ConfigEnvOption
- type GatewayOption
- type GatewayServiceMethodOption
- type GatewayServiceOption
- type InstrumentingOption
- type JSONRPCOption
- type LoggingOption
- type MethodOption
- func ClientDecodeResponseFunc(interface{}) MethodOption
- func ClientEncodeRequestFunc(interface{}) MethodOption
- func HeaderVars([]string) MethodOption
- func Method(string) MethodOption
- func Path(string) MethodOption
- func QueryVars([]string) MethodOption
- func ServerDecodeRequestFunc(interface{}) MethodOption
- func ServerEncodeResponseFunc(interface{}) MethodOption
- func WrapResponse(string) MethodOption
- type OpenapiOption
- func OpenapiContact(name, email, url string) OpenapiOption
- func OpenapiInfo(title, description, version string) OpenapiOption
- func OpenapiLicence(name, url string) OpenapiOption
- func OpenapiOutput(string) OpenapiOption
- func OpenapiServer(description, url string) OpenapiOption
- func OpenapiTags(methods []interface{}, tags []string) OpenapiOption
- type OpenapiServerOption
- type OpenapiServersOption
- type Option
- type ReadmeOption
- type ServiceOption
- func Instrumenting(namespace, subsystem string, opts ...InstrumentingOption) ServiceOption
- func Logging(...LoggingOption) ServiceOption
- func Name(string) ServiceOption
- func Readme(outputDir string, opts ...ReadmeOption) ServiceOption
- func Transport(protocol string, opts ...TransportOption) ServiceOption
- type TransportOption
- func ClientEnable() TransportOption
- func FastEnable() TransportOption
- func JSONRPC(...JSONRPCOption) TransportOption
- func MarkdownDoc(outputDir string) TransportOption
- func MethodDefaultOptions(...MethodOption) TransportOption
- func MethodOptions(signature interface{}, opts ...MethodOption) TransportOption
- func Openapi(...OpenapiOption) TransportOption
- func ServerDisabled() TransportOption
Constants ¶
const Version = "v2.0.0-alpha.1"
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ConfigEnvOption ¶
type ConfigEnvOption string
A ConfigEnvOption is an option env config.
func ConfigMarkdownDoc ¶
func ConfigMarkdownDoc(string) ConfigEnvOption
ConfigMarkdownDoc enable markdown doc generate.
func FuncName ¶
func FuncName(string) ConfigEnvOption
FuncName sets name of the function to load the configuration, default is "LoadConfig".
type GatewayOption ¶
type GatewayOption string
func GatewayService ¶
func GatewayService(iface interface{}, opts ...GatewayServiceOption) GatewayOption
type GatewayServiceMethodOption ¶
type GatewayServiceMethodOption string
func GatewayBalancer ¶
func GatewayBalancer(string) GatewayServiceMethodOption
type GatewayServiceOption ¶
type GatewayServiceOption string
func GatewayServiceMethod ¶
func GatewayServiceMethod(signature interface{}, opts ...GatewayServiceMethodOption) GatewayServiceOption
type InstrumentingOption ¶
type InstrumentingOption string
A InstrumentingOption is an option metrics.
type JSONRPCOption ¶
type JSONRPCOption interface{}
A JSONRPCOption is an option JSON RPC.
func JSONRPCPath ¶
func JSONRPCPath(string) JSONRPCOption
JSONRPCPath sets the end point for transport.
type MethodOption ¶
type MethodOption string
A MethodOption is an option method.
func ClientDecodeResponseFunc ¶
func ClientDecodeResponseFunc(interface{}) MethodOption
ClientDecodeResponseFunc sets a function to extract the user's domain response object from the response object.
func ClientEncodeRequestFunc ¶
func ClientEncodeRequestFunc(interface{}) MethodOption
ClientEncodeRequestFunc sets the function to encode the passed request object into an object.
func HeaderVars ¶
func HeaderVars([]string) MethodOption
HeaderVars sets the key/value array to get method values from headers, where the key is the name of the method parameter, and the value is the name of the header.
func Path ¶
func Path(string) MethodOption
Path sets http path, default is lowecase method name with the prefix "/", for example: the Get method will look like " /get".
func QueryVars ¶
func QueryVars([]string) MethodOption
QueryVars sets the key/value array to get method values from query args, where the key is the name of the method parameter, and the value is the name of the query args.
func ServerDecodeRequestFunc ¶
func ServerDecodeRequestFunc(interface{}) MethodOption
ServerDecodeRequestFunc sets a function to extract the user's domain request object from the request object.
func ServerEncodeResponseFunc ¶
func ServerEncodeResponseFunc(interface{}) MethodOption
ServerEncodeResponseFunc sets the encoding function of the passed response object to the response writer.
func WrapResponse ¶
func WrapResponse(string) MethodOption
WrapResponse wrap the response from the server to an object, for example if you want to return as:
{data: { you responce data }}
need to add option:
...code here... WrapResponse("data") ... code here ...
type OpenapiOption ¶
type OpenapiOption string
A OpenapiOption is an option for openapi doc.
func OpenapiContact ¶
func OpenapiContact(name, email, url string) OpenapiOption
OpenapiContact sets openapi contact.
func OpenapiInfo ¶
func OpenapiInfo(title, description, version string) OpenapiOption
OpenapiInfo sets info.
func OpenapiLicence ¶
func OpenapiLicence(name, url string) OpenapiOption
OpenapiLicence sets openapi licence.
func OpenapiOutput ¶
func OpenapiOutput(string) OpenapiOption
OpenapiOutput sets output directory, path relative to the file, default is "./".
func OpenapiServer ¶
func OpenapiServer(description, url string) OpenapiOption
OpenapiServer sets openapi server.
func OpenapiTags ¶
func OpenapiTags(methods []interface{}, tags []string) OpenapiOption
OpenapiTags sets docs tags for method.
type OpenapiServerOption ¶
type OpenapiServerOption string
A OpenapiServerOption is an openapi concrete server option.
type OpenapiServersOption ¶
type OpenapiServersOption string
A OpenapiServersOption is an openapi servers option.
type Option ¶
type Option string
A Option is an option for a Swipe.
func ConfigEnv ¶
func ConfigEnv(optionsStruct interface{}, opts ...ConfigEnvOption) Option
ConfigEnv option for config generation.
To generate a configuration loader, you can use the swipe.ConfigEnv option. The optionsStruct parameter is a pointer to the configuration structure.
The option can work with all primitives, including datetime, and an array of primitives. The option supports nested structures. To use the default value, just specify it as a value in the structure.
Default func name is `LoadConfig`.
You can use structure tags to control generation:
env - name of environment var, options: `required`. flag - name of flag, enable as the console flag. desc - description for String function.
func Gateway ¶
func Gateway(services ...GatewayOption) Option
func Service ¶
func Service(iface interface{}, opts ...ServiceOption) Option
Service a option that defines the generation of transport, metrics, tracing, and logging for gokit. Given iface is nil pointer interface, for example:
(*pkg.Iface)(nil)
type ReadmeOption ¶
type ReadmeOption string
func ReadmeTemplate ¶
func ReadmeTemplate(path string) ReadmeOption
ReadmeTemplate set markdown template path.
type ServiceOption ¶
type ServiceOption string
A ServiceOption is an option service.
func Instrumenting ¶
func Instrumenting(namespace, subsystem string, opts ...InstrumentingOption) ServiceOption
Instrumenting a option enabled instrumenting (collect metrics) middleware.
func Logging ¶
func Logging(...LoggingOption) ServiceOption
Logging a option enabled logging middleware.
func Readme ¶
func Readme(outputDir string, opts ...ReadmeOption) ServiceOption
Readme enable for generate readme markdown for service.
func Transport ¶
func Transport(protocol string, opts ...TransportOption) ServiceOption
Transport a option that defines the transport generation settings.
Swipe generates a method for creating an transport handler using the following template:
MakeHandler<transportType><projectName><serviceName>
<transportType> is REST or JSONRPC.
type TransportOption ¶
type TransportOption string
A TransportOption is an option gokit transport.
func ClientEnable ¶
func ClientEnable() TransportOption
ClientEnable enable generate client for the selected transport.
func FastEnable ¶
func FastEnable() TransportOption
FastEnable enable use valyala/fasthttp instead net/http package.
Supported in both REST and JSON RPC.
func JSONRPC ¶
func JSONRPC(...JSONRPCOption) TransportOption
JSONRPC enabled use JSON RPC instead of REST.
func MarkdownDoc ¶
func MarkdownDoc(outputDir string) TransportOption
MarkdownDoc enable for generate markdown JSON RPC doc for JS client.
func MethodDefaultOptions ¶
func MethodDefaultOptions(...MethodOption) TransportOption
MethodDefaultOptions option for defining for all methods default settings.
func MethodOptions ¶
func MethodOptions(signature interface{}, opts ...MethodOption) TransportOption
MethodOptions option for defining method settings. Given signature is interface method, for example:
pkg.Iface.Create
func Openapi ¶
func Openapi(...OpenapiOption) TransportOption
Openapi generate openapi documentation.
func ServerDisabled ¶
func ServerDisabled() TransportOption
ServerDisabled disable generate http server.