spec

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: Apache-2.0 Imports: 5 Imported by: 8

Documentation

Overview

Package spec provides spec-version metadata.

For use by code that maps events using (prefixed) attribute name strings. Supports handling multiple spec versions uniformly.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute interface {
	Kind() Kind
	// Name of the attribute with respect to the current spec Version()
	Name() string
	// Version of the spec that this attribute belongs to
	Version() Version
	// Get the value of this attribute from an event context
	Get(ce.EventContextReader) interface{}
	// Set the value of this attribute on an event context
	Set(ce.EventContextWriter, interface{}) error
}

Attribute is a named attribute accessor. The attribute name is specific to a Version.

type Kind

type Kind uint8

Kind is a version-independent identifier for a CloudEvent context attribute.

const (
	// Required cloudevents attributes
	ID Kind = iota
	Source
	SpecVersion
	Type
	// Optional cloudevents attributes
	DataContentType
	DataSchema
	Subject
	Time
)

func (Kind) IsRequired

func (k Kind) IsRequired() bool

IsRequired returns true for attributes defined as "required" by the CE spec.

func (Kind) String

func (k Kind) String() string

String is a human-readable string, for a valid attribute name use Attribute.Name

type Version

type Version interface {
	// String name of the version, e.g. "1.0"
	String() string
	// Prefix for attribute names.
	Prefix() string
	// Attribute looks up a prefixed attribute name (case insensitive).
	// Returns nil if not found.
	Attribute(name string) Attribute
	// Attributes returns all the context attributes for this version.
	Attributes() []Attribute
	// NewContext returns a new context for this version.
	NewContext() ce.EventContext
	// Convert translates a context to this version.
	Convert(ce.EventContextConverter) ce.EventContext
	// SetAttribute sets named attribute to value.
	//
	// Name is case insensitive.
	// Does nothing if name does not start with prefix.
	SetAttribute(context ce.EventContextWriter, name string, value interface{}) error
	// Attribute looks up the attribute from kind.
	// Returns nil if not found.
	AttributeFromKind(kind Kind) Attribute
}

Version provides meta-data for a single spec-version.

var (
	V01 Version
	V02 Version
	V03 Version
	V1  Version
)

Built-in un-prefixed versions.

type Versions

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

Versions contains all known versions with the same attribute prefix.

func New

func New() *Versions

New returns a set of versions

func WithPrefix

func WithPrefix(prefix string) *Versions

WithPrefix returns a set of versions with prefix added to all attribute names.

func (*Versions) FindVersion added in v0.10.1

func (vs *Versions) FindVersion(getAttr func(string) string) (Version, error)

FindVersion calls getAttr with known (prefixed) spec-version attribute names till it finds a valid version.

func (*Versions) Latest

func (vs *Versions) Latest() Version

Latest returns the latest Version

func (*Versions) Prefix

func (vs *Versions) Prefix() string

Prefix is the lowercase attribute name prefix.

func (*Versions) SpecVersionNames

func (vs *Versions) SpecVersionNames() []string

SpecVersionNames returns distinct names of the specversion attribute used in all versions, newest first. Names are prefixed.

func (*Versions) Version

func (vs *Versions) Version(name string) (Version, error)

Version returns the named version.

func (*Versions) Versions

func (vs *Versions) Versions() []Version

Versions returns the list of all known versions, most recent first.

Jump to

Keyboard shortcuts

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