maps

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2025 License: MIT Imports: 13 Imported by: 2

Documentation

Index

Examples

Constants

View Source
const (
	BitPartitionSize = 6
	MaskLevel0       = 0b11111100 << 56
)

Variables

View Source
var (
	ErrDeleteNotImplemented = fmt.Errorf("delete is not implemented")
	ErrKeyNotFound          = fmt.Errorf("key not found")
	ErrWrongType            = fmt.Errorf("wrong type")
)
View Source
var DefaultMapOptions = MapOptions{
	// contains filtered or unexported fields
}

Functions

func EqualsAny

func EqualsAny(a any, b any) bool

func EqualsAnyList

func EqualsAnyList(a []any, b []any) bool

func EqualsStringList

func EqualsStringList(a []string, b []string) bool

func Get

func Get[T any](m *Map, key string) (T, error)

Get retrieves the value of a key from a map and casts it to the desired type. It can return errors if the key is not found or if the value is not of the expected type.

func HavePathInCommon

func HavePathInCommon(a *Map, b *Map) bool

func InformationPaths

func InformationPaths(f *Map) []string

InformationPaths returns a list of paths to all leaf nodes in a map. The format of the paths is a dot-separated string of keys. For example, the following map:

{
    "a": {
        "b": 1,
        "c": {
            "d": 2
        }
    }
}

will return the following paths:

["a.b", "a.c.d"]

func Intersection

func Intersection(one []string, other []string) []string

func RefToLookup

func RefToLookup(ref *Map) []string

func To

func To[T any](m *Map) (T, error)

To converts a Map to a struct by marshalling into JSON and then unmarshalling into the struct.

Example
m := New()
m = m.Set("name", "John")
m = m.Set("age", 30)

type Person struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

p, err := To[Person](m)
if err != nil {
	panic(err)
}

fmt.Println(p.Name)
Output:

John

func ToNativeMap added in v0.0.8

func ToNativeMap(in *Map) map[string]any

func ToStruct added in v0.0.2

func ToStruct(m *Map, out any) error

Types

type ComparableMap

type ComparableMap[T any] interface {
	Equals(other T) bool
}

type Key

type Key struct {
	// contains filtered or unexported fields
}

type KeyValue

type KeyValue struct {
	Key   Key
	Value any
}

type Map

type Map struct {
	// contains filtered or unexported fields
}

func From

func From[T any](f T) (*Map, error)

From converts a struct to a Map by marshalling into JSON and then unmarshalling into the Map.

func FromNativeMap added in v0.0.8

func FromNativeMap(in map[string]any) *Map

func New

func New(opts ...MapOption) *Map

New creates a new map.

func NewFromItems

func NewFromItems(items ...any) *Map

NewFromItems creates a new map from a list of key-value pairs.

func (*Map) Contains

func (m *Map) Contains(key string) bool

func (*Map) Copy

func (m *Map) Copy() *Map

Copy returns a deep copy of a map.

func (*Map) Delete

func (m *Map) Delete(key string) (*Map, bool)

Delete removes a key from the map and returns a new map without the key.

func (*Map) Diff

func (m *Map) Diff(other *Map) (*Map, error)

Diff returns a map with the differences between two maps. The returned map will contain the keys that are not equal in the two maps. The value of the returned map will be the value of the key of the only map that contains it. If the value of a key exists in both maps, the function will compare the values and return the other value if they are different. If the value of a key is a map in both maps, the function will compare the maps recursively.

func (*Map) Equals

func (m *Map) Equals(other *Map) bool

Equals compares two maps recursively and returns true if they are equal.

func (*Map) Error

func (m *Map) Error() string

func (*Map) Get

func (m *Map) Get(key any) (any, bool)

Get retrieves the value of a key from a map. The key can be a string or a list of strings. If the key is a list of strings, the function will traverse the map recursively folloeing the keys in the list.

func (*Map) GetBool

func (m *Map) GetBool(key string) (bool, error)

GetBool retrieves the value of a key from a map and casts it to a bool.

func (*Map) GetFloat

func (m *Map) GetFloat(key string) (float64, error)

GetFloat retrieves the value of a key from a map and casts it to a float64.

func (*Map) GetInt

func (m *Map) GetInt(key string) (int, error)

GetInt retrieves the value of a key from a map and casts it to an int.

func (*Map) GetMap

func (m *Map) GetMap(key string) (*Map, error)

GetMap retrieves the value of a key from a map and casts it to a map.

func (*Map) GetString

func (m *Map) GetString(key string) (string, error)

GetString retrieves the value of a key from a map and casts it to a string.

func (*Map) Keys

func (m *Map) Keys() []string

Keys returns a list of all keys in the map.

func (*Map) MarshalJSON

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

func (*Map) Merge

func (m *Map) Merge(other *Map) *Map

Merge merges two maps. If a key exists in both maps, the value from the other map will be used. If the value of a key is a map in both maps, the maps will be merged recursively.

func (*Map) Set

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

Set implements node.

func (*Map) ToMap

func (m *Map) ToMap() map[string]any

ToMap returns a native Go map with the same structure as the map.

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(d []byte) error

type MapOption

type MapOption func(*MapOptions)

func WithHasher

func WithHasher(h func() hash.Hash64) MapOption

WithHasher sets the hasher used to hash keys in the map.

type MapOptions

type MapOptions struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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