dcodec

package module
v0.0.0-...-126f512 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2025 License: MIT Imports: 8 Imported by: 0

README

dtm-codec-go

Go Reference GoReportCard codecov

dtm-codec-go provides client and server codecs for cmd-stream-go.

How To

  1. Define DTMs:
import (
	com "github.com/mus-format/common-go"
)

const(
  Cmd1DTM com.DTM = iota
  Cmd2DTM
  ...
)

const(
  Result1DTM com.DTM = iota
  Result2DTM
  ...
)
  1. Create DTM support variables for Commands and Results using mus-stream-dts-go:
import (
  dts "github.com/mus-format/mus-stream-dts-go"
)

var (
  Cmd1DTS = dts.New[Cmd1DTS](Cmd1DTM, ...)
  Cmd2DTS = dts.New[Cmd2DTS](Cmd1DTM, ...)
)

var (
  Result1DTS = dts.New[Result1DTS](Result1DTM, ...)
  Result2DTS = dts.New[Result2DTS](Result1DTM, ...)
)
  1. Create codecs:
import (
  dcodec "github.com/cmd-stream/dtm-codec-go"
  "github.com/cmd-stream/base-go"
)

...
clientCodec, err := dcodec.NewClientCodec[Receiver](
  []dcodec.Unmarshaller[base.Result]{ // Elements must be arranged in ascending 
  // order based on their DTM values.
    dcodec.NewResultDTSAdapter(Result1DTS), // DTM == 0
    dcodec.NewResultDTSAdapter(Result2DTS), // DTM == 1
    ...
  },
)
...

serverCodec, err := dcodec.NewServerCodec(
  []dcodec.Unmarshaller[base.Cmd[Receiver]]{ // Elements must be arranged in ascending 
  // order based on their DTM values.
    dcodec.NewCmdDTSAdapter(Cmd1DTS), // DTM == 0
    dcodec.NewCmdDTSAdapter(Cmd2DTS), // DTM == 1
    ...
  },
)
...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmptySliceErr = errors.New("empty slice")

EmptySliceErr occurs when the Codec is initialized with an empty slice.

Functions

func NewDTMNotEqualIndexError

func NewDTMNotEqualIndexError(dtm com.DTM, index int) error

NewDTMNotEqualIndexError creates an error is the cause of NewIncorrectUnmarshallersError.

func NewIncorrectUnmarshallersError

func NewIncorrectUnmarshallersError(cause error) error

NewIncorrectUnmarshallersError creates an error that occurs when the codec is initialized with an incorrect slice.

func NewNilItemError

func NewNilItemError(index int) error

NewNilItemError creates an error is the cause of NewIncorrectUnmarshallersError.

func NewNotMarshallerError

func NewNotMarshallerError[T any](t T) error

NewNotMarshallerError creates an error that occurs when Codec.Encode parameter doesn't implement the Marshaller interface.

func NewUnexpectedDTMError

func NewUnexpectedDTMError(dtm com.DTM) error

NewUnexpectedDTMError creates an error that occurs when Codec.Decode encounters an unexpected DTM.

Types

type CmdDTSAdapter

type CmdDTSAdapter[T base.Cmd[V], V any] struct {
	// contains filtered or unexported fields
}

CmdDTSAdapter implements the Unmarshaller interface and serves as an adapter for Command DTS.

func NewCmdDTSAdapter

func NewCmdDTSAdapter[T base.Cmd[V], V any](d dts.DTS[T]) CmdDTSAdapter[T, V]

Creates a new CmdDTSAdapter.

func (CmdDTSAdapter[T, V]) DTM

func (u CmdDTSAdapter[T, V]) DTM() com.DTM

func (CmdDTSAdapter[T, V]) Unmarshal

func (u CmdDTSAdapter[T, V]) Unmarshal(r muss.Reader) (v base.Cmd[V], n int,
	err error)

type Codec

type Codec[T, V any] struct {
	// contains filtered or unexported fields
}

Codec implements the cmd-stream general Codec interface.

func New

func New[T, V any](us []Unmarshaller[V]) (codec Codec[T, V], err error)

New creates a new Codec.

func NewClientCodec

func NewClientCodec[T any](us []Unmarshaller[base.Result]) (
	Codec[base.Cmd[T], base.Result], error)

NewClientCodec initializes a cmd-stream client codec.

It accepts a slice of Unmarshallers, where each Unmarshaller's DTM value must match its position in the slice. Example of a correct slice:

	[]Unmarshaller[base.Result]{
	  u0, // u0.DTM() = 0, index = 0
	  u1, // u1.DTM() = 1, index = 1
	  u2, // u2.DTM() = 2, index = 2
   ...
	}

Incorrect slices:

[]Unmarshaller{} // Empty slice.
[]Unmarshaller{  // Contains an item where DTM != index.
  u0, // u0.DTM() = 1, index = 0
}
[]Unmarshaller{  // Contains a nil item.
  nil,
}

Returns an error if the provided slice is invalid.

func NewServerCodec

func NewServerCodec[T any](us []Unmarshaller[base.Cmd[T]]) (
	Codec[base.Result, base.Cmd[T]], error)

NewServerCodec initializes a cmd-stream server codec.

It accepts a slice of Unmarshallers, where each Unmarshaller's DTM value must match its position in the slice. Example of a correct slice:

	[]Unmarshaller[base.Result]{
	  u0, // u0.DTM() = 0, index = 0
	  u1, // u1.DTM() = 1, index = 1
	  u2, // u2.DTM() = 2, index = 2
    ...
	}

Incorrect slices:

[]Unmarshaller{} // Empty slice.
[]Unmarshaller{  // Contains an item where DTM != index.
  u0, // u0.DTM() = 1, index = 0
}
[]Unmarshaller{  // Contains a nil item.
  nil,
}

Returns an error if the provided slice is invalid.

func (Codec[T, V]) Decode

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

func (Codec[T, V]) Encode

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

type Marshaller

type Marshaller interface {
	Marshal(w transport.Writer) error
}

Marshaller defines a Marshal method that should serialize DTM and data.

type ResultDTSAdapter

type ResultDTSAdapter[T base.Result] struct {
	// contains filtered or unexported fields
}

ResultDTSAdapter implements the Unmarshaller interface and serves as an adapter for Result DTS.

func NewResultDTSAdapter

func NewResultDTSAdapter[T base.Result](d dts.DTS[T]) ResultDTSAdapter[T]

NewResultDTSAdapter creates a new ResultDTSAdapter.

func (ResultDTSAdapter[T]) DTM

func (u ResultDTSAdapter[T]) DTM() com.DTM

func (ResultDTSAdapter[T]) Unmarshal

func (u ResultDTSAdapter[T]) Unmarshal(r muss.Reader) (v base.Result, n int,
	err error)

type Unmarshaller

type Unmarshaller[T any] interface {
	DTM() com.DTM
	Unmarshal(r muss.Reader) (t T, n int, err error)
}

Unmarshaller defines an Unmarshal method that should deserialize only data, without DTM.

Jump to

Keyboard shortcuts

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