httptransport

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package httptransport provides functions and template helpers for templating the http-transport of a go-kit based service.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
var TemplateFuncs = template.FuncMap{
	"ToLower":  strings.ToLower,
	"ToUpper":  strings.ToUpper,
	"Title":    strings.Title,
	"GoName":   gogen.CamelCase,
	"Contains": strings.Contains,
}

TemplateFuncs contains a series of utility functions to be passed into templates and used within those templates.

Functions

func AllFuncSourceCode

func AllFuncSourceCode(val interface{}) (string, error)

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

func BuildParamMap(urlTmpl string) map[string]int

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

func EnglishNumber(i int) string

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

func FormatCode(code string) string

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

func FuncSourceCode(val interface{}) (string, error)

FuncSourceCode returns a string representing the source code of the function provided to it.

func GenClientTemplate

func GenClientTemplate(exec interface{}) (string, error)

func GenServerTemplate

func GenServerTemplate(exec interface{}) (string, error)

func LowCamelName

func LowCamelName(s string) string

LowCamelName returns a CamelCased string, but with the first letter lowercased. "example_name" becomes "exampleName".

func PathParams

func PathParams(url string, urlTmpl string) (map[string]string, error)

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 RemoveBraces

func RemoveBraces(val string) string

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
	OneofFields []*OneofField
	// 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 svcdef.HTTPBinding that's useful for templating http transport.

func NewBinding

func NewBinding(i int, meth *svcdef.ServiceMethod) *Binding

NewBinding creates a Binding struct based on a svcdef.HTTPBinding. Because NewBinding requires access to some of it's parent method's fields, instead of passing a svcdef.HttpBinding directly, you instead pass a svcdef.ServiceMethod and the index of the HTTPBinding within that methods "HTTPBinding" slice.

func (*Binding) GenClientEncode

func (b *Binding) GenClientEncode() (string, error)

GenClientEncode returns the generated code for the client-side encoding of that clients request struct into the correctly formatted http request.

func (*Binding) GenServerDecode

func (b *Binding) GenServerDecode() (string, error)

GenServerDecode returns the generated code for the server-side decoding of an http request into its request struct.

func (*Binding) PathSections

func (b *Binding) PathSections() []string

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
	QueryParamName 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. "example_name" becomes "exampleName".
	// 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 type within the Go language that's used to represent the original
	// field that this field refers to.
	GoType 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
	// Used in determining if a convert func will need error checking logic
	ConvertFuncNeedsErrorCheck bool
	// The string form of a type cast from 64 to 32bit if the GoType is 32bit
	// as the ConvertFunc will always use return a 64bit type
	TypeConversion 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
	// Protobuf Enums need to be handled uniquely when parsing queryparameters
	IsEnum bool
	// Repeated is true if this arg corresponds to a protobuf field which is
	// given an identifier of "repeated", meaning it will represented in Go as
	// a slice of it's type.
	Repeated bool

	ZeroValue string
}

Field contains the distillation of information within an svcdef.Field that's useful for templating http transport.

func (*Field) GenQueryUnmarshaler

func (f *Field) GenQueryUnmarshaler() (string, error)

GenQueryUnmarshaler returns the generated code for server-side unmarshaling of a query parameter into it's correct field on the request struct.

type Helper

type Helper struct {
	Methods        []*Method
	ServerTemplate func(interface{}) (string, error)
	ClientTemplate func(interface{}) (string, error)
}

Helper is the base struct for the data structure containing all the information necessary to correctly template the HTTP transport functionality of a service. Helper must be built from a Svcdef.

func NewHelper

func NewHelper(svc *svcdef.Service) *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.

type Method

type Method struct {
	Name string
	// RequestType is the name of type of the Request, e.g. *EchoRequest
	RequestType  string
	ResponseType string
	Bindings     []*Binding
}

Method contains the distillation of information within an svcdef.ServiceMethod that's useful for templating http transport.

func NewMethod

func NewMethod(meth *svcdef.ServiceMethod) *Method

NewMethod builds a Method struct from a svcdef.ServiceMethod.

type OneofField added in v0.7.3

type OneofField struct {
	Name     string
	Location string
	Options  []Field
}

OneofField contains the distillation of information within an []*svcdef.Field representing a single oneof field from protobuf that is useful for templating http transport.

func (*OneofField) GenQueryUnmarshaler added in v0.7.3

func (f *OneofField) GenQueryUnmarshaler() (string, error)

GenQueryUnmarshaler returns the generated code for server-side unmarshaling of a query parameter into it's correct field on the request struct.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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