httpcodec

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 18 Imported by: 15

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedType = errors.New("unsupported type")
)

Functions

func MakeErrorEncoder

func MakeErrorEncoder(codec Codec) kithttp.ErrorEncoder

func MakeResponseEncoder

func MakeResponseEncoder(codec Codec, statusCode int) kithttp.EncodeResponseFunc

Types

type BasicParam

type BasicParam struct{}

BasicParam is a built-in implementation of ParamCodec. It is mainly used to encode and decode a basic value or a slice of basic values.

func (BasicParam) Decode

func (p BasicParam) Decode(in []string, out interface{}) error

Decode decodes a string slice to a basic value (or a slice of basic values).

func (BasicParam) Encode

func (p BasicParam) Encode(in interface{}) (out []string)

Encode encodes a basic value (or a slice of basic values) to a string slice.

type Bodier

type Bodier interface {
	Body() interface{}
}

type Codec

type Codec interface {
	// Encoders and decoders used at the server side.
	DecodeRequestParam(name string, values []string, out interface{}) error
	DecodeRequestParams(name string, values map[string][]string, out interface{}) error
	DecodeRequestBody(r *http.Request, out interface{}) error
	EncodeSuccessResponse(w http.ResponseWriter, statusCode int, body interface{}) error
	EncodeFailureResponse(w http.ResponseWriter, err error) error

	// Encoders and decoders used at the client side.
	EncodeRequestParam(name string, value interface{}) []string
	EncodeRequestParams(name string, value interface{}) map[string][]string
	EncodeRequestBody(body interface{}) (io.Reader, map[string]string, error)
	DecodeSuccessResponse(body io.ReadCloser, out interface{}) error
	DecodeFailureResponse(body io.ReadCloser, out *error) error
}

Codec is a series of codecs (encoders and decoders) for HTTP requests and responses.

type Codecs

type Codecs interface {
	EncodeDecoder(name string) Codec
}

type DefaultCodecs

type DefaultCodecs struct {
	Codecs map[string]Codec
	// contains filtered or unexported fields
}

func NewDefaultCodecs

func NewDefaultCodecs(d Codec, namedCodecs ...NamedCodec) *DefaultCodecs

func (*DefaultCodecs) EncodeDecoder

func (dc *DefaultCodecs) EncodeDecoder(name string) Codec

func (*DefaultCodecs) PatchAll

func (dc *DefaultCodecs) PatchAll(patch func(Codec) *Patcher) *DefaultCodecs

PatchAll patches the default codec and all the custom codecs.

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

type FailureResponse

type FailureResponse struct {
	Error Error `json:"error"`
}

type FormFile

type FormFile struct {
	Name   string
	Header map[string][]string
	Size   int64
	File   io.ReadCloser
}

FormFile describes a file part of a multipart message.

func FromMultipartFileHeader

func FromMultipartFileHeader(fh *multipart.FileHeader) (*FormFile, error)

func FromOSFile

func FromOSFile(name string) (*FormFile, error)

func (*FormFile) Save

func (ff *FormFile) Save(name string) error

type JSON

type JSON struct{}

func (JSON) DecodeFailureResponse

func (j JSON) DecodeFailureResponse(body io.ReadCloser, out *error) error

func (JSON) DecodeRequestBody

func (j JSON) DecodeRequestBody(r *http.Request, out interface{}) error

func (JSON) DecodeRequestParam

func (j JSON) DecodeRequestParam(name string, values []string, out interface{}) error

func (JSON) DecodeRequestParams

func (j JSON) DecodeRequestParams(name string, values map[string][]string, out interface{}) error

func (JSON) DecodeSuccessResponse

func (j JSON) DecodeSuccessResponse(body io.ReadCloser, out interface{}) error

func (JSON) EncodeFailureResponse

func (j JSON) EncodeFailureResponse(w http.ResponseWriter, err error) error

func (JSON) EncodeRequestBody

func (j JSON) EncodeRequestBody(body interface{}) (io.Reader, map[string]string, error)

func (JSON) EncodeRequestParam

func (j JSON) EncodeRequestParam(name string, value interface{}) []string

func (JSON) EncodeRequestParams

func (j JSON) EncodeRequestParams(name string, value interface{}) map[string][]string

func (JSON) EncodeSuccessResponse

func (j JSON) EncodeSuccessResponse(w http.ResponseWriter, statusCode int, body interface{}) error

type MultipartForm

type MultipartForm struct {
	JSON
	// contains filtered or unexported fields
}

func NewMultipartForm

func NewMultipartForm(maxMemory int64) *MultipartForm

func (*MultipartForm) DecodeRequestBody

func (mf *MultipartForm) DecodeRequestBody(r *http.Request, out interface{}) error

func (*MultipartForm) EncodeRequestBody

func (mf *MultipartForm) EncodeRequestBody(in interface{}) (io.Reader, map[string]string, error)

type NamedCodec

type NamedCodec struct {
	Name  string
	Codec Codec
}

NamedCodec holds a codec and its corresponding operation name.

func Op

func Op(name string, codec Codec) NamedCodec

Op is a shortcut for creating an instance of NamedCodec.

type ParamCodec

type ParamCodec interface {
	Decode(in []string, out interface{}) error
	Encode(in interface{}) (out []string)
}

ParamCodec is a codec (encoder and decoder) for a single request parameter.

type ParamsCodec

type ParamsCodec interface {
	Decode(in map[string][]string, out interface{}) error
	Encode(in interface{}) (out map[string][]string)
}

ParamsCodec is a codec (encoder and decoder) for a group of request parameters.

func ToParamsCodec

func ToParamsCodec(codec ParamCodec) ParamsCodec

ToParamsCodec creates a ParamsCodec from a ParamCodec. It is mainly used along with StructParams.

type Patcher

type Patcher struct {
	Codec // the original Codec
	// contains filtered or unexported fields
}

Patcher is used to change the encoding and decoding behaviors of an existing instance of Codec.

func NewPatcher

func NewPatcher(codec Codec) *Patcher

func (*Patcher) DecodeRequestParam

func (p *Patcher) DecodeRequestParam(name string, values []string, out interface{}) error

func (*Patcher) DecodeRequestParams

func (p *Patcher) DecodeRequestParams(name string, values map[string][]string, out interface{}) error

func (*Patcher) EncodeRequestParam

func (p *Patcher) EncodeRequestParam(name string, value interface{}) []string

func (*Patcher) EncodeRequestParams

func (p *Patcher) EncodeRequestParams(name string, value interface{}) map[string][]string

func (*Patcher) Param

func (p *Patcher) Param(name string, pc ParamCodec) *Patcher

Param sets a codec for a request parameter specified by name.

func (*Patcher) Params

func (p *Patcher) Params(name string, psc ParamsCodec) *Patcher

Params sets a codec for a group of request parameters specified by name.

type StructParams

type StructParams struct {
	Fields map[string]ParamsCodec
	// contains filtered or unexported fields
}

StructParams is a built-in implementation of ParamsCodec. It is mainly used to encode and decode a struct. The encoding and decoding of each field can be customized by setting Fields.

func (StructParams) CamelCase

func (p StructParams) CamelCase() StructParams

func (StructParams) Decode

func (p StructParams) Decode(in map[string][]string, out interface{}) error

Decode decodes a string map to a struct (or a *struct).

func (StructParams) Encode

func (p StructParams) Encode(in interface{}) (out map[string][]string)

Encode encodes a struct (or a *struct) to a string map.

Jump to

Keyboard shortcuts

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