Documentation
¶
Index ¶
- Constants
- Variables
- func EqualsAny(a any, b any) bool
- func EqualsAnyList(a []any, b []any) bool
- func EqualsStringList(a []string, b []string) bool
- func Get[T any](m *Map, key string) (T, error)
- func HavePathInCommon(a *Map, b *Map) bool
- func InformationPaths(f *Map) []string
- func Intersection(one []string, other []string) []string
- func RefToLookup(ref *Map) []string
- func To[T any](m *Map) (T, error)
- func ToNativeMap(in *Map) map[string]any
- func ToStruct(m *Map, out any) error
- type ComparableMap
- type Key
- type KeyValue
- type Map
- func (m *Map) Contains(key string) bool
- func (m *Map) Copy() *Map
- func (m *Map) Delete(key string) (*Map, bool)
- func (m *Map) Diff(other *Map) (*Map, error)
- func (m *Map) Equals(other *Map) bool
- func (m *Map) Error() string
- func (m *Map) Get(key any) (any, bool)
- func (m *Map) GetBool(key string) (bool, error)
- func (m *Map) GetFloat(key string) (float64, error)
- func (m *Map) GetInt(key string) (int, error)
- func (m *Map) GetMap(key string) (*Map, error)
- func (m *Map) GetString(key string) (string, error)
- func (m *Map) Keys() []string
- func (m *Map) MarshalJSON() ([]byte, error)
- func (m *Map) Merge(other *Map) *Map
- func (m *Map) Set(key string, value any) *Map
- func (m *Map) ToMap() map[string]any
- func (m *Map) UnmarshalJSON(d []byte) error
- type MapOption
- type MapOptions
Examples ¶
Constants ¶
const ( BitPartitionSize = 6 MaskLevel0 = 0b11111100 << 56 )
Variables ¶
var ( ErrDeleteNotImplemented = fmt.Errorf("delete is not implemented") ErrKeyNotFound = fmt.Errorf("key not found") ErrWrongType = fmt.Errorf("wrong type") )
var DefaultMapOptions = MapOptions{ // contains filtered or unexported fields }
Functions ¶
func EqualsAnyList ¶
func EqualsStringList ¶
func Get ¶
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 InformationPaths ¶
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 RefToLookup ¶
func To ¶
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
Types ¶
type ComparableMap ¶
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
func From ¶
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 NewFromItems ¶
NewFromItems creates a new map from a list of key-value pairs.
func (*Map) Diff ¶
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) Get ¶
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) MarshalJSON ¶
func (*Map) Merge ¶
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) UnmarshalJSON ¶
type MapOption ¶
type MapOption func(*MapOptions)
func WithHasher ¶
WithHasher sets the hasher used to hash keys in the map.
type MapOptions ¶
type MapOptions struct {
// contains filtered or unexported fields
}