Documentation ¶
Overview ¶
Package utils hold miscelaneous utility functions
Index ¶
- func BoolToInt(b bool) int
- func CloneInterface(a interface{}) interface{}
- func CloneStringMap(m map[string]string) map[string]string
- func ConvertToMapViaYAML(obj interface{}) (map[string]interface{}, error)
- func DatapointToString(dp *datapoint.Datapoint) string
- func Debounce0(fn func(), duration time.Duration) (func(), chan<- struct{})
- func EnsurePrefix(s, prefix string) string
- func FindFieldWithEmbeddedStructs(st interface{}, name string, typ reflect.Type) reflect.Value
- func FirstNonEmpty(s ...string) string
- func FirstNonZero(ns ...int) int
- func GetStructFieldNames(s interface{}) []string
- func IndentLines(ss string, spaces int) string
- func InterfaceMapToStringMap(m map[interface{}]interface{}) map[string]string
- func InterfaceSliceToStringSlice(is []interface{}) []string
- func IsSignalChanClosed(ch <-chan struct{}) bool
- func LowercaseFirstChar(s string) string
- func MakeRange(min, max int) []int
- func MaxInt(x, y int) int
- func MergeInterfaceMaps(maps ...map[string]interface{}) map[string]interface{}
- func MergeStringMaps(maps ...map[string]string) map[string]string
- func MinInt(x, y int) int
- func NewIDGenerator() func() string
- func ParseLineNumberFromYAMLError(e string) int
- func RegexpGroupMap(re *regexp.Regexp, text string) map[string]string
- func RemoveEmptyMapValues(m map[string]string) map[string]string
- func RunOnInterval(fn func(), interval time.Duration) func()
- func SortMapKeys(m map[string]interface{}) []string
- func StringInterfaceMapToAllInterfaceMap(in map[string]interface{}) map[interface{}]interface{}
- func StringMapToInterfaceMap(m map[string]string) map[string]interface{}
- func StringSetToSlice(set map[string]bool) []string
- func StringSliceToMap(strings []string) map[string]bool
- func StripIndent(s string) string
- func UniqueStrings(strings []string) []string
- func YAMLNameOfField(field reflect.StructField) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolToInt ¶
BoolToInt returns 1 if b is true and 0 otherwise. It is useful for datapoints which track a binary value since we don't support boolean datapoint directly.
func CloneInterface ¶
func CloneInterface(a interface{}) interface{}
CloneInterface takes an object and returns a copy of it regardless of whether it is really a pointer underneath or not. It is roughly equivalent to the following: b = *a (if 'a' is a pointer) b = a (if 'a' is not a pointer)
func CloneStringMap ¶
CloneStringMap makes a shallow copy of a map[string]string
func ConvertToMapViaYAML ¶
ConvertToMapViaYAML takes a struct and converts it to map[string]interface{} by marshalling it to yaml and back to a map. This will return nil if the conversion was not successful.
func DatapointToString ¶
DatapointToString pretty prints a datapoint in a consistent manner for logging purposes. The most important thing here is to sort the dimension dict so it is consistent so that it is easier to visually scan a large list of datapoints.
func EnsurePrefix ¶
EnsurePrefix ensures that string s starts with the given prefix. If it already starts with that prefix, the original string is returned unaltered.
func FindFieldWithEmbeddedStructs ¶
FindFieldWithEmbeddedStructs will look for a field with the given name, recursing down into embedded structs if there are any.
func FirstNonEmpty ¶
FirstNonEmpty returns the first string that is not empty, otherwise ""
func FirstNonZero ¶
FirstNonZero returns the first int in `ns` that is not zero.
func GetStructFieldNames ¶
func GetStructFieldNames(s interface{}) []string
GetStructFieldNames returns a slice with the names of all of the fields in the struct `s`. This will panic if `s` is not a struct.
func IndentLines ¶
IndentLines indents all lines in `ss` by `spaces` number of spaces
func InterfaceMapToStringMap ¶
InterfaceMapToStringMap converts a map[interface{}]interface{} to a map[string]string. Keys and values will be converted with fmt.Sprintf so the original key/values don't have to be strings.
func InterfaceSliceToStringSlice ¶
func InterfaceSliceToStringSlice(is []interface{}) []string
InterfaceSliceToStringSlice returns a new slice that contains the elements of `is` as strings. Returns nil if any of the elements of `is` are not strings.
func IsSignalChanClosed ¶
func IsSignalChanClosed(ch <-chan struct{}) bool
IsSignalChanClosed returns whether a channel is closed that is used only for the sake of sending a single singal. The channel should never be sent any actual values, but should only be closed to tell other goroutines to stop.
func LowercaseFirstChar ¶
LowercaseFirstChar make the first character of a string lowercase
func MergeInterfaceMaps ¶
MergeInterfaceMaps merges any number of map[string]interface{} with a later map's keys overriding earlier maps. Nil values do not override earlier values.
func MergeStringMaps ¶
MergeStringMaps merges n maps with a later map's keys overriding earlier maps
func NewIDGenerator ¶
func NewIDGenerator() func() string
NewIDGenerator returns a function that will produce, for any given generator instance, a unique, non-empty, string value each time it is called.
func ParseLineNumberFromYAMLError ¶
ParseLineNumberFromYAMLError takes an error message nested in yaml.TypeError and returns a line number if indicated in the error message. This is pretty hacky but is the only way to actually get at the line number in the standard yaml package.
func RegexpGroupMap ¶
RegexpGroupMap matches text against the given regexp and returns a map of all of the named subgroups to the values found in text. Returns nil if text does not match.
func RemoveEmptyMapValues ¶
RemoveEmptyMapValues will strip a map of any key/value pairs for which the value is the empty string.
func RunOnInterval ¶
RunOnInterval the given fn once every interval. Returns a function that can be called to stop running the function.
func SortMapKeys ¶
SortMapKeys returns a slice of all of the keys of a map sorted alphabetically ascending.
func StringInterfaceMapToAllInterfaceMap ¶
func StringInterfaceMapToAllInterfaceMap(in map[string]interface{}) map[interface{}]interface{}
StringInterfaceMapToAllInterfaceMap converts a map[string]interface{} to a map[interface{}]interface{}
func StringMapToInterfaceMap ¶
StringMapToInterfaceMap converts a map[string]string to a map[string]interface{}.
func StringSetToSlice ¶
StringSetToSlice converts a map representing a set into a slice of strings
func StringSliceToMap ¶
StringSliceToMap converts a slice of strings into a map with keys from the slice
func StripIndent ¶
StripIndent looks at the first line in s and strips off whatever whitespace indentation it has from every line in s. If subsequent lines do not start with the same indentation as the first line, results are undefined. If the first line is blank, it will be removed before processing.
func UniqueStrings ¶
UniqueStrings returns a slice with the unique set of strings from the input
func YAMLNameOfField ¶
func YAMLNameOfField(field reflect.StructField) string
YAMLNameOfField returns the YAML key that is used for the given struct field. It does this by actually serializing the field and parsing the output string. If the field has no key (e.g. if the `yaml:"-"` tag is set, this will return an empty string.
Types ¶
This section is empty.