codec

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package codec provides encoding and decoding functionality for different data formats.

Package codec provides encoding and decoding functionality for different data formats.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSONCodec

type JSONCodec[T any, U any] struct {
}

JSONCodec is a codec that uses JSON for marshaling and unmarshaling. It implements the Codec interface for encoding responses and decoding requests.

func NewJSONCodec

func NewJSONCodec[T any, U any]() *JSONCodec[T, U]

NewJSONCodec creates a new JSONCodec instance for the specified types. T represents the request type and U represents the response type.

func (*JSONCodec[T, U]) Decode

func (c *JSONCodec[T, U]) Decode(r *http.Request) (T, error)

Decode decodes the request body into a value of type T. It reads the entire request body and unmarshals it from JSON.

func (*JSONCodec[T, U]) Encode

func (c *JSONCodec[T, U]) Encode(w http.ResponseWriter, resp U) error

Encode encodes a value of type U into the response. It marshals the value to JSON and writes it to the response with the appropriate content type.

type ProtoCodec

type ProtoCodec[T proto.Message, U proto.Message] struct{}

Force T to be a pointer to a type implementing proto.Message.

func NewProtoCodec

func NewProtoCodec[T proto.Message, U proto.Message]() *ProtoCodec[T, U]

NewProtoCodec creates a new ProtoCodec instance.

func (*ProtoCodec[T, U]) Decode

func (c *ProtoCodec[T, U]) Decode(r *http.Request) (T, error)

Decode reads the request body and unmarshals into T (which is a pointer).

func (*ProtoCodec[T, U]) Encode

func (c *ProtoCodec[T, U]) Encode(w http.ResponseWriter, resp U) error

Encode marshals U (also a pointer type) and writes to response.

Jump to

Keyboard shortcuts

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