codec

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 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 any, U any] struct {
}

ProtoCodec is a codec that uses Protocol Buffers for marshaling and unmarshaling. This is a basic implementation that requires the types T and U to implement the proto.Message interface and have Marshal/Unmarshal methods. It implements the Codec interface for encoding responses and decoding requests.

func NewProtoCodec

func NewProtoCodec[T any, U any]() *ProtoCodec[T, U]

NewProtoCodec creates a new ProtoCodec instance for the specified types. T represents the request type and U represents the response type. Both types must implement the appropriate Protocol Buffers methods.

func (*ProtoCodec[T, U]) Decode

func (c *ProtoCodec[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 Protocol Buffers format. The type T must implement the Unmarshal method.

func (*ProtoCodec[T, U]) Encode

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

Encode encodes a value of type U into the response. It marshals the value to Protocol Buffers format and writes it to the response with the appropriate content type. The type U must implement the Marshal method.

Jump to

Keyboard shortcuts

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