containers

package
v0.0.0-...-16b3867 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepClone

func DeepClone[T any](m T) T

func KeepKeys

func KeepKeys[K comparable, V any](m map[K]V, keys ...K) map[K]V

KeepKeys remove all the keys that are NOT in keys from map m

func RemoveKeys

func RemoveKeys[K comparable, V any](m map[K]V, keys ...K) map[K]V

RemoveKeys removes all the keys from map m

func Seq2

func Seq2[K comparable, V any](m map[K]V) iter.Seq2[K, V]

func WalkMap

func WalkMap(m map[string]interface{}, prefix string, f func(k string, v interface{}))

WalkMap recursively walks through a map[string]interface{}, applying a function to each key-value pair.

It handles nested maps by recursively calling itself.

Parameters:

Types

type ImmutableMap

type ImmutableMap[K comparable, V any] interface {
	Get(key K) (V, bool)
	Keys() iter.Seq[K]
	Values() iter.Seq[V]
	IterSeq2() iter.Seq2[K, V]
}

func NewImmutableMap

func NewImmutableMap[K comparable, V any](m map[K]V) ImmutableMap[K, V]

type Keyer

type Keyer[T any] func(t *T) string

type Result

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

Result represents a result of an action having one of the following output: success or failure. An instance of Result is an instance of either Ok or Err. It could be compared to `Either[error, T]`.

func Do

func Do[T any](fn func() T) (result Result[T])

Do executes a function within a monadic context, capturing any errors that occur. If the function executes successfully, its result is wrapped in a successful Result. If the function panics (indicating a failure), the panic is caught and converted into an error Result.

func Err

func Err[T any](err error) Result[T]

Err builds a Result when value is invalid. Play: https://go.dev/play/p/PDwADdzNoyZ

func Errf

func Errf[T any](format string, a ...any) Result[T]

Errf builds a Result when value is invalid. Errf formats according to a format specifier and returns the error as a value that satisfies Result[T]. Play: https://go.dev/play/p/N43w92SM-Bs

func Ok

func Ok[T any](value T) Result[T]

Ok builds a Result when value is valid. Play: https://go.dev/play/p/PDwADdzNoyZ

func Try

func Try[T any](f func() (T, error)) Result[T]

Try returns either a Ok or Err object. Play: https://go.dev/play/p/ilOlQx-Mx42

func TupleToResult

func TupleToResult[T any](value T, err error) Result[T]

TupleToResult convert a pair of T and error into a Result. Play: https://go.dev/play/p/KWjfqQDHQwa

func (Result[T]) Error

func (r Result[T]) Error() error

Error returns error when value is invalid or nil. Play: https://go.dev/play/p/CSkHGTyiXJ5

func (Result[T]) FlatMap

func (r Result[T]) FlatMap(mapper func(value T) Result[T]) Result[T]

FlatMap executes the mapper function if Result is valid. It returns a new Result. Play: https://go.dev/play/p/Ud5QjZOqg-7

func (Result[T]) ForEach

func (r Result[T]) ForEach(mapper func(value T))

ForEach executes the given side-effecting function if Result is valid.

func (Result[T]) Get

func (r Result[T]) Get() (T, error)

Get returns value and error. Play: https://go.dev/play/p/8KyX3z6TuNo

func (Result[T]) IsError

func (r Result[T]) IsError() bool

IsError returns true when value is invalid. Play: https://go.dev/play/p/xkV9d464scV

func (Result[T]) IsOk

func (r Result[T]) IsOk() bool

IsOk returns true when value is valid. Play: https://go.dev/play/p/sfNvBQyZfgU

func (Result[T]) Map

func (r Result[T]) Map(mapper func(value T) (T, error)) Result[T]

Map executes the mapper function if Result is valid. It returns a new Result. Play: https://go.dev/play/p/-ndpN_b_OSc

func (Result[T]) MapErr

func (r Result[T]) MapErr(mapper func(error) (T, error)) Result[T]

MapErr executes the mapper function if Result is invalid. It returns a new Result. Play: https://go.dev/play/p/WraZixg9GGf

func (Result[T]) MarshalJSON

func (o Result[T]) MarshalJSON() ([]byte, error)

MarshalJSON encodes Result into json, following the JSON-RPC specification for results, with one exception: when the result is an error, the "code" field is not included. Reference: https://www.jsonrpc.org/specification

func (Result[T]) Match

func (r Result[T]) Match(onSuccess func(value T) (T, error), onError func(err error) (T, error)) Result[T]

Match executes the first function if Result is valid and second function if invalid. It returns a new Result. Play: https://go.dev/play/p/-_eFaLJ31co

func (Result[T]) MustGet

func (r Result[T]) MustGet() T

MustGet returns value when Result is valid or panics. Play: https://go.dev/play/p/8LSlndHoTAE

func (Result[T]) OrElse

func (r Result[T]) OrElse(fallback T) T

OrElse returns value when Result is valid or default value. Play: https://go.dev/play/p/MN_ULx0soi6

func (Result[T]) OrEmpty

func (r Result[T]) OrEmpty() T

OrEmpty returns value when Result is valid or empty value. Play: https://go.dev/play/p/rdKtBmOcMLh

func (*Result[T]) UnmarshalJSON

func (o *Result[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes json into Result. If "error" is set, the result is an Err containing the error message as a generic error object. Otherwise, the result is an Ok containing the result. If the JSON object contains netiher an error nor a result, the result is an Ok containing an empty value. If the JSON object contains both an error and a result, the result is an Err. Finally, if the JSON object contains an error but is not structured correctly (no message field), the unmarshaling fails.

type Store

type Store[K string, V any] interface {
	All() (iter.Seq2[K, *V], error)
	Load(id string) (*V, error)
	Store(v *V) (*V, error)
	Remove(id string) error
}

type SyncSet

type SyncSet[T comparable] interface {
	// Set adds a value to the set if it doesn't already exist.
	// Returns true if the value was added, false if it was already present.
	Set(v T) bool

	// All returns an iterator over all values in the set in no particular order.
	All() iter.Seq[T]
}

SyncSet represents a thread-safe set data structure that can store unique comparable values.

func NewSyncSet

func NewSyncSet[T comparable]() SyncSet[T]

NewSyncSet creates a new thread-safe set implementation. The set can store any comparable type T and prevents duplicate entries. It is safe for concurrent access by multiple goroutines.

Jump to

Keyboard shortcuts

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