codec

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package codec provides an HTTP middleware that performs automatic content negotiation using the encoding registry.

The middleware inspects the incoming Content-Type header, resolves it to a registered encoding.Codec, and stores that codec in the request context. Handler code can then use the helper functions FromContext, ReadBody, and Respond to unmarshal requests and marshal responses without hardcoding a specific serialization format.

Usage:

import (
    "github.com/tx7do/go-wind-plugins/transport/http/middleware/codec"
    _ "github.com/tx7do/go-wind-plugins/encoding/json"  // register JSON codec
    _ "github.com/tx7do/go-wind-plugins/encoding/xml"   // register XML codec
)

srv.Use(codec.Middleware())

// In a handler:
func(w http.ResponseWriter, r *http.Request) {
    var req MyRequest
    if err := codec.ReadBody(r, &req); err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    resp := process(&req)
    codec.Respond(w, r, http.StatusOK, resp)
}

Index

Constants

This section is empty.

Variables

View Source
var ErrCodecNotFound = errors.New("codec: no codec found in context (middleware not registered?)")

ErrCodecNotFound is returned when no codec is registered in the context.

Functions

func FromContext

func FromContext(ctx context.Context) encoding.Codec

FromContext returns the encoding.Codec stored in the request context. Returns nil if the middleware was not registered.

func Middleware

func Middleware(opts ...Option) httpPlugin.Middleware

Middleware returns a httpPlugin.Middleware that resolves the request's Content-Type header to an encoding.Codec and stores it in the request context.

Handlers retrieve the codec with FromContext and use ReadBody / Respond for automatic (un)marshalling.

func ReadBody

func ReadBody(r *http.Request, target any) error

ReadBody reads the request body and unmarshals it into target using the codec from the request context.

The request body is consumed and replaced with a new reader so that downstream code can still access it if needed.

func RegisterMimeType

func RegisterMimeType(mime, codecName string)

RegisterMimeType associates a MIME type with a codec name. This allows applications to add custom MIME type mappings.

func Respond

func Respond(w http.ResponseWriter, r *http.Request, statusCode int, data any) error

Respond marshals data with the codec from the request context and writes it to the response with an appropriate Content-Type header.

Types

type Option

type Option func(*options)

Option configures the codec middleware.

func WithDefaultCodec

func WithDefaultCodec(name string) Option

WithDefaultCodec sets the fallback codec name used when Content-Type is absent or unrecognised.

Jump to

Keyboard shortcuts

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