encoding

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: AGPL-3.0 Imports: 21 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 = "Content-type"
)

Variables

Functions

func ContentTypeToString

func ContentTypeToString(c ContentType) string

ContentTypeToString allows a content type to be converted to a string.

func Decode

func Decode(data []byte, ct *contentType, dest any) error

func DecodeJSON

func DecodeJSON(data []byte, dest any) error

func MustDecode

func MustDecode(data []byte, ct *contentType, dest any)

MustDecode encodes a given piece of data to a given encoding.

func MustDecodeJSON

func MustDecodeJSON(data []byte, dest any)

MustDecodeJSON JSON encodes a piece of data.

func MustEncode

func MustEncode(data any, ct *contentType) []byte

MustEncode encodes a given piece of data to a given encoding.

func MustEncodeJSON

func MustEncodeJSON(data any) []byte

MustEncodeJSON JSON encodes a piece of data.

func MustJSONIntoReader

func MustJSONIntoReader(data any) io.Reader

MustJSONIntoReader JSON encodes a piece of data.

func RegisterServerEncoderDecoder

func RegisterServerEncoderDecoder(i do.Injector)

RegisterServerEncoderDecoder registers a ContentType and ServerEncoderDecoder with the injector.

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 `env:"CONTENT_TYPE" json:"contentType"`
	// 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)
	// ContentTypeTOML is to indicate we want TOML for some reason.
	ContentTypeTOML ContentType = buildContentType(contentTypeTOML)
	// ContentTypeYAML is to indicate we want YAML for some reason.
	ContentTypeYAML ContentType = buildContentType(contentTypeYAML)
	// 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 {
	EncodeResponseWithStatus(ctx context.Context, res http.ResponseWriter, val any, statusCode int)
	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 moq-generated mocks for the encoding package.
Package mockencoding provides moq-generated mocks for the encoding package.

Jump to

Keyboard shortcuts

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