codegen

package
v2.0.5+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package codegen contains code generation logic to generate HTTP server and client that wrap the generated goa endpoint and the OpenAPI 2.0 specifications for the HTTP endpoints.

Index

Constants

This section is empty.

Variables

View Source
var HTTPServices = make(ServicesData)

HTTPServices holds the data computed from the design needed to generate the transport code of the services.

Functions

func ClientCLIFiles

func ClientCLIFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ClientCLIFiles returns the client HTTP CLI support file.

func ClientFiles

func ClientFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ClientFiles returns the client HTTP transport files.

func ClientTypeFiles

func ClientTypeFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ClientTypeFiles returns the HTTP transport client types files.

func ExampleCLIFiles

func ExampleCLIFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ExampleCLIFiles returns an example client tool HTTP implementation for each server expression.

func ExampleServerFiles

func ExampleServerFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ExampleServerFiles returns an example http service implementation.

func OpenAPIFiles

func OpenAPIFiles(root *expr.RootExpr) ([]*codegen.File, error)

OpenAPIFiles returns the files for the OpenAPIFile spec of the given HTTP API.

func PathFiles

func PathFiles(root *expr.RootExpr) []*codegen.File

PathFiles returns the service path files.

func RunHTTPDSL

func RunHTTPDSL(t *testing.T, dsl func()) *expr.RootExpr

RunHTTPDSL returns the HTTP DSL root resulting from running the given DSL.

func ServerFiles

func ServerFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ServerFiles returns all the server HTTP transport files.

func ServerTypeFiles

func ServerTypeFiles(genpkg string, root *expr.RootExpr) []*codegen.File

ServerTypeFiles returns the HTTP transport type files.

Types

type EndpointData

type EndpointData struct {
	// Method contains the related service method data.
	Method *service.MethodData
	// ServiceName is the name of the service exposing the endpoint.
	ServiceName string
	// ServiceVarName is the goified service name (first letter
	// lowercase).
	ServiceVarName string
	// ServicePkgName is the name of the service package.
	ServicePkgName string
	// Payload describes the method HTTP payload.
	Payload *PayloadData
	// Result describes the method HTTP result.
	Result *ResultData
	// Errors describes the method HTTP errors.
	Errors []*ErrorGroupData
	// Routes describes the possible routes for this endpoint.
	Routes []*RouteData
	// BasicScheme is the basic auth security scheme if any.
	BasicScheme *service.SchemeData
	// HeaderSchemes lists all the security requirement schemes that
	// apply to the method and are encoded in the request header.
	HeaderSchemes service.SchemesData
	// BodySchemes lists all the security requirement schemes that
	// apply to the method and are encoded in the request body.
	BodySchemes service.SchemesData
	// QuerySchemes lists all the security requirement schemes that
	// apply to the method and are encoded in the request query
	// string.
	QuerySchemes service.SchemesData

	// MountHandler is the name of the mount handler function.
	MountHandler string
	// HandlerInit is the name of the constructor function for the
	// http handler function.
	HandlerInit string
	// RequestDecoder is the name of the request decoder function.
	RequestDecoder string
	// ResponseEncoder is the name of the response encoder function.
	ResponseEncoder string
	// ErrorEncoder is the name of the error encoder function.
	ErrorEncoder string
	// MultipartRequestDecoder indicates the request decoder for
	// multipart content type.
	MultipartRequestDecoder *MultipartData
	// ServerStream holds the data to render the server struct which
	// implements the server stream interface.
	ServerStream *StreamData

	// ClientStruct is the name of the HTTP client struct.
	ClientStruct string
	// EndpointInit is the name of the constructor function for the
	// client endpoint.
	EndpointInit string
	// RequestInit is the request builder function.
	RequestInit *InitData
	// RequestEncoder is the name of the request encoder function.
	RequestEncoder string
	// ResponseDecoder is the name of the response decoder function.
	ResponseDecoder string
	// MultipartRequestEncoder indicates the request encoder for
	// multipart content type.
	MultipartRequestEncoder *MultipartData
	// ClientStream holds the data to render the client struct which
	// implements the client stream interface.
	ClientStream *StreamData
}

EndpointData contains the data used to render the code related to a single service HTTP endpoint.

type ErrorData

type ErrorData struct {
	// Name is the error name.
	Name string
	// Ref is a reference to the error type.
	Ref string
	// Response is the error response data.
	Response *ResponseData
}

ErrorData contains the error information required to generate the transport decode (client) and encode (server) code.

type ErrorGroupData

type ErrorGroupData struct {
	// StatusCode is the response HTTP status code.
	StatusCode string
	// Errors contains the information for each error.
	Errors []*ErrorData
}

ErrorGroupData contains the error information required to generate the transport decode (client) and encode (server) code for all errors with responses using a given HTTP status code.

type FileServerData

type FileServerData struct {
	// MountHandler is the name of the mount handler function.
	MountHandler string
	// RequestPaths is the set of HTTP paths to the server.
	RequestPaths []string
	// Root is the root server file path.
	FilePath string
	// Dir is true if the file server servers files under a
	// directory, false if it serves a single file.
	IsDir bool
	// PathParam is the name of the parameter used to capture the
	// path for file servers that serve files under a directory.
	PathParam string
}

FileServerData lists the data needed to generate file servers.

type HeaderData

type HeaderData struct {
	// Name is the name of the header key.
	Name string
	// AttributeName is the name of the corresponding attribute.
	AttributeName string
	// Description is the header description.
	Description string
	// CanonicalName is the canonical header key.
	CanonicalName string
	// FieldName is the name of the struct field that holds the
	// header value if any, empty string otherwise.
	FieldName string
	// FieldPointer if true indicates that the struct field that holds the
	// header value is a pointer.
	FieldPointer bool
	// VarName is the name of the Go variable used to read or
	// convert the header value.
	VarName string
	// TypeName is the name of the type.
	TypeName string
	// TypeRef is the reference to the type.
	TypeRef string
	// Required is true if the header is required.
	Required bool
	// Pointer is true if and only the param variable is a pointer.
	Pointer bool
	// StringSlice is true if the param type is array of strings.
	StringSlice bool
	// Slice is true if the param type is an array.
	Slice bool
	// Type describes the datatype of the variable value. Mainly
	// used for conversion.
	Type expr.DataType
	// Validate contains the validation code if any.
	Validate string
	// DefaultValue contains the default value if any.
	DefaultValue interface{}
	// Example is an example value.
	Example interface{}
}

HeaderData describes a HTTP request or response header.

type InitArgData

type InitArgData struct {
	// Name is the argument name.
	Name string
	// Description is the argument description.
	Description string
	// Reference to the argument, e.g. "&body".
	Ref string
	// FieldName is the name of the data structure field that should
	// be initialized with the argument if any.
	FieldName string
	// FieldPointer if true indicates that the data structure field is a
	// pointer.
	FieldPointer bool
	// TypeName is the argument type name.
	TypeName string
	// TypeRef is the argument type reference.
	TypeRef string
	// Pointer is true if a pointer to the arg should be used.
	Pointer bool
	// Required is true if the arg is required to build the payload.
	Required bool
	// DefaultValue is the default value of the arg.
	DefaultValue interface{}
	// Validate contains the validation code for the argument
	// value if any.
	Validate string
	// Example is a example value
	Example interface{}
}

InitArgData represents a single constructor argument.

type InitData

type InitData struct {
	// Name is the constructor function name.
	Name string
	// Description is the function description.
	Description string
	// ServerArgs is the list of constructor arguments for server
	// side code.
	ServerArgs []*InitArgData
	// ClientArgs is the list of constructor arguments for client
	// side code.
	ClientArgs []*InitArgData
	// CLIArgs is the list of arguments that should be initialized
	// from CLI flags. This is used for implicit attributes which
	// as the time of writing is only used for the basic auth
	// username and password.
	CLIArgs []*InitArgData
	// ReturnTypeName is the qualified (including the package name)
	// name of the payload, result or error type.
	ReturnTypeName string
	// ReturnTypeRef is the qualified (including the package name)
	// reference to the payload, result or error type.
	ReturnTypeRef string
	// ReturnTypeAttribute is the name of the attribute initialized
	// by this constructor when it only initializes one attribute
	// (i.e. body was defined with Body("name") syntax).
	ReturnTypeAttribute string
	// ReturnIsStruct is true if the return type is a struct.
	ReturnIsStruct bool
	// ReturnIsPrimitivePointer indicates whether the return type is
	// a primitive pointer.
	ReturnIsPrimitivePointer bool
	// ServerCode is the code that builds the payload from the
	// request on the server when it contains user types.
	ServerCode string
	// ClientCode is the code that builds the payload or result type
	// from the request or response state on the client when it
	// contains user types.
	ClientCode string
}

InitData contains the data required to render a constructor.

type MultipartData

type MultipartData struct {
	// FuncName is the name used to generate function type.
	FuncName string
	// InitName is the name of the constructor.
	InitName string
	// VarName is the name of the variable referring to the function.
	VarName string
	// ServiceName is the name of the service.
	ServiceName string
	// MethodName is the name of the method.
	MethodName string
	// Payload is the payload data required to generate
	// encoder/decoder.
	Payload *PayloadData
}

MultipartData contains the data needed to render multipart encoder/decoder.

type ParamData

type ParamData struct {
	// Name is the name of the mapping to the actual variable name.
	Name string
	// AttributeName is the name of the corresponding attribute.
	AttributeName string
	// Description is the parameter description
	Description string
	// FieldName is the name of the struct field that holds the
	// param value.
	FieldName string
	// FieldPointer if true indicates that the struct field that holds the
	// param value is a pointer.
	FieldPointer bool
	// VarName is the name of the Go variable used to read or
	// convert the param value.
	VarName string
	// ServiceField is true if there is a corresponding attribute in
	// the service types.
	ServiceField bool
	// Type is the datatype of the variable.
	Type expr.DataType
	// TypeName is the name of the type.
	TypeName string
	// TypeRef is the reference to the type.
	TypeRef string
	// Required is true if the param is required.
	Required bool
	// Pointer is true if and only the param variable is a pointer.
	Pointer bool
	// StringSlice is true if the param type is array of strings.
	StringSlice bool
	// Slice is true if the param type is an array.
	Slice bool
	// MapStringSlice is true if the param type is a map of string
	// slice.
	MapStringSlice bool
	// Map is true if the param type is a map.
	Map bool
	// Validate contains the validation code if any.
	Validate string
	// DefaultValue contains the default value if any.
	DefaultValue interface{}
	// Example is an example value.
	Example interface{}
	// MapQueryParams indicates that the query params must be mapped
	// to the entire payload (empty string) or a payload attribute
	// (attribute name).
	MapQueryParams *string
}

ParamData describes a HTTP request parameter.

type PayloadData

type PayloadData struct {
	// Name is the name of the payload type.
	Name string
	// Ref is the fully qualified reference to the payload type.
	Ref string
	// Request contains the data for the corresponding HTTP request.
	Request *RequestData
	// DecoderReturnValue is a reference to the decoder return value
	// if there is no payload constructor (i.e. if Init is nil).
	DecoderReturnValue string
}

PayloadData contains the payload information required to generate the transport decode (server) and encode (client) code.

type RequestData

type RequestData struct {
	// PathParams describes the information about params that are
	// present in the request path.
	PathParams []*ParamData
	// QueryParams describes the information about the params that
	// are present in the request query string.
	QueryParams []*ParamData
	// Headers contains the HTTP request headers used to build the
	// method payload.
	Headers []*HeaderData
	// ServerBody describes the request body type used by server
	// code. The type is generated using pointers for all fields so
	// that it can be validated.
	ServerBody *TypeData
	// ClientBody describes the request body type used by client
	// code. The type does NOT use pointers for every fields since
	// no validation is required.
	ClientBody *TypeData
	// PayloadInit contains the data required to render the
	// payload constructor used by server code if any.
	PayloadInit *InitData
	// MustValidate is true if the request body or at least one
	// parameter or header requires validation.
	MustValidate bool
	// Multipart if true indicates the request is a multipart
	// request.
	Multipart bool
}

RequestData describes a request.

type ResponseData

type ResponseData struct {
	// StatusCode is the return code of the response.
	StatusCode string
	// Description is the response description.
	Description string
	// Headers provides information about the headers in the
	// response.
	Headers []*HeaderData
	// ContentType contains the value of the response
	// "Content-Type" header.
	ContentType string
	// ErrorHeader contains the value of the response "goa-error"
	// header if any.
	ErrorHeader string
	// ServerBody is the type of the response body used by server
	// code, nil if body should be empty. The type does NOT use
	// pointers for all fields. If the method result is a result
	// type and the response data describes a success response, then
	// this field contains a type for every view in the result type.
	// The type name is suffixed with the name of the view (except
	// for "default" view where no suffix is added). A constructor
	// is also generated server side for each view to transform the
	// result type to the corresponding response body type. If
	// method result is not a result type or if the response
	// describes an error response, then this field contains at most
	// one item.
	ServerBody []*TypeData
	// ClientBody is the type of the response body used by client
	// code, nil if body should be empty. The type uses pointers for
	// all fields so they can be validated.
	ClientBody *TypeData
	// Init contains the data required to render the result or error
	// constructor if any.
	ResultInit *InitData
	// TagName is the name of the attribute used to test whether the
	// response is the one to use.
	TagName string
	// TagValue is the value the result attribute named by TagName
	// must have for this response to be used.
	TagValue string
	// TagPointer is true if the tag attribute is a pointer.
	TagPointer bool
	// MustValidate is true if at least one header requires validation.
	MustValidate bool
	// ResultAttr sets the response body from the specified result
	// type attribute. This field is set when the design uses
	// Body("name") syntax to set the response body and the result
	// type is an object.
	ResultAttr string
	// ViewedResult indicates whether the response body type is a
	// result type.
	ViewedResult *service.ViewedResultTypeData
}

ResponseData describes a response.

type ResultData

type ResultData struct {
	// Name is the name of the result type.
	Name string
	// Ref is the reference to the result type.
	Ref string
	// IsStruct is true if the result type is a user type defining
	// an object.
	IsStruct bool
	// Inits contains the data required to render the result
	// constructors if any.
	Inits []*InitData
	// Responses contains the data for the corresponding HTTP
	// responses.
	Responses []*ResponseData
	// View is the view used to render the result.
	View string
	// MustInit indicates if a variable holding the result type must be
	// initialized. It is used by server response encoder to initialize
	// the result variable only if there are multiple responses, or the
	// response has a body or a header.
	MustInit bool
}

ResultData contains the result information required to generate the transport decode (client) and encode (server) code.

type RouteData

type RouteData struct {
	// Verb is the HTTP method.
	Verb string
	// Path is the fullpath including wildcards.
	Path string
	// PathInit contains the information needed to render and call
	// the path constructor for the route.
	PathInit *InitData
}

RouteData describes a route.

type ServiceData

type ServiceData struct {
	// Service contains the related service data.
	Service *service.Data
	// Endpoints describes the endpoint data for this service.
	Endpoints []*EndpointData
	// FileServers lists the file servers for this service.
	FileServers []*FileServerData
	// ServerStruct is the name of the HTTP server struct.
	ServerStruct string
	// MountPointStruct is the name of the mount point struct.
	MountPointStruct string
	// ServerInit is the name of the constructor of the server
	// struct.
	ServerInit string
	// MountServer is the name of the mount function.
	MountServer string
	// ServerService is the name of service function.
	ServerService string
	// ClientStruct is the name of the HTTP client struct.
	ClientStruct string
	// ServerBodyAttributeTypes is the list of user types used to
	// define the request, response and error response type
	// attributes in the server code.
	ServerBodyAttributeTypes []*TypeData
	// ClientBodyAttributeTypes is the list of user types used to
	// define the request, response and error response type
	// attributes in the client code.
	ClientBodyAttributeTypes []*TypeData
	// ServerTypeNames records the user type names used to define
	// the endpoint request and response bodies for server code.
	// The type name is used as the key and a bool as the value
	// which if true indicates that the type has been generated
	// in the server package.
	ServerTypeNames map[string]bool
	// ClientTypeNames records the user type names used to define
	// the endpoint request and response bodies for client code.
	// The type name is used as the key and a bool as the value
	// which if true indicates that the type has been generated
	// in the client package.
	ClientTypeNames map[string]bool
	// ServerTransformHelpers is the list of transform functions
	// required by the various server side constructors.
	ServerTransformHelpers []*codegen.TransformFunctionData
	// ClientTransformHelpers is the list of transform functions
	// required by the various client side constructors.
	ClientTransformHelpers []*codegen.TransformFunctionData
	// Scope initialized with all the server and client types.
	Scope *codegen.NameScope
}

ServiceData contains the data used to render the code related to a single service.

func (*ServiceData) Endpoint

func (svc *ServiceData) Endpoint(name string) *EndpointData

Endpoint returns the service method transport data for the endpoint with the given name, nil if there isn't one.

type ServicesData

type ServicesData map[string]*ServiceData

ServicesData encapsulates the data computed from the design.

func (ServicesData) Get

func (d ServicesData) Get(name string) *ServiceData

Get retrieves the transport data for the service with the given name computing it if needed. It returns nil if there is no service with the given name.

type StreamData

type StreamData struct {
	// VarName is the name of the struct.
	VarName string
	// Type is type of the stream (server or client).
	Type string
	// Interface is the fully qualified name of the interface that
	// the struct implements.
	Interface string
	// Endpoint is endpoint data that defines streaming
	// payload/result.
	Endpoint *EndpointData
	// Payload is the streaming payload type sent via the stream.
	Payload *TypeData
	// Response is the successful response data for the streaming
	// endpoint.
	Response *ResponseData
	// SendName is the name of the send function.
	SendName string
	// SendDesc is the description for the send function.
	SendDesc string
	// SendTypeName is the fully qualified type name sent through
	// the stream.
	SendTypeName string
	// SendTypeRef is the fully qualified type ref sent through the
	// stream.
	SendTypeRef string
	// RecvName is the name of the receive function.
	RecvName string
	// RecvDesc is the description for the recv function.
	RecvDesc string
	// RecvTypeName is the fully qualified type name received from
	// the stream.
	RecvTypeName string
	// RecvTypeRef is the fully qualified type ref received from the
	// stream.
	RecvTypeRef string
	// MustClose indicates whether to generate the Close() function
	// for the stream.
	MustClose bool
	// PkgName is the service package name.
	PkgName string
	// Kind is the kind of the stream (payload, result or
	// bidirectional).
	Kind expr.StreamKind
}

StreamData contains the data needed to render struct type that implements the server and client stream interfaces.

type TypeData

type TypeData struct {
	// Name is the type name.
	Name string
	// VarName is the Go type name.
	VarName string
	// Description is the type human description.
	Description string
	// Init contains the data needed to render and call the type
	// constructor if any.
	Init *InitData
	// Def is the type definition Go code.
	Def string
	// Ref is the reference to the type.
	Ref string
	// ValidateDef contains the validation code.
	ValidateDef string
	// ValidateRef contains the call to the validation code.
	ValidateRef string
	// Example is an example value for the type.
	Example interface{}
	// View is the view using which the type is rendered.
	View string
}

TypeData contains the data needed to render a type definition.

Directories

Path Synopsis
Package openapi produces OpenAPI Specification 2.0 (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md) for the HTTP endpoints.
Package openapi produces OpenAPI Specification 2.0 (https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md) for the HTTP endpoints.

Jump to

Keyboard shortcuts

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