goa: github.com/raphael/goa/http/codegen Index | Files | Directories

package codegen

import "github.com/raphael/goa/http/codegen"

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

Package Files

client.go client_cli.go client_types.go doc.go example_cli.go example_server.go funcs.go openapi.go paths.go server.go server_types.go service_data.go testing.go typedef.go

Variables

var HTTPServices = make(ServicesData)

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

func ClientCLIFiles Uses

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

ClientCLIFiles returns the client HTTP CLI support file.

func ClientFiles Uses

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

ClientFiles returns the client HTTP transport files.

func ClientTypeFiles Uses

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

ClientTypeFiles returns the HTTP transport client types files.

func ExampleCLIFiles Uses

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

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

func ExampleServerFiles Uses

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

ExampleServerFiles returns an example http service implementation.

func OpenAPIFiles Uses

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

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

func PathFiles Uses

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

PathFiles returns the service path files.

func RunHTTPDSL Uses

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

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

func ServerFiles Uses

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

ServerFiles returns all the server HTTP transport files.

func ServerTypeFiles Uses

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

ServerTypeFiles returns the HTTP transport type files.

type EndpointData Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

type ServicesData map[string]*ServiceData

ServicesData encapsulates the data computed from the design.

func (ServicesData) Get Uses

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 Uses

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 Uses

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

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

Package codegen imports 20 packages (graph). Updated 2019-09-17. Refresh now. Tools for package owners.