Documentation
¶
Overview ¶
Package build contains internal methods for building request parts: query string, headers, body.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNilOpts = errors.New("nil options provided")
ErrNilOpts used to be returned in case opts passed are nil. This can be expected in some cases.
Functions ¶
func Headers ¶
Headers is an internal function to be used by request methods in individual resource packages.
It accepts an arbitrary tagged structure and produces a string map that's suitable for use as the HTTP headers of an outgoing request. Field names are mapped to header names based in "h" tags.
type struct QueryStruct {
Bar string `h:"x_bar"`
Baz int `h:"lorem_ipsum"`
}
instance := QueryStruct{
Bar: "AAA",
Baz: "BBB",
}
will be converted into:
map[string]string{
"x_bar": "AAA",
"lorem_ipsum": "BBB",
}
Untagged fields and fields left at their zero values are skipped. Integers, booleans and string values are supported.
func QueryString ¶
QueryString is an internal function to be used by request methods in individual resource packages.
It accepts a tagged structure and expands it into a URL struct. Field names are converted into query parameters based on a "q" tag. For example:
type QueryStruct struct {
Bar string `q:"x_bar"`
Baz int `q:"lorem_ipsum"`
}
instance := QueryStruct{
Bar: "AAA",
Baz: "BBB",
}
will be converted into "?x_bar=AAA&lorem_ipsum=BBB".
The struct's fields may be strings, integers, or boolean values. Fields left at their type's zero value will be omitted from the query.
func ValidateTags ¶
func ValidateTags(opts interface{}) error
ValidateTags validating structure by tags.
Supported validations:
required (`required:"true"`) - mark field required, returns error if it is empty. or: (`or:"OtherField"`) - requires at least one field to be not empty. xor: (`xor:"OtherField"`) - requires exactly of this and the other field to be set.
Types ¶
type Body ¶
type Body struct {
RootTag string
Wrapped interface{}
}
Body wraps original request data providing root tag support.
Example:
wrapped := structure {
Field string `json:"field"`
} {"data"}
Body{
RootTag: "root",
Wrapped: wrapped,
}
Will produce the following json:
{
"root": {"field": "data"}
}
func RequestBody ¶
RequestBody validates given structure by its tags and build the body ready to be marshalled to the JSON.
func (Body) MarshalJSON ¶
MarshalJSON satisfies json.Marshaler interface.