codec

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2025 License: MIT Imports: 7 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.

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeBase62 added in v1.0.5

func DecodeBase62(s string) ([]byte, error)

DecodeBase62 decodes a base62-encoded string and returns the corresponding bytes.

The base62 encoding uses the characters [0-9, A-Z, a-z], corresponding to values [0..61]. This function treats the first 10 digits ('0'–'9') as values 0–9, the next 26 letters ('A'–'Z') as values 10–35, and the final 26 letters ('a'–'z') as values 36–61.

An error is returned if the input string contains invalid characters.

Example usage:

decoded, err := DecodeBase62("0A1B")
if err != nil {
    log.Fatal(err)
}
fmt.Printf("Decoded bytes: %x\n", decoded)

func DecodeBase64 added in v1.0.5

func DecodeBase64(encoded string) ([]byte, error)

DecodeBase64 decodes a base64-encoded string to bytes. It uses the standard base64 encoding as defined in RFC 4648. This function is used by the router when processing requests with Base64QueryParameter or Base64PathParameter source types.

Parameters:

  • encoded: The base64-encoded string to decode

Returns:

  • []byte: The decoded bytes
  • error: An error if the input is not valid base64

Types

type Codec added in v1.3.1

type Codec[T any, U any] interface {
	// NewRequest creates a new zero-value instance of the request type T.
	// This is used by the framework to get an instance for decoding, avoiding reflection.
	NewRequest() T

	// Decode extracts and deserializes data from an HTTP request body into a value of type T.
	// The type T represents the request data type.
	Decode(r *http.Request) (T, error)

	// DecodeBytes extracts and deserializes data from a byte slice into a value of type T.
	// This is used for source types where the data is already extracted (e.g., query/path parameters).
	// The type T represents the request data type.
	DecodeBytes(data []byte) (T, error)

	// Encode serializes a value of type U and writes it to the HTTP response.
	// It converts the Go value to the wire format (e.g., JSON, Protocol Buffers) and
	// sets appropriate headers (e.g., Content-Type). If the serialization fails, it returns an error.
	// The type U represents the response data type.
	Encode(w http.ResponseWriter, resp U) error
}

Codec defines an interface for marshaling and unmarshaling request and response data. It provides methods for creating new request objects, decoding request data, and encoding response data. This allows for different data formats (e.g., JSON, Protocol Buffers). The framework includes implementations for JSON and Protocol Buffers in the codec package.

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. The returned codec can be used with generic routes to automatically handle JSON marshaling and unmarshaling of request and response data.

Example:

codec := NewJSONCodec[CreateUserReq, CreateUserResp]()

func (*JSONCodec[T, U]) Decode

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

Decode reads and unmarshals JSON data from the HTTP request body into type T. It implements the Codec interface. The entire request body is read and the body is closed after reading. If the JSON is malformed or doesn't match the structure of T, an error is returned along with the zero value of T.

func (*JSONCodec[T, U]) DecodeBytes added in v1.1.3

func (c *JSONCodec[T, U]) DecodeBytes(body []byte) (T, error)

DecodeBytes unmarshals JSON data from a byte slice into type T. It implements the Codec interface. This method is used when the request data comes from sources other than the request body (e.g., base64-encoded query parameters). Returns an error if the JSON is invalid.

func (*JSONCodec[T, U]) Encode

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

Encode marshals the response value of type U to JSON and writes it to the HTTP response. It implements the Codec interface. Sets the Content-Type header to "application/json" before writing the response body. Returns an error if marshaling fails or if writing to the response writer fails.

func (*JSONCodec[T, U]) NewRequest added in v1.1.3

func (c *JSONCodec[T, U]) NewRequest() T

NewRequest creates a new zero-value instance of the request type T. This method is required by the Codec interface and is used internally by the framework to get an instance for decoding without using reflection.

type ProtoCodec

type ProtoCodec[T proto.Message, U proto.Message] struct {
	// contains filtered or unexported fields
}

ProtoCodec implements the Codec interface for Protocol Buffers. It handles marshaling and unmarshaling of protobuf messages for use with generic routes. Both T and U must be pointer types that implement proto.Message (e.g., *MyRequest, *MyResponse).

func NewProtoCodec

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

NewProtoCodec creates a new ProtoCodec instance with the provided request factory. The factory function is used to create new instances of the request type without reflection.

Example:

codec := NewProtoCodec[*pb.CreateUserReq, *pb.CreateUserResp](func() *pb.CreateUserReq {
    return &pb.CreateUserReq{}
})

func (*ProtoCodec[T, U]) Decode

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

Decode reads and unmarshals protobuf data from the HTTP request body into type T. It implements the Codec interface. The entire request body is read and the body is closed after reading. Returns an error if the data is not valid protobuf or doesn't match the expected message type.

func (*ProtoCodec[T, U]) DecodeBytes added in v1.1.3

func (c *ProtoCodec[T, U]) DecodeBytes(data []byte) (T, error)

DecodeBytes unmarshals protobuf data from a byte slice into type T. It implements the Codec interface. This method is used when the request data comes from sources other than the request body (e.g., base64-encoded query parameters). Returns an error if the data is invalid.

func (*ProtoCodec[T, U]) Encode

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

Encode marshals the response protobuf message to binary format and writes it to the HTTP response. It implements the Codec interface. Sets the Content-Type header to "application/x-protobuf" before writing the response body. Returns an error if marshaling fails or if writing to the response writer fails.

func (*ProtoCodec[T, U]) NewRequest added in v1.1.3

func (c *ProtoCodec[T, U]) NewRequest() T

NewRequest creates a new instance of the request protobuf message using the factory. It implements the Codec interface. The factory pattern avoids the need for reflection when creating new message instances.

type ProtoRequestFactory added in v1.1.3

type ProtoRequestFactory[T proto.Message] func() T

ProtoRequestFactory is a function type that creates new instances of protobuf request messages. It returns a pointer to a type implementing proto.Message. This factory pattern is used to avoid reflection when creating new message instances for decoding. T must be a pointer type (e.g., *MyRequest) that implements proto.Message.

Jump to

Keyboard shortcuts

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