service

package
v2.0.0+incompatible Latest Latest
Warning

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

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

Documentation

Overview

Package service contains the code generation algorithms to produce code for the service and views packages and dummy implementation for the services defined in the design.

Index

Constants

This section is empty.

Variables

View Source
var Services = make(ServicesData)

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

Functions

func AddServiceDataMetaTypeImports

func AddServiceDataMetaTypeImports(header *codegen.SectionTemplate, serviceE *expr.ServiceExpr)

AddServiceDataMetaTypeImports Adds all imports defined by struct:field:type from the service expr and the service data

func AuthFuncsFile

func AuthFuncsFile(genpkg string, root *expr.RootExpr) *codegen.File

AuthFuncsFile returns a file that contains a dummy implementation of the authorization functions needed to instantiate the service endpoints.

func ClientFile

func ClientFile(service *expr.ServiceExpr) *codegen.File

ClientFile returns the client file for the given service.

func ConvertFile

func ConvertFile(root *expr.RootExpr, service *expr.ServiceExpr) (*codegen.File, error)

ConvertFile returns the file containing the conversion and creation functions if any.

func EndpointFile

func EndpointFile(genpkg string, service *expr.ServiceExpr) *codegen.File

EndpointFile returns the endpoint file for the given service.

func ExampleServiceFiles

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

ExampleServiceFiles returns a basic service implementation for every service expression.

func File

func File(genpkg string, service *expr.ServiceExpr) *codegen.File

File returns the service file for the given service.

func ViewsFile

func ViewsFile(genpkg string, service *expr.ServiceExpr) *codegen.File

ViewsFile returns the views file for the given service which contains logic to render result types using the defined views.

Types

type Data

type Data struct {
	// Name is the service name.
	Name string
	// Description is the service description.
	Description string
	// StructName is the service struct name.
	StructName string
	// VarName is the service variable name (first letter in lowercase).
	VarName string
	// PkgName is the name of the package containing the generated service
	// code.
	PkgName string
	// ViewsPkg is the name of the package containing the projected and viewed
	// result types.
	ViewsPkg string
	// Methods lists the service interface methods.
	Methods []*MethodData
	// Schemes is the list of security schemes required by the service methods.
	Schemes SchemesData
	// Scope initialized with all the service types.
	Scope *codegen.NameScope
	// ViewScope initialized with all the viewed types.
	ViewScope *codegen.NameScope
	// contains filtered or unexported fields
}

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

func (*Data) Method

func (s *Data) Method(name string) *MethodData

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

type ErrorInitData

type ErrorInitData struct {
	// Name is the name of the init function.
	Name string
	// Description is the error description.
	Description string
	// ErrName is the name of the error.
	ErrName string
	// TypeName is the error struct type name.
	TypeName string
	// TypeRef is the reference to the error type.
	TypeRef string
	// Temporary indicates whether the error is temporary.
	Temporary bool
	// Timeout indicates whether the error is due to timeouts.
	Timeout bool
	// Fault indicates whether the error is server-side fault.
	Fault bool
}

ErrorInitData describes an error returned by a service method of type ErrorResult.

type InitArgData

type InitArgData struct {
	// Name is the argument name.
	Name string
	// Ref is the reference to the argument type.
	Ref string
}

InitArgData represents a single constructor argument.

type InitData

type InitData struct {
	// Name is the name of the constructor function.
	Name string
	// Description is the function description.
	Description string
	// Args lists arguments to this function.
	Args []*InitArgData
	// ReturnTypeRef is the reference to the return type.
	ReturnTypeRef string
	// Code is the transformation code.
	Code string
	// Helpers contain the helpers used in the transformation code.
	Helpers []*codegen.TransformFunctionData
}

InitData contains the data to render a constructor to initialize service types from viewed result types and vice versa.

type MethodData

type MethodData struct {
	// Name is the method name.
	Name string
	// Description is the method description.
	Description string
	// VarName is the Go method name.
	VarName string
	// Payload is the name of the payload type if any,
	Payload string
	// PayloadDef is the payload type definition if any.
	PayloadDef string
	// PayloadRef is a reference to the payload type if any,
	PayloadRef string
	// PayloadDesc is the payload type description if any.
	PayloadDesc string
	// PayloadEx is an example of a valid payload value.
	PayloadEx interface{}
	// StreamingPayload is the name of the streaming payload type if any.
	StreamingPayload string
	// StreamingPayloadDef is the streaming payload type definition if any.
	StreamingPayloadDef string
	// StreamingPayloadRef is a reference to the streaming payload type if any.
	StreamingPayloadRef string
	// StreamingPayloadDesc is the streaming payload type description if any.
	StreamingPayloadDesc string
	// StreamingPayloadEx is an example of a valid streaming payload value.
	StreamingPayloadEx interface{}
	// Result is the name of the result type if any.
	Result string
	// ResultDef is the result type definition if any.
	ResultDef string
	// ResultRef is the reference to the result type if any.
	ResultRef string
	// ResultDesc is the result type description if any.
	ResultDesc string
	// ResultEx is an example of a valid result value.
	ResultEx interface{}
	// Errors list the possible errors defined in the design if any.
	Errors []*ErrorInitData
	// Requirements contains the security requirements for the
	// method.
	Requirements RequirementsData
	// Schemes contains the security schemes types used by the
	// method.
	Schemes SchemesData
	// ViewedResult contains the data required to generate the code handling
	// views if any.
	ViewedResult *ViewedResultTypeData
	// ServerStream indicates that the service method receives a payload
	// stream or sends a result stream or both.
	ServerStream *StreamData
	// ClientStream indicates that the service method receives a result
	// stream or sends a payload result or both.
	ClientStream *StreamData
	// StreamKind is the kind of the stream (payload or result or bidirectional).
	StreamKind expr.StreamKind
}

MethodData describes a single service method.

type ProjectedTypeData

type ProjectedTypeData struct {
	// the projected type
	*UserTypeData
	// Validations lists the validation functions to run on the projected type.
	// If the projected type corresponds to a result type then a validation
	// function for each view is generated. For user types, only one validation
	// function is generated.
	Validations []*ValidateData
	// Projections contains the code to create a projected type based on
	// views. If the projected type corresponds to a result type, then a
	// function for each view is generated.
	Projections []*InitData
	// TypeInits contains the code to convert a projected type to its
	// corresponding service type. If the projected type corresponds to a
	// result type, then a function for each view is generated.
	TypeInits []*InitData
	// ViewsPkg is the views package name.
	ViewsPkg string
	// Views lists the views defined on the projected type.
	Views []*ViewData
}

ProjectedTypeData contains the data used to generate a projected type for the corresponding user type or result type in the service package. The generated type uses pointers for all fields. It also contains the data to generate view-based validation logic and transformation functions to convert a projected type to its corresponding service type and vice versa.

type RequirementData

type RequirementData struct {
	// Schemes list the requirement schemes.
	Schemes []*SchemeData
	// Scopes list the required scopes.
	Scopes []string
}

RequirementData lists the schemes and scopes defined by a single security requirement.

type RequirementsData

type RequirementsData []*RequirementData

RequirementsData is the list of security requirements.

func (RequirementsData) Scheme

func (r RequirementsData) Scheme(name string) *SchemeData

Scheme returns the scheme data with the given scheme name.

type SchemeData

type SchemeData struct {
	// Kind is the type of scheme, one of "Basic", "APIKey", "JWT"
	// or "OAuth2".
	Type string
	// SchemeName is the name of the scheme.
	SchemeName string
	// Name refers to a header or parameter name, based on In's
	// value.
	Name string
	// UsernameField is the name of the payload field that should be
	// initialized with the basic auth username if any.
	UsernameField string
	// UsernamePointer is true if the username field is a pointer.
	UsernamePointer bool
	// UsernameAttr is the name of the attribute that contains the
	// username.
	UsernameAttr string
	// UsernameRequired specifies whether the attribute that
	// contains the username is required.
	UsernameRequired bool
	// PasswordField is the name of the payload field that should be
	// initialized with the basic auth password if any.
	PasswordField string
	// PasswordPointer is true if the password field is a pointer.
	PasswordPointer bool
	// PasswordAttr is the name of the attribute that contains the
	// password.
	PasswordAttr string
	// PasswordRequired specifies whether the attribute that
	// contains the password is required.
	PasswordRequired bool
	// CredField contains the name of the payload field that should
	// be initialized with the API key, the JWT token or the OAuth2
	// access token.
	CredField string
	// CredPointer is true if the credential field is a pointer.
	CredPointer bool
	// CredRequired specifies if the key is a required attribute.
	CredRequired bool
	// KeyAttr is the name of the attribute that contains
	// the security tag (for APIKey, OAuth2, and JWT schemes).
	KeyAttr string
	// Scopes lists the scopes that apply to the scheme.
	Scopes []string
	// Flows describes the OAuth2 flows.
	Flows []*expr.FlowExpr
	// In indicates the request element that holds the credential.
	In string
}

SchemeData describes a single security scheme.

func (*SchemeData) Dup

func (s *SchemeData) Dup() *SchemeData

Dup creates a copy of the scheme data.

type SchemesData

type SchemesData []*SchemeData

SchemesData is the list of security schemes.

func (SchemesData) Append

func (s SchemesData) Append(d *SchemeData) SchemesData

Append appends a scheme data to schemes only if it doesn't exist.

type ServicesData

type ServicesData map[string]*Data

ServicesData encapsulates the data computed from the service designs.

func (ServicesData) Get

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

Get retrieves the 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 {
	// Interface is the name of the stream interface.
	Interface string
	// VarName is the name of the struct type that implements the stream
	// interface.
	VarName string
	// SendName is the name of the send function.
	SendName string
	// SendDesc is the description for the send function.
	SendDesc string
	// SendTypeName is the type name sent through the stream.
	SendTypeName string
	// SendTypeRef is the reference to the type 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 type name received from the stream.
	RecvTypeName string
	// RecvTypeRef is the reference to the type received from the stream.
	RecvTypeRef string
	// MustClose indicates whether the stream should implement the Close()
	// function.
	MustClose bool
	// EndpointStruct is the name of the endpoint struct that holds a payload
	// reference (if any) and the endpoint server stream. It is set only if the
	// client sends a normal payload and server streams a result.
	EndpointStruct string
	// Kind is the kind of the stream (payload or result or bidirectional).
	Kind expr.StreamKind
}

StreamData is the data used to generate client and server interfaces that a streaming endpoint implements. It is initialized if a method defines a streaming payload or result or both.

type UserTypeData

type UserTypeData struct {
	// Name is the type name.
	Name string
	// VarName is the corresponding Go type name.
	VarName string
	// Description is the type human description.
	Description string
	// Def is the type definition Go code.
	Def string
	// Ref is the reference to the type.
	Ref string
	// Type is the underlying type.
	Type expr.UserType
}

UserTypeData contains the data describing a user-defined type.

type ValidateData

type ValidateData struct {
	// Name is the validation function name.
	Name string
	// Ref is the reference to the type on which the validation function
	// is defined.
	Ref string
	// Description is the description for the validation function.
	Description string
	// Validate is the validation code.
	Validate string
}

ValidateData contains data to render a validate function to validate a projected type or a viewed result type based on views.

type ViewData

type ViewData struct {
	// Name is the view name.
	Name string
	// Description is the view description.
	Description string
	// Attributes is the list of attributes rendered in the view.
	Attributes []string
	// TypeVarName is the Go variable name of the type that defines the view.
	TypeVarName string
}

ViewData contains data about a result type view.

type ViewedResultTypeData

type ViewedResultTypeData struct {
	// the viewed result type
	*UserTypeData
	// Views lists the views defined on the viewed result type.
	Views []*ViewData
	// Validate is the validation run on the viewed result type.
	Validate *ValidateData
	// Init is the constructor code to initialize a viewed result type from
	// a result type.
	Init *InitData
	// ResultInit is the constructor code to initialize a result type
	// from the viewed result type.
	ResultInit *InitData
	// FullName is the fully qualified name of the viewed result type.
	FullName string
	// FullRef is the complete reference to the viewed result type
	// (including views package name).
	FullRef string
	// IsCollection indicates whether the viewed result type is a collection.
	IsCollection bool
	// ViewName is the view name to use to render the result type. It is set
	// only if the result type has at most one view.
	ViewName string
	// ViewsPkg is the views package name.
	ViewsPkg string
}

ViewedResultTypeData contains the data used to generate a viewed result type (i.e. a method result type with more than one view). The viewed result type holds the projected type and a view based on which it creates the projected type. It also contains the code to validate the viewed result type and the functions to initialize a viewed result type from a result type and vice versa.

Jump to

Keyboard shortcuts

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