maps

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2024 License: Apache-2.0 Imports: 14 Imported by: 43

README

godoc codecov Go Report Card

maps - go map type utilities

maps is a package for general purpose map-type things, such as getting a list of naturally SortedKeys.

Installation

> go get github.com/go-corelibs/maps@latest

Examples

SortedKeys

func main() {
    m := map[string]struct{}{
        "one": {},
        "two": {},
        "many": {},
        "v1": {},
        "v10": {},
        "v2": {},
    }
    keys := maps.SortedKeys(m)
    // keys == []string{"many", "one", "two", "v1", "v2", "v10"}
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelizeKeys added in v1.2.0

func CamelizeKeys[K ~string, V interface{}](data map[K]V)

CamelizeKeys is a CamelCase wrapper around RenameKeys

func CopyBaseType added in v1.2.0

func CopyBaseType[K comparable, V BaseTypes](src map[K]V) (dst map[K]V)

CopyBaseType returns a shallow copy of the given map[comparable]BaseTypes

func DeepCopy added in v1.2.0

func DeepCopy[V interface{}](src map[string]V) (dst map[string]V)

DeepCopy is a wrapper around values.DeepCopy

func Delete added in v1.2.0

func Delete(m map[string]interface{}, key string)

func DeleteKV added in v1.2.0

func DeleteKV(c map[string]interface{}, key string) (deleted bool)

DeleteKV uses Delete if the first character of the key is a period ("."), otherwise DeleteKV uses GetKV to find the correct key and then deletes it from the map

func Dump added in v1.2.0

func Dump[T comparable, V interface{}](m map[T]V) (pretty string)

Dump is a convenience wrapper around github.com/gookit/goutil/dump to return a human-readable representation of the given map

Only really useful during ad-hoc development cycles

func Get added in v1.2.0

func Get(m map[string]interface{}, key string) (value interface{})

func GetKV added in v1.2.0

func GetKV(c map[string]interface{}, key string) (k string, v interface{})

GetKV finds the relative key given, returning both the actual key and the associated interface{} value, using the following procedure:

1. uses Get if the first character of the key is a period (".") 2. checks if the key is already an exact match 3. checks for a CamelCased version of the key 4. checks for a kebab-cased version of the key 5. checks for a snake_cased version of the key

func Has added in v1.2.0

func Has(m map[string]interface{}, key string) (present bool)

Has checks for the presence of the (deep or exact) key given

func KebabKeys added in v1.2.0

func KebabKeys[K ~string, V interface{}](data map[K]V)

KebabKeys is a kebab-case wrapper around RenameKeys

func Keys added in v1.2.0

func Keys[K comparable, V interface{}](data map[K]V) (keys []K)

Keys returns the list of map keys, in whatever order Go produces from a simple range over the data

func MakeTypedKey added in v1.2.0

func MakeTypedKey[K comparable, L comparable, V interface{}, M map[L]V](m map[K]M, key K) (made bool)

MakeTypedKey is used to simplify the adding of more map values to a parent map without having to check if the value map exists already or needs to be created first

Example:

// Standard way
m := make(map[string]map[string]struct{})
if _, present := m["top"]; !present {
  m["top"] = make(map[string]struct{})
}
m["top"]["thing"] = struct{}{}

// Using MakeTypedKey
m := make(map[string]map[string]struct{})
_ = maps.MakeTypedKey(m, "top")
m["top"]["thing"] = struct{}{}

func OrderedKeys added in v1.2.0

func OrderedKeys[K cmp.Ordered, V interface{}](data map[K]V) (keys []K)

OrderedKeys returns the list of cmp.Ordered keys in ascending order

func ParseDeepKeySlice added in v1.2.0

func ParseDeepKeySlice(input string) (key string, idx int, ok bool)

func RenameKeys added in v1.2.0

func RenameKeys[K ~string, V interface{}](data map[K]V, rename func(original K) (renamed K))

RenameKeys ranges over the given data and calls the rename function for each key, if the renamed string is different from the original string, the renamed key is set on the data and the original key is deleted

func ReverseOrderedKeys added in v1.2.0

func ReverseOrderedKeys[K cmp.Ordered, V interface{}](data map[K]V) (keys []K)

ReverseOrderedKeys returns the list of cmp.Ordered keys in descending order

func ReverseSortedKeys added in v1.2.0

func ReverseSortedKeys[K ~string, V interface{}](data map[K]V) (keys []K)

ReverseSortedKeys returns the list of keys in reverse natural sorted order

func ReverseSortedNumbers

func ReverseSortedNumbers[K maths.Number, V interface{}](data map[K]V) (keys []K)

ReverseSortedNumbers returns a slice of descending sorted keys from the given map

func ScreamingSnakeKeys added in v1.2.0

func ScreamingSnakeKeys[K ~string, V interface{}](data map[K]V)

ScreamingSnakeKeys converts all keys to kebab-case

For each key/value pair: - if the key is already kebab-cased, does nothing - if the kebab-cased key does not exist, sets it - deletes the original, not-kebab-case key

func Set added in v1.2.0

func Set(m map[string]interface{}, key string, value interface{}) (err error)

Set checks if the key being set is a deep key and if so, sets the value deeply within the map structure. Set is a simple map value assignment for non-deep key cases

func SetKV added in v1.2.0

func SetKV(c map[string]interface{}, key string, value interface{}) (err error)

SetKV uses Set if the first character of the key is a period ("."), otherwise SetKV sets the map value to a CamelCased version of the key

func SnakeKeys added in v1.2.0

func SnakeKeys[K ~string, V interface{}](data map[K]V)

SnakeKeys is a snake_case wrapper around RenameKeys

func SortedKeyLengths added in v1.2.0

func SortedKeyLengths[K ~string, V interface{}](data map[K]V) (keys []K)

SortedKeyLengths returns the list of keys, from longest to shortest and natural sorted for same length keys

func SortedKeys

func SortedKeys[K ~string, V interface{}](data map[K]V) (keys []K)

SortedKeys returns a slice of natural-sorted keys from the given map

func SortedKeysByLastName added in v1.2.0

func SortedKeysByLastName[K ~string, V interface{}](data map[K]V) (keys []K)

SortedKeysByLastName assumes that the keys in the map are human names and uses strings.LastName produce a list of keys which are sorted by last name and uses a natural sorting for keys where the last names are the same

func SortedNumbers

func SortedNumbers[K maths.Number, V interface{}](data map[K]V) (keys []K)

SortedNumbers returns a slice of ascending sorted keys from the given map

func ToEnviron added in v1.2.0

func ToEnviron[K ~string, V interface{}](data map[K]V) (environ []string)

ToEnviron transforms the map into a SortedKeys os.Environ slice of KEY=value pairs. ToEnviron uses fmt.Sprintf("%v") to transform the values

Example:

environ := ToEnviron(map[string]interface{}{"one":"two", "many": 10})
// environ == []string{"ONE=two", "MANY=10"}

func ValuesSortedByKeys added in v1.1.0

func ValuesSortedByKeys[K ~string, V interface{}](data map[K]V) (values []V)

ValuesSortedByKeys returns a slice of values, ordered by SortedKeys

func ValuesSortedByNumbers added in v1.1.0

func ValuesSortedByNumbers[K maths.Number, V interface{}](data map[K]V) (values []V)

ValuesSortedByNumbers returns a slice of values, ordered by SortedNumbers

Types

type BaseTypes added in v1.2.0

type BaseTypes interface {
	~bool |
		~string | []byte | []rune |
		~float32 | ~float64 |
		~complex64 | ~complex128 |
		~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

BaseTypes is a generic interface defining types suitable for a shallow copy using CopyBaseType

Jump to

Keyboard shortcuts

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