mapper

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 6 Imported by: 8 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DoesNotExistError = errors.New("does not exist")
)

Functions

This section is empty.

Types

type Mapper

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

Mapper provides a bidirectional mapping between string keys and typed values. It is particularly useful for enums, configuration keys, and any scenario where you need to convert between string representations and concrete types.

The type requires T to implement fmt.Stringer, and all keys are stored in lowercase to ensure case-insensitive lookups.

Basic usage:

mapper := NewMapper(StatusPending, StatusActive, StatusClosed)
status, err := mapper.Get("active")      // returns StatusActive
exists := mapper.HasByKey("pending")     // true
exists := mapper.HasByValue(StatusClosed) // true

The zero value is not ready for use and must be created via NewMapper. All methods are safe for concurrent read-only access, but concurrent writes (Add) require external synchronization.

func NewMapper

func NewMapper[T fmt.Stringer](values ...T) *Mapper[T]

func (*Mapper[T]) Add

func (mapper *Mapper[T]) Add(values ...T)

func (*Mapper[T]) All

func (mapper *Mapper[T]) All() iter.Seq2[string, T]

func (*Mapper[T]) Get

func (mapper *Mapper[T]) Get(key string) (T, error)

func (*Mapper[T]) HasByKey

func (mapper *Mapper[T]) HasByKey(key string) bool

func (*Mapper[T]) HasByValue

func (mapper *Mapper[T]) HasByValue(value T) bool

func (*Mapper[T]) Keys

func (mapper *Mapper[T]) Keys() []string

func (*Mapper[T]) RemoveByKey

func (mapper *Mapper[T]) RemoveByKey(key string)

func (*Mapper[T]) RemoveByValue

func (mapper *Mapper[T]) RemoveByValue(value T)

Jump to

Keyboard shortcuts

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