codec

package
v1.1.19 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 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 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]) DecodeBytes added in v1.1.3

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

DecodeBytes decodes a byte slice into a value of type T. It unmarshals the byte slice 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.

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.

type ProtoCodec

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

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

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.

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]) DecodeBytes added in v1.1.3

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

DecodeBytes unmarshals a byte slice 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.

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

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

NewRequest creates a new zero-value instance of the request type T using the factory.

type ProtoRequestFactory added in v1.1.3

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

ProtoRequestFactory is a function type that creates a new instance of the request type T. T must be a pointer to a type implementing proto.Message.

Jump to

Keyboard shortcuts

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