encoding

package
v0.0.0-...-98cd694 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: Unlicense Imports: 17 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 (
	// EncDecProviders provides ResponseEncoders for dependency injection.
	EncDecProviders = wire.NewSet(
		ProvideServerEncoderDecoder,
		ProvideContentType,
	)
)

Functions

This section is empty.

Types

type ClientEncoder

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

ClientEncoder is an encoder for a service client.

func ProvideClientEncoder

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

ProvideClientEncoder provides a ClientEncoder.

type Config

type Config struct {
	ContentType string `json:"contentType" 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 to indicate we want JSON for some reason.
	ContentTypeJSON ContentType = buildContentType(contentTypeJSON)
	// ContentTypeXML is to indicate we want XML for some reason.
	ContentTypeXML ContentType = buildContentType(contentTypeXML)
	// ContentTypeEmoji is to indicate we want Emoji for some reason.
	ContentTypeEmoji ContentType = buildContentType(contentTypeEmoji)
)

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 any)
	EncodeResponseWithStatus(ctx context.Context, res http.ResponseWriter, val any, 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 any) error
	DecodeBytes(ctx context.Context, payload []byte, dest any) error
	MustEncode(ctx context.Context, v any) []byte
	MustEncodeJSON(ctx context.Context, v any) []byte
}

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

func ProvideServerEncoderDecoder

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

ProvideServerEncoderDecoder provides a ServerEncoderDecoder.

Directories

Path Synopsis
Package mockencoding provides mockable implementations of every interface defined in the outer encoding package.
Package mockencoding 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