build

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package build contains internal methods for building request parts: query string, headers, body.

Index

Constants

This section is empty.

Variables

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

func Headers(opts interface{}) (map[string]string, error)

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

func QueryString(opts interface{}) (*url.URL, error)

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

func RequestBody(opts interface{}, parent string) (*Body, error)

RequestBody validates given structure by its tags and build the body ready to be marshalled to the JSON.

func (Body) MarshalJSON

func (r Body) MarshalJSON() ([]byte, error)

MarshalJSON satisfies json.Marshaler interface.

func (Body) String

func (r Body) String() string

String allows simple pretty-print of prepared value.

Jump to

Keyboard shortcuts

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