codec

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentStream   = "application/octet-stream"
	ContentJSON     = "application/json"
	ContentJSONL    = "application/jsonl"
	ContentYAML     = "application/x-yaml"
	ContentPNG      = "image/png"
	ContentJPG      = "image/jpeg"
	ContentText     = "text/plain"
	ContentMarkdown = "text/markdown"
	ContentEOF      = "application/x-eof" // Signals end of stream
)

Content type constants

Variables

View Source
var Default = NewRegistry()

Default is the global default registry

Functions

This section is empty.

Types

type BinaryCodec

type BinaryCodec struct{}

func (*BinaryCodec) ContentType

func (c *BinaryCodec) ContentType() string

func (*BinaryCodec) Decode

func (c *BinaryCodec) Decode(r io.Reader) (any, error)

func (*BinaryCodec) Encode

func (c *BinaryCodec) Encode(w io.Writer, data any) error

func (*BinaryCodec) Extensions

func (c *BinaryCodec) Extensions() []string

type Codec

type Codec interface {
	// ContentType returns the MIME type this codec handles
	ContentType() string

	// Decode reads from io.Reader and converts to Go value
	Decode(r io.Reader) (any, error)

	// Encode converts Go value to bytes and writes to io.Writer
	Encode(w io.Writer, data any) error

	// Extensions returns common file extensions for this type
	Extensions() []string
}

Codec handles encoding/decoding for a specific content type.

func NewBinaryCodec

func NewBinaryCodec() Codec

func NewImageCodec

func NewImageCodec(contentType string) Codec

func NewJSONCodec

func NewJSONCodec(pretty bool) Codec

func NewJSONLCodec

func NewJSONLCodec() Codec

func NewPrettyJSONCodec

func NewPrettyJSONCodec(color bool, indent int) Codec

func NewTextCodec

func NewTextCodec(mime string, ext string) Codec

func NewYAMLCodec

func NewYAMLCodec() Codec

type ImageCodec

type ImageCodec struct {
	// contains filtered or unexported fields
}

func (*ImageCodec) ContentType

func (c *ImageCodec) ContentType() string

func (*ImageCodec) Decode

func (c *ImageCodec) Decode(r io.Reader) (any, error)

func (*ImageCodec) Encode

func (c *ImageCodec) Encode(w io.Writer, data any) error

func (*ImageCodec) Extensions

func (c *ImageCodec) Extensions() []string

type JSONCodec

type JSONCodec struct {
	// contains filtered or unexported fields
}

func (*JSONCodec) ContentType

func (c *JSONCodec) ContentType() string

func (*JSONCodec) Decode

func (c *JSONCodec) Decode(r io.Reader) (any, error)

func (*JSONCodec) Encode

func (c *JSONCodec) Encode(w io.Writer, data any) error

func (*JSONCodec) Extensions

func (c *JSONCodec) Extensions() []string

type JSONLCodec

type JSONLCodec struct{}

func (*JSONLCodec) ContentType

func (c *JSONLCodec) ContentType() string

func (*JSONLCodec) Decode

func (c *JSONLCodec) Decode(r io.Reader) (any, error)

func (*JSONLCodec) Encode

func (c *JSONLCodec) Encode(w io.Writer, data any) error

func (*JSONLCodec) Extensions

func (c *JSONLCodec) Extensions() []string

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages codec instances.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new codec registry

func (*Registry) Decode

func (r *Registry) Decode(doc io.Reader, contentType string) (any, error)

Decode decodes a document using appropriate codec

func (*Registry) DetectContentType

func (r *Registry) DetectContentType(path string) string

DetectContentType detects content type from file path

func (*Registry) Encode

func (r *Registry) Encode(data any, contentType string) (io.Reader, error)

Encode encodes data to a new document

func (*Registry) Get

func (r *Registry) Get(contentType string) (Codec, bool)

Get returns codec for content type

func (*Registry) GetByExtension

func (r *Registry) GetByExtension(ext string) (Codec, bool)

GetByExtension returns codec for file extension

func (*Registry) GetExtension

func (r *Registry) GetExtension(contentType string) string

GetExtension returns the preferred file extension for a content type. Returns empty string if content type is not registered.

func (*Registry) Register

func (r *Registry) Register(c Codec)

Register adds a codec to the registry

type TextCodec

type TextCodec struct {
	// contains filtered or unexported fields
}

func (*TextCodec) ContentType

func (c *TextCodec) ContentType() string

func (*TextCodec) Decode

func (c *TextCodec) Decode(r io.Reader) (any, error)

func (*TextCodec) Encode

func (c *TextCodec) Encode(w io.Writer, data any) error

func (*TextCodec) Extensions

func (c *TextCodec) Extensions() []string

type YAMLCodec

type YAMLCodec struct{}

func (*YAMLCodec) ContentType

func (c *YAMLCodec) ContentType() string

func (*YAMLCodec) Decode

func (c *YAMLCodec) Decode(r io.Reader) (any, error)

func (*YAMLCodec) Encode

func (c *YAMLCodec) Encode(w io.Writer, data any) error

func (*YAMLCodec) Extensions

func (c *YAMLCodec) Extensions() []string

Jump to

Keyboard shortcuts

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