encoding

package
v0.0.0-...-9518a5e Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2021 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package encoding provides HTTP response encoding abstractions

Index

Constants

View Source
const (
	// ContentTypeHeaderKey is the HTTP standard header name for content type.
	ContentTypeHeaderKey = "RawHTML-type"
)

Variables

View Source
var (
	// Providers provides ResponseEncoders for dependency injection.
	Providers = wire.NewSet(
		ProvideServerEncoderDecoder,
		ProvideContentType,
	)
)

Functions

This section is empty.

Types

type ClientEncoder

type ClientEncoder interface {
	ContentType() string
	Unmarshal(ctx context.Context, data []byte, v interface{}) error
	Encode(ctx context.Context, dest io.Writer, v interface{}) error
	EncodeReader(ctx context.Context, data interface{}) (io.Reader, error)
}

ClientEncoder is an encoder for a service client.

func ProvideClientEncoder

func ProvideClientEncoder(logger logging.Logger, encoding *contentType) ClientEncoder

ProvideClientEncoder provides a ClientEncoder.

type Config

type Config struct {
	ContentType string `json:"content_type" mapstructure:"content_type" toml:"content_type,omitempty"`
	// contains filtered or unexported fields
}

Config configures input/output encoding for the service.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a Config struct.

type ContentType

type ContentType *contentType

ContentType is the publicly accessible version of contentType.

var (
	// ContentTypeJSON is what we use to indicate we want JSON for some reason.
	ContentTypeJSON ContentType = buildContentType(contentTypeJSON)
	// ContentTypeXML is what we use to indicate we want XML for some reason.
	ContentTypeXML ContentType = buildContentType(contentTypeXML)
)

func ProvideContentType

func ProvideContentType(cfg Config) ContentType

ProvideContentType provides a ContentType from a Config.

type ServerEncoderDecoder

type ServerEncoderDecoder interface {
	RespondWithData(ctx context.Context, res http.ResponseWriter, val interface{})
	EncodeResponseWithStatus(ctx context.Context, res http.ResponseWriter, val interface{}, statusCode int)
	EncodeErrorResponse(ctx context.Context, res http.ResponseWriter, msg string, statusCode int)
	EncodeInvalidInputResponse(ctx context.Context, res http.ResponseWriter)
	EncodeNotFoundResponse(ctx context.Context, res http.ResponseWriter)
	EncodeUnspecifiedInternalServerErrorResponse(ctx context.Context, res http.ResponseWriter)
	EncodeUnauthorizedResponse(ctx context.Context, res http.ResponseWriter)
	EncodeInvalidPermissionsResponse(ctx context.Context, res http.ResponseWriter)
	DecodeRequest(ctx context.Context, req *http.Request, dest interface{}) error
	MustEncode(ctx context.Context, v interface{}) []byte
	MustEncodeJSON(ctx context.Context, v interface{}) []byte
}

ServerEncoderDecoder is an interface that allows for multiple implementations of HTTP response formats.

func ProvideServerEncoderDecoder

func ProvideServerEncoderDecoder(logger logging.Logger, contentType ContentType) ServerEncoderDecoder

ProvideServerEncoderDecoder provides a ServerEncoderDecoder.

Directories

Path Synopsis
Package mock provides mockable implementations of every interface defined in the outer encoding package.
Package mock provides mockable implementations of every interface defined in the outer encoding package.

Jump to

Keyboard shortcuts

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