bodydecoder

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package bodydecoder provides a pluggable body decoding pipeline. Decoders are tried in registration order; first CanDecode match wins.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoDecoder = errors.New("no decoder matched content type")

ErrNoDecoder is returned when no registered decoder matches the content type.

Functions

This section is empty.

Types

type Decoder

type Decoder interface {
	// CanDecode reports whether this decoder handles the given content type.
	CanDecode(contentType string) bool

	// Decode decodes body bytes and returns the decoded text plus a
	// resultContentType hint for downstream syntax highlighting
	// (e.g. "application/json").
	Decode(body []byte, metadata DecoderMetadata) (decoded string, resultContentType string, err error)
}

Decoder decodes a body from wire format into a human-readable string.

type DecoderMetadata

type DecoderMetadata struct {
	RequestPath string // e.g. /api/v1/package.Service/Method
	IsRequest   bool   // true = request body, false = response body
}

DecoderMetadata carries per-request context that decoders may use for message type resolution (e.g. gRPC service/method from path).

type GRPCWebDecoder

type GRPCWebDecoder struct {
	Proto *RawProtobufDecoder
}

GRPCWebDecoder strips gRPC-Web frame headers and delegates the extracted payload to a protobuf decoder.

func (*GRPCWebDecoder) CanDecode

func (d *GRPCWebDecoder) CanDecode(contentType string) bool

func (*GRPCWebDecoder) Decode

func (d *GRPCWebDecoder) Decode(body []byte, meta DecoderMetadata) (string, string, error)

type ProtoRegistry

type ProtoRegistry struct {
	// contains filtered or unexported fields
}

ProtoRegistry holds compiled proto file descriptors and provides service/method lookup for named message decoding.

func LoadProtoFiles

func LoadProtoFiles(paths []string) (*ProtoRegistry, []error)

LoadProtoFiles compiles .proto files from the given paths into a registry. Paths may be files or directories (recursively globbed for *.proto). Returns the registry and any non-fatal errors (bad files are skipped).

func (*ProtoRegistry) DecodeNamed

func (r *ProtoRegistry) DecodeNamed(body []byte, requestPath string, isRequest bool) (string, error)

DecodeNamed attempts to decode body as a named protobuf message. isRequest selects input vs output message type.

func (*ProtoRegistry) HasMethods

func (r *ProtoRegistry) HasMethods() bool

HasMethods returns true if any service methods were loaded.

func (*ProtoRegistry) LookupMethod

func (r *ProtoRegistry) LookupMethod(requestPath string) (protoreflect.MethodDescriptor, bool)

LookupMethod finds a method descriptor by matching the gRPC path suffix. The path format is /{prefix}/{package.Service}/{Method}; the last two segments are used for lookup.

type RawProtobufDecoder

type RawProtobufDecoder struct {
	ProtoReg *ProtoRegistry
}

RawProtobufDecoder decodes protobuf wire format into a JSON-like representation using field numbers as keys. When ProtoReg is set and the request has a gRPC path, it attempts named message decode first.

func (*RawProtobufDecoder) CanDecode

func (d *RawProtobufDecoder) CanDecode(contentType string) bool

func (*RawProtobufDecoder) Decode

func (d *RawProtobufDecoder) Decode(body []byte, meta DecoderMetadata) (string, string, error)

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry holds an ordered list of decoders and routes decode requests to the first matching decoder.

func NewRegistry

func NewRegistry(decoders ...Decoder) *Registry

NewRegistry creates a Registry with the given decoders tried in order.

func (*Registry) Decode

func (r *Registry) Decode(body []byte, contentType string, meta DecoderMetadata) (string, string, error)

Decode finds the first decoder that can handle contentType and delegates to it. Returns ErrNoDecoder if no decoder matches.

Jump to

Keyboard shortcuts

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