vmux

package
v0.0.0-...-067284b Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2023 License: Apache-2.0 Imports: 8 Imported by: 16

Documentation

Overview

Package vmux provides utilities that make it easy to implement "version multiplexing" with your Thema lineages.

Version multiplexing is taking an input of []byte expected to represent an object schematized by some Thema lineage, and automatically decoding, validating, and translating it to a single schema version in that lineage. The effect of this is a Go program that "accepts all, sees one" - all historical schema versions are accepted, but the program can be written as though only a single version exists.

The generic utilities in this package reduce version muxing to a single function call. Still, they are pure convenience: this package relies solely on the public interface of github.com/grafana/thema, and can be reimplemented with different tradeoffs elsewhere if needed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteMux

type ByteMux func(b []byte) ([]byte, thema.TranslationLacunas, error)

ByteMux is a version multiplexer that maps a []byte containing data at any schematized version to a []byte containing data at a particular schematized version.

func NewByteMux

func NewByteMux(sch thema.Schema, codec Codec) ByteMux

NewByteMux creates a ByteMux func from the provided thema.Schema.

When the returned mux func is called, it will:

The returned error may be from any of the above steps.

type Codec

type Codec interface {
	Decoder
	Encoder
}

A Codec can decode a []byte in a particular format (e.g. JSON, YAML) into CUE, and decode from a thema.Instance back into a []byte.

It is customary, but not necessary, that a Codec's input and output formats are the same.

func NewJSONCodec

func NewJSONCodec(path string) Codec

NewJSONCodec creates a Codec that decodes from and encodes to a JSON []byte.

The provided path is used as the CUE source path for each []byte input passed through the decoder. These paths do not affect behavior, but show up in error output (e.g. validation).

func NewYAMLCodec

func NewYAMLCodec(path string) Codec

NewYAMLCodec creates a Codec that decodes from and encodes to a YAML []byte.

The provided path is used as the CUE source path for each []byte input passed through the decoder. These paths do not affect behavior, but show up in error output (e.g. validation).

type Decoder

type Decoder interface {
	Decode(ctx *cue.Context, b []byte) (cue.Value, error)
}

A Decoder can decode a []byte in a particular format (e.g. JSON, YAML) into a cue.Value, readying it for a call to thema.Schema.Validate.

type Encoder

type Encoder interface {
	Encode(cue.Value) ([]byte, error)
}

An Encoder can encode a thema.Instance to a []byte in a particular format (e.g. JSON, YAML).

type TypedMux

type TypedMux[T thema.Assignee] func(b []byte) (*thema.TypedInstance[T], thema.TranslationLacunas, error)

TypedMux is a version multiplexer that maps a []byte containing data at any schematized version to a thema.TypedInstance at a particular schematized version.

func NewTypedMux

func NewTypedMux[T thema.Assignee](sch thema.TypedSchema[T], dec Decoder) TypedMux[T]

NewTypedMux creates a TypedMux func from the provided thema.TypedSchema.

When the returned mux func is called, it will:

The returned error may be from any of the above steps.

type UntypedMux

type UntypedMux func(b []byte) (*thema.Instance, thema.TranslationLacunas, error)

UntypedMux is a version multiplexer that maps a []byte containing data at any schematized version to a thema.Instance at a particular schematized version.

func NewUntypedMux

func NewUntypedMux(sch thema.Schema, dec Decoder) UntypedMux

NewUntypedMux creates an UntypedMux from the provided thema.Schema.

When the returned mux func is called, it will:

The returned error may be from any of the above steps.

type ValueMux

type ValueMux[T thema.Assignee] func(b []byte) (T, thema.TranslationLacunas, error)

ValueMux is a version multiplexer that maps a []byte containing data at any schematized version to a Go var of a type that a particular schematized version is thema.AssignableTo.

func NewValueMux

func NewValueMux[T thema.Assignee](sch thema.TypedSchema[T], dec Decoder) ValueMux[T]

NewValueMux creates a ValueMux func from the provided thema.TypedSchema.

When the returned mux func is called, it will:

The returned error may be from any of the above steps.

Jump to

Keyboard shortcuts

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