rest

package
v0.0.0-...-b14b5fa Latest Latest
Warning

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

Go to latest
Published: May 26, 2015 License: Apache-2.0 Imports: 9 Imported by: 0

README

The binding object should be used by both the server as well as the client generator. This way we dont have repetitions on building this. So one binding is a Restful HTTP binding (others are RCP etc). A binding should specify the service, operation and details about the transport level endpoint. eg:

Binding:
	Service: IUserService,
	Operation: CreateTeam,
	Input: 
		Params: request
		Endpoint: POST /teams/
	Output:
		Params: request, error
		Body: // Only one of the following is required
			Template: A template string to which the outputs are passed as an array to be rendered.
			TemplateFile: Name of the template file to which the outputs are passed as an array to be rendered.
			Presenter: A presenter function that presents the output to the
			transport

The above binding is for creating a team. It specifies the endpoint on both sides. The type of the input will be inferred by inspecting the parameters for the service method. Variable bindings are specified with {...}. Input parameters can be given names as above. The input endpoint and the body will all the details to read the request object. The input endpoint path can have parametrised values to denote which values in the request are to be set.

Similary output presenter has its own section.

This binding should have enough information to allow the following:

1. Generation of request parsers
2. Generation of request presenters
3. Generation of response parsers
4. Generation of response presenters

Note that this is specific to one kind of transport (http). The input and output structures for a service could be different for other kinds of transport (eg binary or socket or RPC etc)

How will all this be integrated back?

  1. go generate httpbindings -input bindings.go -lang -o outfile --

This will generate a list of files (or a single file depending on language) that has serialiser and deserializer of service request objects to and from http (taking into account content types and accept headers)

  1. This will also generate http handler functions for each service operation, eg:
type ITeamServiceHandler struct {
	teamService ITeamService
	RequestDecorator func(req *http.Request) (*http.Request, error)
}

func (svc ITeamServiceClient) SendCreateTeam(req *CreateTeamRequest) (*CreateTeamResponse, error) {
	httpreq, err := SerializeCreateTeamRequest(req)
	if err != nil {
		return nil, err
	}
	if svc.RequestDecorator != nil {
		httpreq, err = svc.RequestDecorator(httpreq)
		if err != nil {
			return nil, err
		}
	}
	resp, err := SendHttpRequest(httpreq)
	if err != nil {
		return nil, err
	}
	return DeserializeCreateTeamResponse(resp, httpreq)
}

What will be generated for each language is:

  • SerializeRequest
  • DeserializeRequest
  • SerializeResponse
  • DeserializeResponse
  • Handler class
  • Client class

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// where the templates are
	Bindings     map[string]*HttpBinding
	TypeLib      bridge.ITypeLibrary
	TemplatesDir string

	// Parameters to determine Generated output
	Package           string
	ClientPackageName string
	ServiceName       string
	ClientPrefix      string
	ClientSuffix      string

	ServiceType      *bridge.Type
	TransportRequest string
	OpName           string
	OpType           *bridge.FunctionTypeData
	OpMethod         string
	OpEndpoint       string
	ExistingWriters  map[string]string
	ExistingReaders  map[string]string

	// Callbacks from the template to mark certain items in the code generation
	TypeMarker func(types ...*bridge.Type)
	// contains filtered or unexported fields
}

*

  • Responsible for generating the code for the client classes.

func NewGenerator

func NewGenerator(bindings map[string]*HttpBinding, typeLib bridge.ITypeLibrary, templatesDir string) *Generator

func (*Generator) ClientName

func (g *Generator) ClientName() string

func (*Generator) EmitClientClass

func (g *Generator) EmitClientClass(writer io.Writer, serviceType *bridge.Type) error

*

  • Emits the class that captures all the methods for sendign service calls and
  • receiving parsing the responses.

func (*Generator) EmitServiceCallMethod

func (g *Generator) EmitServiceCallMethod(writer io.Writer, opName string, opType *bridge.FunctionTypeData, argPrefix string) error

*

  • For a given service operation, emits a method which:
  • 1. Has inputs the same as those of the underlying service operation,
  • 2. creates a transport level request
  • 3. Sends the transport level request
  • 4. Gets a response from the transport level and returns it

func (*Generator) EmitTypeReader

func (g *Generator) EmitTypeReader(writer io.Writer, argType *bridge.Type) error

*

  • Emits the reader for a particular type.

func (*Generator) EmitTypeWriter

func (g *Generator) EmitTypeWriter(writer io.Writer, argType *bridge.Type) error

*

  • Emits the writer for a particular type.

func (*Generator) EmitTypeWriterBody

func (g *Generator) EmitTypeWriterBody(writer io.Writer, argType *bridge.Type) error

func (*Generator) EmitTypeWriterFooter

func (g *Generator) EmitTypeWriterFooter(writer io.Writer, argType *bridge.Type) error

func (*Generator) EmitTypeWriterHeader

func (g *Generator) EmitTypeWriterHeader(writer io.Writer, argType *bridge.Type) error

func (*Generator) IOMethodForType

func (g *Generator) IOMethodForType(t *bridge.Type) string

func (*Generator) MarkType

func (g *Generator) MarkType(types ...*bridge.Type) string

func (*Generator) MarkTypes

func (g *Generator) MarkTypes(types []*bridge.Type) string

func (*Generator) ServiceTypeData

func (g *Generator) ServiceTypeData() *bridge.RecordTypeData

func (*Generator) TypeWriterBodyString

func (g *Generator) TypeWriterBodyString(argType *bridge.Type) string

type HttpBinding

type HttpBinding struct {
	/**
	 * Methods required to match for this binding to get triggered.
	 */
	Methods []string

	/**
	 * The URL which will trigger this binding.
	 */
	Url string

	// Mappings between a query or BODY parameter to a key with the request
	ParamMappings map[string][]string

	// Mappings between a path variable to a key with the request
	VarMappings map[string][]string

	/**
	 * The service that needs to be invoked when the binding matches.
	 */
	Service interface{}

	/**
	 * Name of the operation to invoke.
	 */
	Operation string

	/**
	 * Type of the request object to be created and populated.
	 */
	RequestType      reflect.Type
	RequestTypeIsPtr bool

	/**
	 * The method corresponding to the operation within the service.
	 */
	Method reflect.Value
}

func NewHttpBinding

func NewHttpBinding(url string, methods []string, service interface{}, operation string) *HttpBinding

type HttpInputBinder

type HttpInputBinder struct {
	// Methods that are ok for this
	Bindings []*HttpBinding
}

func (*HttpInputBinder) AddBinding

func (h *HttpInputBinder) AddBinding(binding *HttpBinding)

func (*HttpInputBinder) MatchBinding

func (h *HttpInputBinder) MatchBinding(request *http.Request) *HttpBinding

type RestProtocol

type RestProtocol struct {
}

func (*RestProtocol) WriteReadResponseMethod

func (protocl *RestProtocol) WriteReadResponseMethod(opName string, opType *bridge.FunctionTypeData)

Jump to

Keyboard shortcuts

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