utils

package
v0.0.0-...-a5f9fc7 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 2 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains(list []string, element string) bool

Contains checks if a given string is in a given list of strings. Returns true if the element was found, false otherwise.

Example:

	list := []string{"a", "b", "c"}
	element := "b"
 contains := Contains(list, element)  // contains is true

func ErrorOf

func ErrorOf(_ any, err error) error

ErrorOf is used to wrap a function call and extract the error out of it For example, in this code function Foo returns an error,

func Bar() (string, error){
 // ...
}

The Foo function can use ErrorOf to return the error from Bar:

func Foo() error{
	return ErrorOf(Bar())
}

func FindKeyCaseInsensitive

func FindKeyCaseInsensitive(input map[string]string, key string) (string, bool)

This method tries to find the given key is in the map. It searches different cases of the key:

  1. the exact match
  2. all lower case letters
  3. all upper case letters

If the key in any of these versions was found, it'll be returned alongside with a boolean indicating whether or not it was found.

Example:

map := {"a": "b", "c": "d"}
key := "A"
key, found = FindKeyCaseInsensitive(map, key)  // key is "a" and found is true

func FindValueCaseInsensitive

func FindValueCaseInsensitive(input map[string]string, key string) (string, bool)

This method tries to find the given key is in the map and return its value. It searches different cases of the key:

  1. the exact match
  2. all lower case letters
  3. all upper case letters

If the key in any of these versions was found, its value will be returned alongside with a boolean indicating whether or not it was found.

Example:

map := {"a": "b", "c": "d"}
key := "A"
value, found = FindValueCaseInsensitive(map, key)  // value is "b" and found is true

func Merge

func Merge(input1 []string, input2 []string) []string

Merge merges two lists of strings and returns the result. The result will contain all elements from the first list and all elements from the second list which are not already in the first list.

Example:

list1 := []string{"a", "b", "c"}
list2 := []string{"b", "c", "d"}
mergedList := Merge(list1, list2)  // mergedList is ["a", "b", "c", "d"]

func Remove

func Remove(input map[string]string, key string) map[string]string

Removes a given key from the input map and uses FindKeyCaseInsensitive() for this. The resulting map is being returned. If the key was not found, the input map will be returned.

Example:

map := {"a": "b", "c": "d"}
key := "A"
map = Remove(map, key)  // map is {"c": "d"}

func RemoveSimilar

func RemoveSimilar(list []string, element string) []string

RemoveSimilar removes all elements from the list which contain the given element. Returns the filtered list.

Example:

list := []string{"a", "b", "c"}
element := "b"
filteredList := RemoveSimilar(list, element)  // filteredList is ["a", "c"]

func ToKeyValueMap

func ToKeyValueMap(input []string, splitBy string) map[string]string

ToKeyValueMap converts a list of strings to a map of strings. The input list will be converted based on the delimiter, 'splitBy'. The resulting map will contain the keys and values of the input list.

Example:

list := []string{"a=b", "c=d"}
splitBy := "="
keyValueMap := ToKeyValueMap(list, splitBy)  // keyValueMap is {"a": "b", "c": "d"}

func ToSlice

func ToSlice(input map[string]string, combineBy string) []string

ToSlice converts a map of strings to a list of strings. The keys and values will be combined by the given delimiter, 'combineBy'. The resulting list will contain the keys and values of the input map.

Example:

map := {"a": "b", "c": "d"}
combineBy := "="
slice := ToSlice(map, combineBy)  // slice is ["a=b", "c=d"]

func ValueOf

func ValueOf[T any](value T, _ error) T

Types

This section is empty.

Jump to

Keyboard shortcuts

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