Documentation
¶
Overview ¶
Package encoding provides HTTP response encoding abstractions
Index ¶
- Constants
- Variables
- func ContentTypeToString(c ContentType) string
- func Decode(data []byte, ct *contentType, dest any) error
- func DecodeJSON(data []byte, dest any) error
- func MustDecode(data []byte, ct *contentType, dest any)
- func MustDecodeJSON(data []byte, dest any)
- func MustEncode(data any, ct *contentType) []byte
- func MustEncodeJSON(data any) []byte
- func MustJSONIntoReader(data any) io.Reader
- func RegisterServerEncoderDecoder(i do.Injector)
- type ClientEncoder
- type Config
- type ContentType
- type ServerEncoderDecoder
Constants ¶
const (
// ContentTypeHeaderKey is the HTTP standard header name for content type.
ContentTypeHeaderKey = "Content-type"
)
Variables ¶
var ( ContentTypes = []ContentType{ ContentTypeJSON, ContentTypeXML, ContentTypeTOML, ContentTypeYAML, ContentTypeEmoji, } )
var ( // Providers provides ResponseEncoders for dependency injection. Providers = wire.NewSet( ProvideServerEncoderDecoder, ProvideContentType, ) )
Functions ¶
func ContentTypeToString ¶
func ContentTypeToString(c ContentType) string
ContentTypeToString allows a content type to be converted to a string.
func DecodeJSON ¶
func MustDecode ¶
MustDecode encodes a given piece of data to a given encoding.
func MustDecodeJSON ¶
MustDecodeJSON JSON encodes a piece of data.
func MustEncode ¶
MustEncode encodes a given piece of data to a given encoding.
func MustEncodeJSON ¶
MustEncodeJSON JSON encodes a piece of data.
func MustJSONIntoReader ¶
MustJSONIntoReader JSON encodes a piece of data.
func RegisterServerEncoderDecoder ¶
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.
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.