Documentation
¶
Overview ¶
Package bodydecoder provides a pluggable body decoding pipeline. Decoders are tried in registration order; first CanDecode match wins.
Index ¶
- Variables
- type Decoder
- type DecoderMetadata
- type Encoder
- type GRPCWebDecoder
- func (d *GRPCWebDecoder) CanDecode(contentType string) bool
- func (d *GRPCWebDecoder) CanEncode(contentType string) bool
- func (d *GRPCWebDecoder) Decode(body []byte, meta DecoderMetadata) (string, string, error)
- func (d *GRPCWebDecoder) Encode(jsonBody []byte, contentType string, meta DecoderMetadata) ([]byte, error)
- type HostAwareRegistry
- type ProtoRegResolver
- type ProtoRegistry
- func (r *ProtoRegistry) DecodeNamed(body []byte, requestPath string, isRequest bool) (string, error)
- func (r *ProtoRegistry) EncodeNamed(jsonBody []byte, requestPath string, isRequest bool) ([]byte, error)
- func (r *ProtoRegistry) HasMethods() bool
- func (r *ProtoRegistry) LookupMethod(requestPath string) (protoreflect.MethodDescriptor, bool)
- type RawProtobufDecoder
- func (d *RawProtobufDecoder) CanDecode(contentType string) bool
- func (d *RawProtobufDecoder) CanEncode(contentType string) bool
- func (d *RawProtobufDecoder) Decode(body []byte, meta DecoderMetadata) (string, string, error)
- func (d *RawProtobufDecoder) Encode(jsonBody []byte, _ string, meta DecoderMetadata) ([]byte, error)
- type Registry
Constants ¶
This section is empty.
Variables ¶
var ErrNoDecoder = errors.New("no decoder matched content type")
ErrNoDecoder is returned when no registered decoder matches the content type.
var ErrNoEncoder = errors.New("no encoder matched content type")
ErrNoEncoder is returned when no registered decoder also implements Encoder for the given 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
Host string // request host, used for per-host proto resolution
IsRequest bool // true = request body, false = response body
OriginalBody []byte // set during re-encode so encoders can preserve non-data frames
}
DecoderMetadata carries per-request context that decoders may use for message type resolution (e.g. gRPC service/method from path).
type Encoder ¶ added in v0.1.12
type Encoder interface {
CanEncode(contentType string) bool
Encode(jsonBody []byte, contentType string, meta DecoderMetadata) ([]byte, error)
}
Encoder encodes a JSON body back into wire format. Decoders that support round-tripping implement both Decoder and Encoder.
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) CanEncode ¶ added in v0.1.12
func (d *GRPCWebDecoder) CanEncode(contentType string) bool
func (*GRPCWebDecoder) Decode ¶
func (d *GRPCWebDecoder) Decode(body []byte, meta DecoderMetadata) (string, string, error)
func (*GRPCWebDecoder) Encode ¶ added in v0.1.12
func (d *GRPCWebDecoder) Encode(jsonBody []byte, contentType string, meta DecoderMetadata) ([]byte, error)
type HostAwareRegistry ¶ added in v0.1.13
type HostAwareRegistry struct {
// contains filtered or unexported fields
}
HostAwareRegistry maps host patterns to per-host ProtoRegistry instances and falls back to a default registry when no pattern matches.
func NewHostAwareRegistry ¶ added in v0.1.13
func NewHostAwareRegistry(fallback *ProtoRegistry, hosts map[string]*ProtoRegistry) *HostAwareRegistry
NewHostAwareRegistry creates a registry that resolves by host pattern. The fallback is used when no pattern matches.
func (*HostAwareRegistry) Resolve ¶ added in v0.1.13
func (r *HostAwareRegistry) Resolve(host string) *ProtoRegistry
Resolve returns the ProtoRegistry for the given host. Exact match is tried first, then wildcard patterns (*.example.com). Falls back to the default registry if nothing matches.
func (*HostAwareRegistry) Resolver ¶ added in v0.1.13
func (r *HostAwareRegistry) Resolver() ProtoRegResolver
Resolver returns a ProtoRegResolver func backed by this HostAwareRegistry.
type ProtoRegResolver ¶ added in v0.1.13
type ProtoRegResolver func(host string) *ProtoRegistry
ProtoRegResolver returns the ProtoRegistry for a given host.
func StaticResolver ¶ added in v0.1.13
func StaticResolver(reg *ProtoRegistry) ProtoRegResolver
StaticResolver returns a ProtoRegResolver that always returns the given registry.
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, includeDirs ...string) (*ProtoRegistry, []error)
LoadProtoFiles compiles .proto files from the given paths into a registry. Paths may be files or directories (recursively globbed for *.proto). Extra includeDirs are added to the import search path (like protoc -I). 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) EncodeNamed ¶ added in v0.1.12
func (r *ProtoRegistry) EncodeNamed(jsonBody []byte, requestPath string, isRequest bool) ([]byte, error)
EncodeNamed encodes a JSON body back into protobuf wire format using the named method descriptor. 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 {
// ResolveReg returns the ProtoRegistry for a given host.
// When nil, named decode/encode is unavailable.
ResolveReg ProtoRegResolver
}
RawProtobufDecoder decodes protobuf wire format into a JSON-like representation using field numbers as keys. When ResolveReg 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) CanEncode ¶ added in v0.1.12
func (d *RawProtobufDecoder) CanEncode(contentType string) bool
func (*RawProtobufDecoder) Decode ¶
func (d *RawProtobufDecoder) Decode(body []byte, meta DecoderMetadata) (string, string, error)
func (*RawProtobufDecoder) Encode ¶ added in v0.1.12
func (d *RawProtobufDecoder) Encode(jsonBody []byte, _ string, meta DecoderMetadata) ([]byte, 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 ¶
NewRegistry creates a Registry with the given decoders tried in order.