ezmap

package
v2.11.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetTyped

func GetTyped[T any](m map[string]any, key string) T

GetTyped returns the value associated with key as type T, if exists.

func GetTypedOr added in v2.8.5

func GetTypedOr[T any](m map[string]any, key string, defaultVal T) T

GetTypedOr returns typed value for the given key if it exists in the map, else it returns the default value.

Types

type Map

type Map map[string]any

Map is a map of string key and any value. It provides many useful methods to work with map[string]any.

func NewMap

func NewMap() Map

NewMap returns a new initialized Map.

func (Map) Get

func (p Map) Get(key string) (value any, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exist it returns (nil, false)

func (Map) GetBool

func (p Map) GetBool(key string) bool

GetBool returns the value associated with the key as a boolean value.

func (Map) GetBytes

func (p Map) GetBytes(key string) []byte

GetBytes returns the value associated with the key as bytes.

func (Map) GetDuration

func (p Map) GetDuration(key string) time.Duration

GetDuration returns the value associated with the key as a duration.

func (Map) GetFloat

func (p Map) GetFloat(key string) float64

GetFloat returns the value associated with the key as a float64.

func (Map) GetInt

func (p Map) GetInt(key string) int64

GetInt returns the value associated with the key as an int64.

func (Map) GetInt32s

func (p Map) GetInt32s(key string) []int32

GetInt32s returns the value associated with the key as a slice of int32.

func (Map) GetInt64s

func (p Map) GetInt64s(key string) []int64

GetInt64s returns the value associated with the key as a slice of int64.

func (Map) GetMap

func (p Map) GetMap(key string) Map

GetMap returns the value associated with the key as a Map (map[string]any).

func (Map) GetOr added in v2.8.5

func (p Map) GetOr(key string, defaultVal any) (value any)

GetOr returns the value for the given key if it exists in the map, else it returns the default value.

func (Map) GetSlice

func (p Map) GetSlice(key string) any

GetSlice returns the value associated with the key as a slice. It returns nil if key does not present in Map or the value's type is not a slice.

func (Map) GetString

func (p Map) GetString(key string) string

GetString returns the value associated with the key as a string.

func (Map) GetStringMap

func (p Map) GetStringMap(key string) map[string]string

GetStringMap returns the value associated with the key as a map of (map[string]string).

func (Map) GetStrings

func (p Map) GetStrings(key string) []string

GetStrings returns the value associated with the key as a slice of strings.

func (Map) GetTime

func (p Map) GetTime(key string) time.Time

GetTime returns the value associated with the key as time.

func (Map) Iterate

func (p Map) Iterate(fn func(k string, v any) int)

Iterate iterates the map in unspecified order, the given function fn will be called for each key value pair. The iteration can be aborted by returning a non-zero value from fn.

func (Map) MarshalJSON

func (p Map) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Map) MarshalJSONPretty

func (p Map) MarshalJSONPretty() ([]byte, error)

MarshalJSONPretty returns its marshaled data as `[]byte` with indentation using two spaces.

func (Map) MarshalYAML added in v2.8.4

func (p Map) MarshalYAML() (any, error)

MarshalYAML implements the yaml.Marshaler interface.

func (Map) MustGet

func (p Map) MustGet(key string) any

MustGet returns the value for the given key if it exists, otherwise it panics.

func (*Map) Set

func (p *Map) Set(key string, value any)

Set is used to store a new key/value pair exclusively in the map. It also lazily initializes the map if it was not used previously.

func (*Map) UnmarshalJSON

func (p *Map) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Map) UnmarshalYAML added in v2.8.4

func (p *Map) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML implements the yaml.Unmarshaler interface.

type SafeMap

type SafeMap struct {
	sync.RWMutex
	Map
}

SafeMap wraps a Map with a RWMutex to provide concurrent safety.

Note that user must explicitly acquire and release the lock to be concurrent safe (i.e. to avoid data race).

func NewSafeMap

func NewSafeMap() *SafeMap

NewSafeMap returns a new initialized SafeMap.

Jump to

Keyboard shortcuts

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