Documentation
¶
Overview ¶
Package httptransport provides functions and template helpers for templating the http-transport of a go-kit based microservice.
Index ¶
- Variables
- func AllFuncSourceCode(val interface{}) (string, error)
- func ApplyTemplate(name string, tmpl string, executor interface{}, fncs template.FuncMap) (string, error)
- func BuildParamMap(urlTmpl string) map[string]int
- func EnglishNumber(i int) string
- func FormatCode(code string) string
- func FuncSourceCode(val interface{}) (string, error)
- func LowCamelName(s string) string
- func PathParams(url string, urlTmpl string) (map[string]string, error)
- func QueryParams(vals url.Values) (map[string]string, error)
- func RemoveBraces(val string) string
- type Binding
- type Field
- type Helper
- type Method
Constants ¶
This section is empty.
Variables ¶
var BuildParamMapTemplate = `` /* 256-byte string literal not displayed */
BuildParamMapTemplate is a source code literal of the code for the BuildParamMap function found in embeddeable_funcs.go
var ClientEncodeTemplate = `` /* 1394-byte string literal not displayed */
ClientEncodeTemplate is the template for generating the client-side encoding function for a particular Binding.
var DigitEnglish = map[rune]string{
'0': "zero",
'1': "one",
'2': "two",
'3': "three",
'4': "four",
'5': "five",
'6': "six",
'7': "seven",
'8': "eight",
'9': "nine",
}
DigitEnglish is a map of runes of digits zero to nine to their lowercase english language spellings.
var HTTPAssistFuncs = PathParamsTemplate + BuildParamMapTemplate + RemoveBracesTemplate + QueryParamsTemplate
HTTPAssistFuncs is a source code literal of all the helper functions used for encoding and decoding http request to and from generated protobuf structs, and is used within the generated code of each microservice.
var PathParamsTemplate = `` /* 231-byte string literal not displayed */
PathParamsTemplate is a source code literal of the code for the PathParams function found in embeddeable_funcs.go
var QueryParamsTemplate = `` /* 154-byte string literal not displayed */
QueryParamsTemplate is a source code literal of the code for the QueryParams function found in embeddeable_funcs.go
var RemoveBracesTemplate = `` /* 135-byte string literal not displayed */
RemoveBracesTemplate is a source code literal of the code for the RemoveBraces function found in embeddeable_funcs.go
var ServerDecodeTemplate = `` /* 1344-byte string literal not displayed */
ServerDecodeTemplate is the template for generating the server-side decoding function for a particular Binding.
var TemplateFuncs = template.FuncMap{ "ToLower": strings.ToLower, "Title": strings.Title, "GoName": gogen.CamelCase, }
TemplateFuncs contains a series of utility functions to be passed into templates and used within those templates.
Functions ¶
func AllFuncSourceCode ¶
AllFuncSourceCode returns the the source code for all the functions defined in the same file as the one provided, including the source of the function provided.
func ApplyTemplate ¶
func ApplyTemplate(name string, tmpl string, executor interface{}, fncs template.FuncMap) (string, error)
ApplyTemplate applies a template with a given name, executor context, and function map. Returns the output of the template on success, returns an error if template failed to execute.
func BuildParamMap ¶
BuildParamMap takes a string representing a url template and returns a map indicating the location of each parameter within that url, where the location is the index as if in a slash-separated sequence of path components. For example, given the url template:
"/v1/{a}/{b}"
The returned param map would look like:
map[string]int { "a": 2, "b": 3, }
func EnglishNumber ¶
EnglishNumber takes an integer and returns the english words that represents that number, in base ten. Examples:
1 -> "One" 5 -> "Five" 10 -> "OneZero" 48 -> "FourEight"
func FormatCode ¶
FormatCode takes a string representing some go code and attempts to format that code. If formating fails, the original source code is returned.
func FuncSourceCode ¶
FuncSourceCode returns a string representing the source code of the function provided to it.
func LowCamelName ¶
LowCamelName returns a CamelCased string, but with the first letter lowercased. "package_name" becomes "packageName".
func PathParams ¶
PathParams takes a url and a gRPC-annotation style url template, and returns a map of the named parameters in the template and their values in the given url.
PathParams does not support the entirety of the URL template syntax defined in third_party/googleapis/google/api/httprule.proto. Only a small subset of the functionality defined there is implemented here.
func QueryParams ¶
QueryParams takes query parameters in the form of url.Values, and returns a bare map of the string representation of each key to the string representation for each value. The representations of repeated query parameters is undefined.
func RemoveBraces ¶
RemoveBraces replace all curly braces in the provided string, opening and closing, with empty strings.
Types ¶
type Binding ¶
type Binding struct { // Label is the name of this method, plus the english word for the index of // this binding in this methods slice of bindings. So if this binding where // the first binding in the slice of bindings for the method "Sum", the // label for this binding would be "SumZero". If it where the third // binding, it would be named "SumTwo". Label string // PathTemplate is the full path template as it appeared in the http // annotation which this binding refers to. PathTemplate string // BasePath is the longest static portion of the full PathTemplate, and is // given to the net/http mux as the path for the route for this binding. BasePath string Verb string Fields []*Field // A pointer back to the parent method of this binding. Used within some // binding methods Parent *Method }
Binding contains the distillation of information within an doctree.HttpBinding that's useful for templating http transport.
func NewBinding ¶
func NewBinding(i int, meth *doctree.ServiceMethod) *Binding
NewBinding creates a Binding struct based on a doctree.HttpBinding. Because NewBinding requires access to some of it's parent method's fields, instead of passing a doctree.HttpBinding directly, you instead pass a doctree.ServiceMethod and the index of the HttpBinding within that methods "HttpBindings" slice.
func (*Binding) GenClientEncode ¶
GenClientEncode returns the generated code for the client-side encoding of that clients request struct into the correctly formatted http request.
func (*Binding) GenServerDecode ¶
GenServerDecode returns the generated code for the server-side decoding of an http request into its request struct.
func (*Binding) PathSections ¶
PathSections returns a slice of strings for templating the creation of a fully assembled URL with the correct fields in the correct locations.
For example, let's say there's a method "Sum" which accepts a "SumRequest", and SumRequest has two fields, 'a' and 'b'. Additionally, lets say that this binding for "Sum" has a path of "/sum/{a}". If we call the PathSection() method on this binding, it will return a slice that looks like the following slice literal:
[]string{ "\"\"", "\"sum\"", "fmt.Sprint(req.A)", }
type Field ¶
type Field struct { Name string // The name of this field, but passed through the CamelCase function. // Removes underscores, adds camelcase; "client_id" becomes "ClientId". CamelName string // The name of this field, but run through the camelcase function and with // the first letter lowercased. "package_name" becomes "packageName". // LowCamelName is how the names of fields should appear when marshaled to // JSON, according to the gRPC language guide. LowCamelName string // The go-compatible name for this variable, for use in auto generated go // code. LocalName string // The location within the the http request that this field is to be found. Location string // The protobuf type that this field is of. ProtobufType string // The type within the Go language that's used to represent the original // field that this field refers to. GoType string // The protobuf label for the original field that this field refers to. Is // probably "OPTIONAL", though may be "REPEATED". ProtobufLabel string // The string form of the function to be used to convert the incoming // string msg from a string into it's intended type. ConvertFunc string // Indicates whether this field represents a basic protobuf type such as // one of the ints, floats, strings, bools, etc. Since we can only create // automatic marshaling of base types, if this is false a warning is given // to the user. IsBaseType bool }
Field contains the distillation of information within an doctree.MessageField that's useful for templating http transport.
type Helper ¶
Helper is the base struct for the data structure containing all the information necessary to correctly template the HTTP transport functionality of a microservice. Helper must be built from a doctree.
func NewHelper ¶
func NewHelper(svc *doctree.ProtoService) *Helper
NewHelper builds a helper struct from a service declaration. The other "New*" functions in this file are there to make this function smaller and more testable.