swipe

package
v1.0.0-beta3 Latest Latest
Warning

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

Go to latest
Published: May 14, 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

Full example:

 //+build swipe

 package jsonrpc

 import (
	  "github.com/swipeio/swipe/fixtures/service"

	  . "github.com/swipeio/swipe/pkg/swipe"
 )

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

				ClientEnable(),

				Openapi(
					OpenapiOutput("/../../docs"),
					OpenapiVersion("1.0.0"),
				),
			),
			Logging(),
			Instrumenting(),
		),
	  )
 }

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 ClientDecodeResponseFunc

func ClientDecodeResponseFunc(interface{}) MethodOption

ClientDecodeResponseFunc it is intended for use in clients, for client-side endpoints. One of the simple response decoding functions can be that JSON decodes from the response body to a specific response type.

func ClientEncodeRequestFunc

func ClientEncodeRequestFunc(interface{}) MethodOption

ClientEncodeRequestFunc it is intended for use in clients, for client-side endpoints. One of the simple functions of an encoding request is that JSON encodes an object directly into the request body.

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

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 it is intended for use in servers, for server-side endpoints. One of the simple methods of DecodeRequestFunc can be that JSON decodes from the request body to a specific request type.

func ServerEncodeResponseFunc

func ServerEncodeResponseFunc(interface{}) MethodOption

ServerEncodeResponseFunc it is intended for use in servers, for server-side endpoints. One of the simple functions of encoding a response is that JSON encodes an object directly into the response body.

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.

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

			Openapi(
				OpenapiContactEmail("your_email@mail.com"),
			),
		),
	),
)
Output:

func OpenapiContactName

func OpenapiContactName(string) OpenapiOption

OpenapiContactName sets contact name.

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

			Openapi(
				OpenapiContactName("name"),
			),
		),
	),
)
Output:

func OpenapiContactURL

func OpenapiContactURL(string) OpenapiOption

OpenapiContactURL sets contact URL.

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

			Openapi(
				OpenapiContactURL("http://contact.url"),
			),
		),
	),
)
Output:

func OpenapiDescription

func OpenapiDescription(string) OpenapiOption

OpenapiDescription sets description.

func OpenapiLicenceName

func OpenapiLicenceName(string) OpenapiOption

OpenapiLicenceName sets licence name.

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

			Openapi(
				OpenapiLicenceName("MIT"),
			),
		),
	),
)
Output:

func OpenapiLicenceURL

func OpenapiLicenceURL(string) OpenapiOption

OpenapiLicenceURL sets licence URL.

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

			Openapi(
				OpenapiLicenceURL("http://mit.licence"),
			),
		),
	),
)
Output:

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 OpenapiServers

func OpenapiServers(...OpenapiServersOption) OpenapiOption

OpenapiServers sets openapi servers.

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

			Openapi(
				OpenapiServers(
					OpenapiServer(
						OpenapiServerDescription("Description for server"),
						OpenapiServerURL("http://server.domain"),
					),
				),
			),
		),
	),
)
Output:

func OpenapiTitle

func OpenapiTitle(string) OpenapiOption

OpenapiTitle sets title.

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

			Openapi(
				OpenapiTitle("Openapi doc title"),
			),
		),
	),
)
Output:

func OpenapiVersion

func OpenapiVersion(string) OpenapiOption

OpenapiVersion sets version.

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

			Openapi(
				OpenapiVersion("1.0.0"),
			),
		),
	),
)
Output:

type OpenapiServerOption

type OpenapiServerOption string

A OpenapiServerOption is an openapi concrete server option.

func OpenapiServerDescription

func OpenapiServerDescription(string) OpenapiServerOption

OpenapiServerDescription sets server description.

func OpenapiServerURL

func OpenapiServerURL(string) OpenapiServerOption

OpenapiServerURL sets URL description.

type OpenapiServersOption

type OpenapiServersOption string

A OpenapiServersOption is an openapi servers option.

func OpenapiServer

func OpenapiServer(...OpenapiServerOption) OpenapiServersOption

OpenapiServer sets concrete server options.

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.

This option is required

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 (JsonRPCListener)
h, err := jsonrpc.ServerJSONRPCServiceInterface(&service.Service{}, log.NewNopLogger())
if err != nil {
	panic(err)
}
go http.ListenAndServe(":80", h)
Output:

Example (RestListener)
h, err := rest.ServerRESTServiceInterface(&service.Service{}, log.NewNopLogger())
if err != nil {
	panic(err)
}
go http.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

NotWrapBody coming soon.

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, coming soon gRPC.

This option is 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