codec

package module
v0.0.0-...-5f13e0e Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2025 License: MIT Imports: 6 Imported by: 0

README

codec-json-go

Go Reference GoReportCard codecov

codec-json-go provides a JSON-based codec for cmd-stream-go.

It maps concrete Command and Result types to internal identifiers, allowing type-safe serialization across network boundaries.

How To

import (
  "reflect"
  codec "github.com/cmd-stream/codec-json-go"
)

var (
  // Note: The order of types matters — two codecs created with the same types
  // in a different order are not considered equal.
  cmdTypes = []reflect.Type{
    reflect.TypeFor[YourCmd](),
    // ...
  }
  resultTypes = []reflect.Type{
    reflect.TypeFor[YourResult](),
    // ...
  }
  serverCodec = codec.NewServerCodec(cmdTypes, resultTypes)
  clientCodec = codec.NewClientCodec(cmdTypes, resultTypes)
)

Documentation

Overview

Package codec provides a JSON-based codec implementation for cmd-stream-go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec[T, V any] struct {
	codecgnrc.Codec[T, V]
}

Codec represents a generic type-safe codec for encoding and decoding values. T is the type used for encoding, V is the type used for decoding.

func NewClientCodec

func NewClientCodec[T any](cmdTypes []reflect.Type, resultTypes []reflect.Type) (
	codec Codec[core.Cmd[T], core.Result],
)

NewClientCodec creates a JSON Codec for the client side.

The cmdTypes slice lists Command types the client can send. The resultTypes slice lists Result types the client expects to receive.

Note: The order of types matters — two codecs created with the same types in a different order are not considered equal.

func NewCodec

func NewCodec[T, V any](types1 []reflect.Type,
	types2 []reflect.Type,
) Codec[T, V]

NewCodec constructs a JSON Codec.

Parameters:

  • types1 lists the Go types that can be encoded.
  • types2 lists the Go types that can be decoded.

func NewServerCodec

func NewServerCodec[T any](cmdTypes []reflect.Type, resultTypes []reflect.Type) (
	codec Codec[core.Result, core.Cmd[T]],
)

NewServerCodec creates a JSON Codec for the server side.

The cmdTypes slice lists Command types the server can handle. The resultTypes slice lists Result types that can be returned to the client.

Note: The order of types matters — two codecs created with the same types in a different order are not considered equal.

func (Codec[T, V]) Decode

func (c Codec[T, V]) Decode(r transport.Reader) (v V, n int, err error)

Decode reads a value of type V from the given transport.Reader. Returns the decoded value, total bytes read, and any error.

func (Codec[T, V]) Encode

func (c Codec[T, V]) Encode(t T, w transport.Writer) (n int, err error)

Encode writes a value of type T to the given transport.Writer. Returns the total number of bytes written and any error.

type Serializer

type Serializer[T, V any] struct{}

func (Serializer[T, V]) Marshal

func (s Serializer[T, V]) Marshal(t T) (bs []byte, err error)

func (Serializer[T, V]) Unmarshal

func (s Serializer[T, V]) Unmarshal(bs []byte, v V) (err error)

Jump to

Keyboard shortcuts

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