utils

package
v3.7.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2018 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package utils hold miscelaneous utility functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolToInt

func BoolToInt(b bool) int

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 ChunkScanner

func ChunkScanner(output io.ReadCloser) *bufio.Scanner

ChunkScanner looks for a line and all subsequent indented lines and returns a scanner that will output that chunk as a single token. This assumes that the entire chunk comes in a single read call, which will not always be the case.

func CloneAndExcludeStringMapByKey

func CloneAndExcludeStringMapByKey(in map[string]string, exclude map[string]bool) map[string]string

CloneAndExcludeStringMapByKey clones a string map excluding the specified keys

func CloneAndFilterStringMapWithFunc

func CloneAndFilterStringMapWithFunc(in map[string]string, filter func(string, string) bool) (out map[string]string)

CloneAndFilterStringMapWithFunc clones a string map and only includes key/value pairs for which the filter function returns true

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

func CloneStringMap(m map[string]string) map[string]string

CloneStringMap makes a shallow copy of a map[string]string

func ConvertToMapViaYAML

func ConvertToMapViaYAML(obj interface{}) (map[string]interface{}, error)

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

func DatapointToString(dp *datapoint.Datapoint) string

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 Debounce0

func Debounce0(fn func(), duration time.Duration) (func(), chan<- struct{})

Debounce0 calls a zero arg function on the trailing edge of every `duration`.

func DecodeValueGenerically

func DecodeValueGenerically(val string) interface{}

DecodeValueGenerically apply some very basic heuristics to decode string values to the most sensible type for use in config structs.

func DuplicateInterfaceMapKeysAsCamelCase

func DuplicateInterfaceMapKeysAsCamelCase(m map[string]interface{}) map[string]interface{}

DuplicateInterfaceMapKeysAsCamelCase takes a map[string]interface{} and camel cases the keys

func EnsurePrefix

func EnsurePrefix(s, prefix string) string

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

func FindFieldWithEmbeddedStructs(st interface{}, name string, typ reflect.Type) reflect.Value

FindFieldWithEmbeddedStructs will look for a field with the given name, recursing down into embedded structs if there are any.

func FirstNonEmpty

func FirstNonEmpty(s ...string) string

FirstNonEmpty returns the first string that is not empty, otherwise ""

func FirstNonZero

func FirstNonZero(ns ...int) int

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

func IndentLines(ss string, spaces int) string

IndentLines indents all lines in `ss` by `spaces` number of spaces

func InterfaceMapToStringMap

func InterfaceMapToStringMap(m map[interface{}]interface{}) map[string]string

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 IsStructOrPointerToStruct

func IsStructOrPointerToStruct(typ reflect.Type) bool

IsStructOrPointerToStruct returns true if the given reflect.Type is a struct or pointer to a struct

func LowercaseFirstChar

func LowercaseFirstChar(s string) string

LowercaseFirstChar make the first character of a string lowercase

func MakeRange

func MakeRange(min, max int) []int

MakeRange creates an int slice containing all ints between `min` and `max`

func MaxInt

func MaxInt(x, y int) int

MaxInt returns the greater of x and y

func MergeInterfaceMaps

func MergeInterfaceMaps(maps ...map[string]interface{}) map[string]interface{}

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

func MergeStringMaps(maps ...map[string]string) map[string]string

MergeStringMaps merges n maps with a later map's keys overriding earlier maps

func MinInt

func MinInt(x, y int) int

MinInt returns the lesser of x and y

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

func ParseLineNumberFromYAMLError(e string) int

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

func RegexpGroupMap(re *regexp.Regexp, text string) map[string]string

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 RemoveAllElementsFromStringSlice

func RemoveAllElementsFromStringSlice(inputStrings []string, toRemoveStrings []string) []string

RemoveAllElementsFromStringSlice removes all elements from toRemove that exists in inputStrings

func RemoveEmptyMapValues

func RemoveEmptyMapValues(m map[string]string) map[string]string

RemoveEmptyMapValues will strip a map of any key/value pairs for which the value is the empty string.

func RunOnArrayOfIntervals

func RunOnArrayOfIntervals(ctx context.Context, fn func(), intervals []time.Duration, repeatPolicy RepeatPolicy)

RunOnArrayOfIntervals the given function once on the specified intervals, and repeat according to the supplied RepeatPolicy. Please note the function is executed after the first interval. If you want the function executed immediately, you should specify a duration of 0 as the first element in the intervals array.

func RunOnInterval

func RunOnInterval(ctx context.Context, fn func(), interval time.Duration)

RunOnInterval the given fn once every interval, starting at the moment the function is called. Returns a function that can be called to stop running the function.

func SortMapKeys

func SortMapKeys(m map[string]interface{}) []string

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

func StringMapToInterfaceMap(m map[string]string) map[string]interface{}

StringMapToInterfaceMap converts a map[string]string to a map[string]interface{}.

func StringSetToSlice

func StringSetToSlice(set map[string]bool) []string

StringSetToSlice converts a map representing a set into a slice of strings

func StringSliceToMap

func StringSliceToMap(strings []string) map[string]bool

StringSliceToMap converts a slice of strings into a map with keys from the slice

func StripIndent

func StripIndent(s string) string

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

func UniqueStrings(strings []string) []string

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.

func YAMLNameOfFieldInStruct

func YAMLNameOfFieldInStruct(fieldName string, st interface{}) string

YAMLNameOfFieldInStruct returns the YAML key that is used for the given struct field, looking up fieldName in the given st struct. If the field has no key (e.g. if the `yaml:"-"` tag is set, this will return an empty string. It uses YAMLNameOfField under the covers. If st is not a struct, this will panic.

Types

type RepeatPolicy

type RepeatPolicy int

RepeatPolicy repeat behavior for RunOnIntervals Function

const (
	// RepeatAll repeats all intervals
	RepeatAll RepeatPolicy = iota
	// RepeatLast repeats only the last interval
	RepeatLast
	// RepeatNone does not repeat
	RepeatNone
)

Directories

Path Synopsis
Package filter contains common filtering logic that can be used to filter datapoints or various resources within other agent components, such as monitors.
Package filter contains common filtering logic that can be used to filter datapoints or various resources within other agent components, such as monitors.
network

Jump to

Keyboard shortcuts

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