swipe

package
v1.0.0-alpha11 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: MIT Imports: 2 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 ClientOption

type ClientOption string

A ClientOption is an option for client.

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 HTTPDecodeRequest

func HTTPDecodeRequest(kithttp.DecodeRequestFunc) MethodOption

HTTPDecodeRequest sets custom gokit DecodeRequestFunc.

func HTTPEncodeResponse

func HTTPEncodeResponse(kithttp.EncodeResponseFunc) MethodOption

HTTPEncodeResponse sets custom gokit EncodeResponseFunc.

func HTTPFastDecodeRequest

func HTTPFastDecodeRequest(kitfasthttp.DecodeRequestFunc) MethodOption

HTTPFastDecodeRequest sets custom gokit DecodeRequestFunc for valyala/fasthttp, use only if you specified HTTP Fast.

func HTTPFastEncodeResponse

func HTTPFastEncodeResponse(kitfasthttp.EncodeResponseFunc) MethodOption

HTTPFastEncodeResponse sets custom gokit EncodeResponseFunc for valyala/fasthttp, use only if you specified HTTP Fast.

func HTTPFastServerOption

func HTTPFastServerOption(...kitfasthttp.ServerOption) MethodOption

HTTPFastServerOption sets custom gokit ServerOption for valyala/fasthttp, use only if you specified HTTP Fast.

func HTTPHeaderVars

func HTTPHeaderVars([]string) MethodOption

HTTPHeaderVars 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.

Example

A parameter is a key pair, where the key is the name of the method parameter, and the value is the name of the parameter in the header.

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

			MethodOption(
				Signature(service.Interface.Get),

				HTTPHeaderVars([]string{"name", "x-name"}),
			),
		),
	),
)
Output:

func HTTPMethod

func HTTPMethod(string) MethodOption

HTTPMethod sets http method, default is GET.

Example

Use the swipe.MethodOptions option to specify settings for generating the service method.

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

			MethodOption(
				Signature(service.Interface.Get),

				HTTPMethod(http.MethodGet),
			),
		),
	),
)
Output:

func HTTPPath

func HTTPPath(string) MethodOption

HTTPPath sets http path, default is lowecase method name with the prefix "/", for example: the Get method will look like " /get".

Example

Use the swipe.MethodOptions option to specify settings for generating the service method.

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

			MethodOption(
				Signature(service.Interface.Get),

				HTTPPath("/users"),
			),
		),
	),
)
Output:

func HTTPQueryVars

func HTTPQueryVars([]string) MethodOption

HTTPQueryVars 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.

Example

A parameter is a key pair, where the key is the name of the method parameter, and the value is the name of the parameter in the url query arguments.

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

			MethodOption(
				Signature(service.Interface.Get),

				HTTPQueryVars([]string{"name", "x-name"}),
			),
		),
	),
)
Output:

func HTTPServerOption

func HTTPServerOption(...kithttp.ServerOption) MethodOption

HTTPServerOption sets custom gokit ServerOption.

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 OpenapiTitle

func OpenapiTitle(string) OpenapiOption

OpenapiTitle sets title.

func OpenapiVersion

func OpenapiVersion(string) OpenapiOption

OpenapiVersion sets version.

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 (Basic)

Example basic use Service option.

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

type ServiceMiddlewareOption

type ServiceMiddlewareOption string

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 `Make<transportType>Handler<packageName><serviceName>` template.

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 Client

func Client(opts ...ClientOption) TransportOption

Client generate client for the selected transport.

func HTTPFast

func HTTPFast() TransportOption

HTTPFast enable use valyala/fasthttp instead net/http package.

Supported in both REST and JSON RPC.

Example

Example enabled valyala/fasthttp. Supported in both REST and JSON RPC.

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

			HTTPFast(),
		),
	),
)
Output:

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 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 (BasicREST)
Build(
	Service(
		Interface((*service.Interface)(nil)),
		Transport(
			Protocol("http"),
		),
	),
)
Output:

Jump to

Keyboard shortcuts

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