maps

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: MIT Imports: 14 Imported by: 3

README

go-maps

Golang map extensions

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNilMap      = errors.New("map is nil")
	ErrReadOnlyMap = errors.New("map is readonly")
)

Functions

func AnyElem

func AnyElem[K comparable, V any, M ~map[K]V](m M) (K, V)

AnyElem returns first key and value found in map. If map is empty it returns default values. Multiple calls can return same or different values.

func Equals

func Equals[K, V comparable, M ~map[K]V](a, b M) (equal bool)

Types

type Bucket

type Bucket[V any] struct {
	// contains filtered or unexported fields
}

func NewBucket

func NewBucket[V any](m Maper[string, V], pfx string) *Bucket[V]

func (Bucket[V]) Bucket

func (b Bucket[V]) Bucket(pfx string) *Bucket[V]

func (Bucket[V]) Delete

func (b Bucket[V]) Delete(k string)

func (Bucket[V]) Exists

func (b Bucket[V]) Exists(k string) bool

func (Bucket[V]) ForEach

func (b Bucket[V]) ForEach(fn func(k string, v V) error) error

func (Bucket[V]) Get

func (b Bucket[V]) Get(k string) V

func (Bucket[V]) GetFull

func (b Bucket[V]) GetFull(k string) (V, bool)

func (Bucket[V]) Iter

func (b Bucket[V]) Iter() types.Iterator[string, V]

func (Bucket[V]) Keys

func (b Bucket[V]) Keys() (keys []string)

func (Bucket[V]) Len

func (b Bucket[V]) Len() int

func (Bucket[V]) Set

func (b Bucket[V]) Set(k string, v V)

func (Bucket[V]) Values

func (b Bucket[V]) Values() (values []V)

func (Bucket[V]) Watch

func (b Bucket[V]) Watch(ctx context.Context) types.Watcher[string, V]

type EventfulMap

type EventfulMap[K comparable, V any] struct {
	Maper[K, V]
	*channel.Hub[types.WatchMsg[K, V]]
}

func NewEventful

func NewEventful[K comparable, V any](m Maper[K, V], ctx context.Context, buf int) *EventfulMap[K, V]

func (*EventfulMap[K, V]) Delete

func (m *EventfulMap[K, V]) Delete(k K)

func (*EventfulMap[K, V]) Set

func (m *EventfulMap[K, V]) Set(k K, v V)

func (*EventfulMap[K, V]) UnmarshalCBOR

func (m *EventfulMap[K, V]) UnmarshalCBOR(data []byte) error

func (*EventfulMap[K, V]) UnmarshalJSON

func (m *EventfulMap[K, V]) UnmarshalJSON(data []byte) error

type EventfulMaper

type EventfulMaper[K comparable, V any] interface {
	Maper[K, V]

	Register(context.Context) channel.Client[types.WatchMsg[K, V]]
}

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](data map[K]V) *Map[K, V]

func (*Map[K, V]) Commit

func (m *Map[K, V]) Commit(fn func(data map[K]V))

run function with direct access to Map

func (*Map[K, V]) Copy

func (m *Map[K, V]) Copy() Maper[K, V]

return Map copy

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(k K)

delete key from Map

func (*Map[K, V]) Eventful

func (m *Map[K, V]) Eventful(ctx context.Context, buf int) *EventfulMap[K, V]

func (*Map[K, V]) Exists

func (m *Map[K, V]) Exists(k K) bool

return key existence

func (*Map[K, V]) ForEach

func (m *Map[K, V]) ForEach(fn func(k K, v V) error) error

range over Map

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(k K) V

return value for key

func (*Map[K, V]) GetFull

func (m *Map[K, V]) GetFull(k K) (obj V, exists bool)

return value and existence of key

func (*Map[K, V]) Iter

func (m *Map[K, V]) Iter() types.Iterator[K, V]

return iterator for safe iterating over Map

func (*Map[K, V]) Keys

func (m *Map[K, V]) Keys() (keys []K)

return all Map keys

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

return Map length

func (*Map[K, V]) MarshalCBOR

func (m *Map[K, V]) MarshalCBOR() ([]byte, error)

func (*Map[K, V]) MarshalJSON

func (m *Map[K, V]) MarshalJSON() ([]byte, error)

func (*Map[K, V]) Raw

func (m *Map[K, V]) Raw() map[K]V

returns original map. Use copy before calling this method.

func (*Map[K, V]) ReadOnly

func (m *Map[K, V]) ReadOnly() *ReadOnlyMap[K, V]

return ReadOnly Map

func (*Map[K, V]) Safe

func (m *Map[K, V]) Safe() *SafeMap[K, V]

return Safe Map

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(k K, v V)

set value for key

func (*Map[K, V]) String

func (m *Map[K, V]) String() string

func (*Map[K, V]) UnmarshalCBOR

func (m *Map[K, V]) UnmarshalCBOR(data []byte) error

func (*Map[K, V]) UnmarshalJSON

func (m *Map[K, V]) UnmarshalJSON(data []byte) error

func (*Map[K, V]) Values

func (m *Map[K, V]) Values() (values []V)

return all Map values

type MapConverter

type MapConverter[K comparable, V any] interface {
	// Eventful map forces safe
	Eventful(ctx context.Context, buf int) *EventfulMap[K, V]

	// return SafeMap
	Safe() *SafeMap[K, V]

	// return ReadOnly Map
	ReadOnly() *ReadOnlyMap[K, V]
}

All maps must implement this interface themselves.

type Maper

type Maper[K comparable, V any] interface {
	MapConverter[K, V]

	// return key existence
	Exists(k K) bool

	// return value for key
	Get(k K) V

	// return value and existence of key
	GetFull(k K) (obj V, exists bool)

	// set value for key
	Set(k K, v V)

	// delete key from Map
	Delete(k K)

	// run function with direct access to Map
	Commit(fn func(data map[K]V))

	// return iterator for safe iterating over Map
	Iter() types.Iterator[K, V]

	// range over Map
	ForEach(fn func(k K, v V) error) error

	// return all Map keys
	Keys() (keys []K)

	// return all Map values
	Values() (values []V)

	// return Map length
	Len() int

	// return underlying Map copy (writable, non-safe or eventful)
	Copy() Maper[K, V]

	// returns original map. Use copy before calling this method.
	Raw() map[K]V

	MarshalJSON() ([]byte, error)
	UnmarshalJSON(data []byte) error

	MarshalCBOR() ([]byte, error)
	UnmarshalCBOR(data []byte) error

	String() string
}

type OrderedMap

type OrderedMap[K constraints.Ordered, V any] struct {
	Map[K, V]
	// contains filtered or unexported fields
}

func NewOrdered

func NewOrdered[K constraints.Ordered, V any](data map[K]V, sorter SortFunction[K, V]) *OrderedMap[K, V]

func NewOrderedByValue

func NewOrderedByValue[K constraints.Ordered, V OrderedValue](data map[K]V, sorter SortFunction[K, V]) *OrderedMap[K, V]

func NewOrderedFromMap

func NewOrderedFromMap[K constraints.Ordered, V any](m *Map[K, V], sorter SortFunction[K, V]) *OrderedMap[K, V]

func (*OrderedMap[K, V]) Commit

func (m *OrderedMap[K, V]) Commit(fn func(data map[K]V))

run function with direct access to Map

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(k K)

delete key from Map

func (*OrderedMap[K, V]) Eventful

func (m *OrderedMap[K, V]) Eventful(ctx context.Context, buf int) *EventfulMap[K, V]

func (*OrderedMap[K, V]) ForEach

func (m *OrderedMap[K, V]) ForEach(fn func(k K, v V) error) error

range over Map

func (*OrderedMap[K, V]) Iter

func (m *OrderedMap[K, V]) Iter() types.Iterator[K, V]

return iterator for safe iterating over Map

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() (keys []K)

return all Map keys

func (*OrderedMap[K, V]) ReadOnly

func (m *OrderedMap[K, V]) ReadOnly() *ReadOnlyMap[K, V]

func (*OrderedMap[K, V]) Safe

func (m *OrderedMap[K, V]) Safe() *SafeMap[K, V]

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(k K, v V)

set value for key

func (*OrderedMap[K, V]) UnmarshalCBOR

func (m *OrderedMap[K, V]) UnmarshalCBOR(data []byte) error

func (*OrderedMap[K, V]) UnmarshalJSON

func (m *OrderedMap[K, V]) UnmarshalJSON(data []byte) error

func (*OrderedMap[K, V]) Values

func (m *OrderedMap[K, V]) Values() (values []V)

return all Map values

type OrderedValue

type OrderedValue interface {
	Less(x any) bool
}

type ReadOnlyMap

type ReadOnlyMap[K comparable, V any] struct {
	Maper[K, V]
}

func NewReadOnlyMap

func NewReadOnlyMap[K comparable, V any](m Maper[K, V]) *ReadOnlyMap[K, V]

func (ReadOnlyMap[K, V]) Commit

func (ReadOnlyMap[K, V]) Commit(fn func(data map[K]V))

func (ReadOnlyMap[K, V]) Delete

func (ReadOnlyMap[K, V]) Delete(k K)

func (ReadOnlyMap[K, V]) Set

func (ReadOnlyMap[K, V]) Set(k K, v V)

func (ReadOnlyMap[K, V]) UnmarshalCBOR

func (ReadOnlyMap[K, V]) UnmarshalCBOR(data []byte) error

func (ReadOnlyMap[K, V]) UnmarshalJSON

func (ReadOnlyMap[K, V]) UnmarshalJSON(data []byte) error

func (ReadOnlyMap[K, V]) UnmarshalText

func (ReadOnlyMap[K, V]) UnmarshalText(data []byte) error

type SafeMap

type SafeMap[K comparable, V any] struct {
	Maper[K, V]
	// contains filtered or unexported fields
}

func NewSafe

func NewSafe[K comparable, V any](m Maper[K, V]) *SafeMap[K, V]

func (*SafeMap[K, V]) Commit

func (m *SafeMap[K, V]) Commit(fn func(data map[K]V))

run function with direct access to Map

func (*SafeMap[K, V]) Copy

func (m *SafeMap[K, V]) Copy() Maper[K, V]

func (*SafeMap[K, V]) Delete

func (m *SafeMap[K, V]) Delete(k K)

delete key from Map

func (*SafeMap[K, V]) Eventful

func (m *SafeMap[K, V]) Eventful(ctx context.Context, buf int) *EventfulMap[K, V]

func (*SafeMap[K, V]) Exists

func (m *SafeMap[K, V]) Exists(k K) bool

return key existence

func (*SafeMap[K, V]) ForEach

func (m *SafeMap[K, V]) ForEach(fn func(k K, v V) error) error

range over Map

func (*SafeMap[K, V]) Get

func (m *SafeMap[K, V]) Get(k K) V

return value for key

func (*SafeMap[K, V]) GetFull

func (m *SafeMap[K, V]) GetFull(k K) (obj V, exists bool)

return value and existence of key

func (*SafeMap[K, V]) Iter

func (m *SafeMap[K, V]) Iter() types.Iterator[K, V]

return iterator for safe iterating over Map

func (*SafeMap[K, V]) Keys

func (m *SafeMap[K, V]) Keys() (keys []K)

return all Map keys

func (*SafeMap[K, V]) Len

func (m *SafeMap[K, V]) Len() int

return Map length

func (*SafeMap[K, V]) MarshalCBOR

func (m *SafeMap[K, V]) MarshalCBOR() ([]byte, error)

func (*SafeMap[K, V]) MarshalJSON

func (m *SafeMap[K, V]) MarshalJSON() ([]byte, error)

func (*SafeMap[K, V]) ReadOnly

func (m *SafeMap[K, V]) ReadOnly() *ReadOnlyMap[K, V]

return ReadOnly Map

func (*SafeMap[K, V]) Safe

func (m *SafeMap[K, V]) Safe() *SafeMap[K, V]

return Safe Map

func (*SafeMap[K, V]) Set

func (m *SafeMap[K, V]) Set(k K, v V)

set value for key

func (*SafeMap[K, V]) String

func (m *SafeMap[K, V]) String() string

func (*SafeMap[K, V]) UnmarshalCBOR

func (m *SafeMap[K, V]) UnmarshalCBOR(data []byte) error

func (*SafeMap[K, V]) UnmarshalJSON

func (m *SafeMap[K, V]) UnmarshalJSON(data []byte) error

func (*SafeMap[K, V]) Values

func (m *SafeMap[K, V]) Values() (values []V)

return all Map values

type SortFunction

type SortFunction[K comparable, V any] func(data map[K]V) []types.Item[K, V]

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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