swipe

package
v1.0.0-alpha13 Latest Latest
Warning

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

Go to latest
Published: May 11, 2020 License: MIT Imports: 0 Imported by: 0

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 ads that are not related to options found in the file will 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/your_package/your_file.go

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(Option)

Build the basic option for defining the generation.

Types

type ConfigEnvOption

type ConfigEnvOption string

A ConfigEnvOption is an option env config.

func FuncName

func FuncName(string) ConfigEnvOption

FuncName sets name of the function to load the configuration, default is "LoadConfig".

func Struct

func Struct(interface{}) ConfigEnvOption

Struct sets the structure where configuration parameters will be loaded.

type InstrumentingOption

type InstrumentingOption string

A InstrumentingOption is an option metrics.

func Namespace

func Namespace(string) InstrumentingOption

Namespace a namespace metric.

func Subsystem

func Subsystem(string) InstrumentingOption

Subsystem a subsystem metric.

type JsonRPCOption

type JsonRPCOption string

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 DecodeRequest

func DecodeRequest(interface{}) MethodOption

func EncodeResponse

func EncodeResponse(interface{}) MethodOption

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 Method

func Method(string) MethodOption

Method sets http method, default is GET.

func Path

func Path(string) MethodOption

HTTPPath 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 Signature

func Signature(interface{}) MethodOption

Signature signature of the interface method.

type OpenapiOption

type OpenapiOption string

A OpenapiOption is an option for openapi doc.

func OpenapiContactEmail

func OpenapiContactEmail(string) OpenapiOption

OpenapiContactEmail sets contact email.

func OpenapiContactName

func OpenapiContactName(string) OpenapiOption

OpenapiContactName sets contact name.

func OpenapiContactURL

func OpenapiContactURL(string) OpenapiOption

OpenapiContactURL sets contact URL.

func OpenapiDescription

func OpenapiDescription(string) OpenapiOption

OpenapiDescription sets description.

func OpenapiLicenceName

func OpenapiLicenceName(string) OpenapiOption

OpenapiLicenceName sets licence name.

func OpenapiLicenceURL

func OpenapiLicenceURL(string) OpenapiOption

OpenapiLicenceURL sets licence URL.

func OpenapiOutput

func OpenapiOutput(string) OpenapiOption

OpenapiOutput sets output directory, path relative to the file, default is "./".

Example
Build(
	Service(
		Interface((*service.Service)(nil)),
		Transport(
			Protocol("http"),

			Openapi(
				OpenapiOutput("../../docs"),
			),
		),
	),
)
Output:

func OpenapiServer

func OpenapiServer(...OpenapiServerOption) OpenapiOption

func OpenapiServerDescription

func OpenapiServerDescription(string) OpenapiOption

func OpenapiServerURL

func OpenapiServerURL(string) OpenapiOption

func OpenapiServers

func OpenapiServers(...OpenapiServersOption) OpenapiOption

func OpenapiTitle

func OpenapiTitle(string) OpenapiOption

OpenapiTitle sets title.

func OpenapiVersion

func OpenapiVersion(string) OpenapiOption

OpenapiVersion sets version.

type OpenapiServerOption

type OpenapiServerOption string

type OpenapiServersOption

type OpenapiServersOption string

type Option

type Option string

A Option is an option for a Swipe.

func ConfigEnv

func ConfigEnv(...ConfigEnvOption) Option

ConfigEnv option for config generation. To generate a configuration loader, you can use the `swipe.ConfigEnv` directive. The first parameter is a pointer to the configuration structure, and the second parameter is the name of the configuration loader function.

The directive can work with all primitives, including datetime, and an array of primitives.

The directive 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.
Example
Build(
	ConfigEnv(
		Struct(&Config{
			BindAddr: ":9000",
		}),
		FuncName("LoadConfig"),
	),
)
Output:

func Service

func Service(...ServiceOption) Option

Service a option that defines the generation of transport, metrics, tracing, and logging for gokit.

Example

Example basic use Service option.

Build(
	Service(
		Interface((*service.Service)(nil)),
	),
)
Output:

type ServiceOption

type ServiceOption string

A ServiceOption is an option service.

func Instrumenting

func Instrumenting(...InstrumentingOption) ServiceOption

Instrumenting a option enabled instrumenting (collect metrics) middleware.

Example

Example basic use instrumenting.

Build(
	Service(
		Interface((*service.Service)(nil)),
		Transport(
			Protocol("http"),
		),
		Instrumenting(
			Namespace("api"),
			Subsystem("api"),
		),
	),
)
Output:

func Interface

func Interface(interface{}) ServiceOption

Interface a option that defines the service interface.

func Logging

func Logging() ServiceOption

Logging a option enabled logging middleware.

Example

Example basic use logging.

Build(
	Service(
		Interface((*service.Service)(nil)),
		Transport(
			Protocol("http"),
		),
		Logging(),
	),
)
Output:

func Transport

func Transport(...TransportOption) ServiceOption

Transport a option that defines the transport generation settings.

Swipe generates a method for creating an transport handler using the following `Server<transportType><packageName><serviceName>` template, transportType is REST, JSONRPC.

Example (Listener)
h, err := rest.MakeRestHandlerServiceInterface(&service.Service{}, log.NewNopLogger())
if err != nil {
	panic(err)
}
go fasthttp.ListenAndServe(":80", h)
Output:

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 MethodOptions

func MethodOptions(...MethodOption) TransportOption

MethodOptions option for defining method settings.

func NotWrapBody

func NotWrapBody() TransportOption

func Openapi

func Openapi(...OpenapiOption) TransportOption

Openapi generate openapi documentation.

Example
Build(
	Service(
		Interface((*service.Service)(nil)),
		Transport(
			Protocol("http"),

			Openapi(),
		),
	),
)
Output:

func Protocol

func Protocol(string) TransportOption

Protocol type of transport protocol, currently available only http, required.

Example
Build(
	Service(
		Interface((*service.Interface)(nil)),
		Transport(
			Protocol("http"),
		),
	),
)
Output:

func ServerDisabled

func ServerDisabled() TransportOption

ServerDisabled disable generate http server.

Jump to

Keyboard shortcuts

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