container

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: Apache-2.0 Imports: 4 Imported by: 3

Documentation

Overview

Package container contains generic containers.

Package container provides generic map types.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrExists is returned when the given element exists in the container.
	ErrExists = errors.New("element exists already")
	// ErrUnknown is returned when the given name doesn't exist in the container.
	ErrUnknown = errors.New("unknown element given")
)

Functions

This section is empty.

Types

type List

type List[T fmt.Stringer] struct {
	// contains filtered or unexported fields
}

List is a list container of T.

func NewList

func NewList[T fmt.Stringer]() *List[T]

NewList creates a new container that holds element's of type T.

func (*List[T]) Add

func (c *List[T]) Add(element T) error

Add adds a new element to the container.

func (*List[T]) Clear

func (c *List[T]) Clear()

Clear clears the internal list.

func (*List[T]) List

func (c *List[T]) List() []T

List returns the internal list.

type Map

type Map[T any] struct {
	// contains filtered or unexported fields
}

Map is a map container for function factories.

func NewMap

func NewMap[T any]() *Map[T]

NewMap creates a new map of any type. Not concurency safe.

func (*Map[T]) Add

func (c *Map[T]) Add(name string, element T) error

Add adds a new factory function to this container. It returns ErrExists if the plugin already exists.

func (*Map[T]) All

func (c *Map[T]) All() map[string]T

All returns the internal map.

func (*Map[T]) Get

func (c *Map[T]) Get(name string) (T, error)

Get returns a single item by its name.

func (*Map[T]) Set

func (c *Map[T]) Set(name string, element T)

Set will set a value regardless of whether it already exists in the map.

type Plugins

type Plugins[T any] struct {
	Map[T]
}

Plugins is an alias around the Map type, for nicer plugin method names.

func NewPlugins

func NewPlugins[T any]() *Plugins[T]

NewPlugins creates a new plugins container of any type. Not concurency safe.

func (*Plugins[T]) Deregister

func (p *Plugins[T]) Deregister(name string)

Deregister a plugin.

func (*Plugins[T]) Register

func (p *Plugins[T]) Register(name string, element T)

Register a plugin.

type SafeMap

type SafeMap[T any] struct {
	Map[T]
	// contains filtered or unexported fields
}

SafeMap is a concurrently safe generic map.

func NewSafeMap

func NewSafeMap[T any]() *SafeMap[T]

NewSafeMap creates a new concurrently map of any types.

func (*SafeMap[T]) Add

func (c *SafeMap[T]) Add(name string, element T) error

Add adds a new factory function to this container. It returns ErrExists if the plugin already exists.

func (*SafeMap[T]) All

func (c *SafeMap[T]) All() map[string]T

All returns the internal map.

func (*SafeMap[T]) Get

func (c *SafeMap[T]) Get(name string) (T, error)

Get returns a single item by its name.

func (*SafeMap[T]) Set

func (c *SafeMap[T]) Set(name string, element T)

Set will either insert into or update the map, without returning an error if an item already exists.

type Sorted

type Sorted[T SortedElement] struct {
	// contains filtered or unexported fields
}

Sorted is a sorted container.

func NewSorted

func NewSorted[T SortedElement]() *Sorted[T]

NewSorted creates a new container that holds elements of type T sorted. Adds are costly as it sorts on each add, calls to Sorted() are free.

func (*Sorted[T]) Add

func (c *Sorted[T]) Add(element T) error

Add adds a new factory function to this container. It returns ErrExists if the plugin already exists.

func (*Sorted[T]) Sorted

func (c *Sorted[T]) Sorted() []T

Sorted returns the internal list.

type SortedElement

type SortedElement interface {
	fmt.Stringer

	Priority() int
}

SortedElement needs to be implemented by every interface.

Jump to

Keyboard shortcuts

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