marshalkit

package module
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2023 License: MIT Imports: 7 Imported by: 10

README

Dogma Marshaling Toolkit

Utilities for marshaling and unmarshaling Dogma messages and other types.

Documentation Latest Version Build Status Code Coverage

Documentation

Overview

Package marshalkit provides utilities for marshaling and unmarshaling Dogma messages and other types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustMarshalEnvelopeIdentity added in v0.5.0

func MustMarshalEnvelopeIdentity(id configkit.Identity) *envelopespec.Identity

MustMarshalEnvelopeIdentity marshals id to its protocol buffers representation, as used within envelopespec.Envelope.

func MustMarshalEnvelopeTime added in v0.5.0

func MustMarshalEnvelopeTime(t time.Time) string

MustMarshalEnvelopeTime marshals t to its RFC-3339 representation, as used within envelopespec.Envelope.

func MustMarshalMessageIntoEnvelope added in v0.5.0

func MustMarshalMessageIntoEnvelope(
	vm ValueMarshaler,
	m dogma.Message,
	env *envelopespec.Envelope,
)

MustMarshalMessageIntoEnvelope marshals a Dogma message into an envelopespec.Envelope.

func MustMarshalType

func MustMarshalType(ma TypeMarshaler, rt reflect.Type) string

MustMarshalType marshals rt to its portable representation. It panics if the type can not be marshaled.

func MustUnmarshal

func MustUnmarshal(ma ValueMarshaler, p Packet) any

MustUnmarshal produces a value from its binary representation. It panics if p can not be unmarshalled.

func MustUnmarshalType

func MustUnmarshalType(ma TypeMarshaler, n string) reflect.Type

MustUnmarshalType unmarshals a type from its portable representation. It panics if the type can not be unmarshaled.

func Recover

func Recover(err *error)

Recover recovers from a panic caused by one of the MustXXX() functions.

It is intended to be used in a defer statement. The error that caused the panic is assigned to *err.

func UnmarshalEnvelopeIdentity added in v0.5.0

func UnmarshalEnvelopeIdentity(id *envelopespec.Identity) (configkit.Identity, error)

UnmarshalEnvelopeIdentity unmarshals id from its protocol buffers representation, as used within envelopespec.Envelope.

func UnmarshalEnvelopeTime added in v0.5.0

func UnmarshalEnvelopeTime(t string) (time.Time, error)

UnmarshalEnvelopeTime unmarshals t from its RFC-3339 representation, as used within envelopespec.Envelope.

func UnmarshalMessageFromEnvelope added in v0.5.0

func UnmarshalMessageFromEnvelope(
	vm ValueMarshaler,
	env *envelopespec.Envelope,
) (dogma.Message, error)

UnmarshalMessageFromEnvelope unmarshals a Dogma message from an envelopespec.Envelope.

Types

type Marshaler

type Marshaler interface {
	TypeMarshaler
	ValueMarshaler
}

Marshaler is a marshaler that can marshal types and values.

type Packet

type Packet struct {
	// MediaType is a MIME media-type describing the content and encoding of the
	// binary data.
	MediaType string

	// Data is the marshaled binary data.
	Data []byte
}

Packet is a container of marshaled data and its related meta-data.

func MustMarshal

func MustMarshal(ma ValueMarshaler, v any) Packet

MustMarshal returns a binary representation of v. It panics if v can not be marshalled.

func NewPacket added in v0.2.1

func NewPacket(mt string, n string, data []byte) Packet

NewPacket returns a new packet.

mt is the MIME media-type describing the content and encoding of the binary data. n is the marshaled value's portable type name.

func (*Packet) ParseMediaType added in v0.2.1

func (p *Packet) ParseMediaType() (string, string, error)

ParseMediaType returns the media-type and the portable type name encoded in the packet's MIME media-type.

type PanicSentinel

type PanicSentinel struct {
	// Cause is the error that caused the panic.
	Cause error
}

PanicSentinel is a wrapper value used to identify panic's that are caused by one of the MustXXX() functions.

func (PanicSentinel) String added in v0.5.0

func (s PanicSentinel) String() string

type TypeMarshaler

type TypeMarshaler interface {
	// MarshalType marshals a type to its portable representation.
	MarshalType(rt reflect.Type) (string, error)

	// UnmarshalType unmarshals a type from its portable representation.
	UnmarshalType(n string) (reflect.Type, error)
}

A TypeMarshaler marshals and unmarshals Go types to and from "portable" string representations.

type ValueMarshaler added in v0.2.0

type ValueMarshaler interface {
	// Marshal returns a binary representation of v.
	Marshal(v any) (Packet, error)

	// MarshalAs returns a binary representation of v encoded using a format
	// associated with one of the supplied media-types.
	//
	// mediaTypes is a list of acceptible media-types, in order of preference.
	// If none of the media-types are supported, ok is false.
	MarshalAs(v any, mediaTypes []string) (p Packet, ok bool, err error)

	// Unmarshal produces a value from its binary representation.
	Unmarshal(p Packet) (any, error)

	// MediaTypesFor returns the media-types that the marshaler can use to
	// represent the given type, in order of preference.
	//
	// It returns an empty slice if the type is not supported.
	MediaTypesFor(reflect.Type) []string
}

A ValueMarshaler marshals and unmarshals arbitrary Go values.

Directories

Path Synopsis
cbor
Package cbor contains a codec implementation that marshals in CBOR format.
Package cbor contains a codec implementation that marshals in CBOR format.
json
Package json contains a codec that uses Go's standard JSON marshaling.
Package json contains a codec that uses Go's standard JSON marshaling.
protobuf
Package protobuf contains codec implemenentations for protocol buffers types.
Package protobuf contains codec implemenentations for protocol buffers types.
Package fixtures contains test fixtures for the types from the github.com/dogmatiq/dogma/fixtures package.
Package fixtures contains test fixtures for the types from the github.com/dogmatiq/dogma/fixtures package.
internal

Jump to

Keyboard shortcuts

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