utils

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Version        = "dev"
	CommitHash     = "n/a"
	BuildTime      = "n/a"
	Branch         = "n/a"
	StartTimestamp = time.Now()
)

Functions

func ArgsMapToObject added in v1.0.0

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

ArgsMapToObject converts a string map to an object via JSON encode/decode

func BuildFullVersion added in v1.2.0

func BuildFullVersion() string

func BuildVersion added in v1.2.0

func BuildVersion() string

func ClearString added in v1.0.0

func ClearString(str string) string

func ConvertToBool added in v1.3.0

func ConvertToBool(v interface{}) (bool, error)

ConvertToBool converts a value to a bool

func ConvertToFloat added in v1.3.0

func ConvertToFloat(v interface{}) (float64, error)

ConvertToFloat converts a value to a float64

func ConvertToInt64 added in v1.3.0

func ConvertToInt64(v interface{}) (int64, error)

ConvertToInt64 converts a value to an int

func ConvertToString added in v1.3.0

func ConvertToString(v interface{}) (string, error)

ConvertToString converts a value to a string

func DeleteElemFromSlice added in v1.2.0

func DeleteElemFromSlice(s []string, i int) []string

DeleteElemFromSlice swaps i-th and last Element and deletes the last

func DeleteTempDir added in v1.2.0

func DeleteTempDir() error

DeleteTempDir deletes the temp dir that was created when the process was started

func GetDefaultPath

func GetDefaultPath(productname string) string

func GetDurationMap added in v1.2.0

func GetDurationMap() map[string]string

GetDurationMap returns a map of typical durations for operator argument suggestions

func GetHexColor added in v1.2.0

func GetHexColor(c color.Color) string

GetHexColor returns the hex represantion of a Go-color

func GetSectionsMap added in v0.9.1

func GetSectionsMap(jsonBytes []byte) (map[string]interface{}, error)

GetSectionsMap returns a map of section names (all lower case) to config-objects, the type of config-object depends on the section

func GetTempDir added in v1.2.0

func GetTempDir() (string, error)

GetTempDir returns the temp dir that was created when the process was started

func KeysToLower added in v1.3.0

func KeysToLower(input map[string]string) map[string]string

KeysToLower converts all keys in a map to lower case (always returns a new map, even if the input map is nil)

func MapToURLArgs added in v1.3.0

func MapToURLArgs(args map[string]string) url.Values

MapToURLArgs converts a string map to a string map of arrays and splits multiple values per key

func ObjectToArgsMap added in v1.3.0

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

ObjectToArgsMap converts and object to a string map via JSON encode/decode

func ParseBool added in v1.2.0

func ParseBool(str string) bool

ParseBool returns the bool value represented by string

func ParseHexColor added in v1.2.0

func ParseHexColor(s string) (c color.RGBA, err error)

func ReadBytesFromFile added in v0.9.1

func ReadBytesFromFile(filePath string, configFileDir string) []byte

func ReadBytesFromUrl added in v0.9.1

func ReadBytesFromUrl(url string) []byte

func ReadObjectFromURL added in v1.0.0

func ReadObjectFromURL(url string, obj interface{}) error

ReadObjectFromURL decodes a JSON object from an http stream

func ReadSectionWithDefaults

func ReadSectionWithDefaults(jsonBytes []byte, sectionName string, configStruct interface{}, configFileDir string) ([]byte, error)

ReadSectionWithDefaults parses the content of the first-level-JSON object in <sectionName> into configStruct

if the section exists, configStruct will first get overwritten by an optional included file, then by the contents of the section (append+overwrite in both cases), returns an empty byte-slice if the section does not exist, the serialized content of configStruct (assuming these are default values) is added to jsonBytes and returned

func RemoveSection added in v1.3.0

func RemoveSection(jsonBytes []byte, sectionName string) ([]byte, error)

RemoveSection removes the given section from the config file, returns a byte-slice with the new content, empty byte slice if nothing was removed

func StringCmpIgnoreCase added in v1.3.0

func StringCmpIgnoreCase(a string, b string) bool

StringCmpIgnoreCase compares two strings ignoring case

func StringEndsWith added in v1.3.0

func StringEndsWith(input string, suffix string) bool

func StringPtr added in v1.3.0

func StringPtr(input string) *string

StringPtr returns a pointer to a string

func StringStartsWith added in v1.3.0

func StringStartsWith(input string, prefix string) bool

func StringToBool added in v1.3.0

func StringToBool(input string) bool

StringToBool converts a string to a bool

func StringToFloat64 added in v1.3.0

func StringToFloat64(input string) (float64, error)

StringToFloat64 converts a string to a float64

func StringToIdentifier added in v1.3.0

func StringToIdentifier(input string) string

func StringToInt added in v1.3.0

func StringToInt(input string) (int, error)

StringToInt converts a string to an int

func StringToLower added in v1.3.0

func StringToLower(input string) string

StringToLower converts a string to lower case

func URLArgsToJSON

func URLArgsToJSON(args map[string][]string) []byte

URLArgsToJSON converts the string map of arrays to a string map of strings by dropping all but the first elements from the map, it returns the json serialization of the map

func URLArgsToMap added in v1.0.0

func URLArgsToMap(args url.Values) map[string]string

URLArgsToMap converts the string map of arrays to a string map of strings by joining multiple values per key this simplfies handling of single key-value pairs, while consciously sacrificing keys with multiple values

func URLParseQuery added in v1.3.0

func URLParseQuery(query string) (map[string]string, error)

URLParseQuery parses a query string and returns a map of the values

func WriteSection added in v0.9.1

func WriteSection(jsonBytes []byte, sectionName string, configStruct interface{}, preserveExisting bool) ([]byte, error)

WriteSection puts the ConfigStruct object in the config file by preserving everything that is part of the section

func WriteSectionBytes added in v1.3.0

func WriteSectionBytes(jsonBytes []byte, sectionName string, sectionBytes []byte) ([]byte, error)

WriteSectionBytes puts the given bytes into the section of the config file

Types

type CIMap added in v1.3.0

type CIMap[Val any] interface {
	// Has returns true if map contains a key
	Has(key string) bool
	// Get returns the first value stored for key or the default value if key is not in map
	Get(key string) Val
	// GetOrDefault returns the first value stored for key or the given default value if key is not in map
	GetOrDefault(key string, defaultVal Val) Val
	// GetValues returns all values stored for key
	GetValues(key string) []Val

	// GetLowerCaseKeys returns the stored keys in lower case
	GetLowerCaseKeys() []string
	// GetOriginalKeys returns the stored key in original case
	GetOriginalKeys() []string

	GetOriginalCase(key string) string // problematic -> what to do if key does not exist

	GetOriginalCaseMap() map[string][]Val
	GetOriginalCaseMapOnlyFirst() map[string]Val
	GetOriginalCaseMapJoined() map[string]Val
	GetLowerCaseMap() map[string][]Val
	GetLowerCaseMapOnlyFirst() map[string]Val
	GetLowerCaseMapJoined() map[string]Val

	IsEmpty() bool
}

CIMap is a struct that can be used to pass arguments to a function

func NewStringCIMap added in v1.3.0

func NewStringCIMap(args map[string]string) CIMap[string]

NewStringCIMap creates a new CIMap struct from the given map

func NewStringCIMapFromValues added in v1.3.0

func NewStringCIMapFromValues(args map[string][]string) CIMap[string]

NewStringCIMapFromValues creates a new FunctionArguments struct from the given url.Values

type CIMapImpl added in v1.3.0

type CIMapImpl[Val any] struct {
	OriginalMap map[string][]Val // map with original case keys
	// contains filtered or unexported fields
}

CIMapImpl is a struct similar to url.Values that allows case-insensitive comparisons but stores the orignal case it can be built from url.Values or a map[string]Val and preserves the case of the keys but allows accessing them in a case-insensitive way when inserting keys with different cases, they will be combined into one key with the first case used

func (*CIMapImpl[Val]) Get added in v1.3.0

func (fa *CIMapImpl[Val]) Get(key string) Val

Get returns the first value for the given key

func (*CIMapImpl[Val]) GetLowerCaseKeys added in v1.3.0

func (fa *CIMapImpl[Val]) GetLowerCaseKeys() []string

GetLowerCaseKeys returns the stored keys in lower case

func (*CIMapImpl[Val]) GetLowerCaseMap added in v1.3.0

func (fa *CIMapImpl[Val]) GetLowerCaseMap() map[string][]Val

GetLowerCaseMap returns a map of all keys in lower case

func (*CIMapImpl[Val]) GetLowerCaseMapJoined added in v1.3.0

func (fa *CIMapImpl[Val]) GetLowerCaseMapJoined() map[string]string

GetLowerCaseMapJoined

func (*CIMapImpl[Val]) GetLowerCaseMapOnlyFirst added in v1.3.0

func (fa *CIMapImpl[Val]) GetLowerCaseMapOnlyFirst() map[string]Val

GetLowerCaseMapOnlyFirst returns a map of all keys in lower case with only a single value

func (*CIMapImpl[Val]) GetOrDefault added in v1.3.0

func (fa *CIMapImpl[Val]) GetOrDefault(key string, defaultVal Val) Val

GetOrDefault returns the first value for the given key

func (*CIMapImpl[Val]) GetOriginalCase added in v1.3.0

func (fa *CIMapImpl[Val]) GetOriginalCase(key string) string

GetOriginalCase returns the key in the correct variant (if multiple, whichever comes first)

func (*CIMapImpl[Val]) GetOriginalCaseMap added in v1.3.0

func (fa *CIMapImpl[Val]) GetOriginalCaseMap() map[string][]Val

GetOriginalCaseMap returns a map of all keys in the original case (this will contain only one case-variant if multiple key with different cases were inserted)

func (*CIMapImpl[Val]) GetOriginalCaseMapJoined added in v1.3.0

func (fa *CIMapImpl[Val]) GetOriginalCaseMapJoined() map[string]string

GetOriginalCaseMapJoined

func (*CIMapImpl[Val]) GetOriginalCaseMapOnlyFirst added in v1.3.0

func (fa *CIMapImpl[Val]) GetOriginalCaseMapOnlyFirst() map[string]Val

GetOriginalCaseMap returns a map of all keys in the original case (this will contain only one case-variant if multiple key with different cases were inserted)

func (*CIMapImpl[Val]) GetOriginalKeys added in v1.3.0

func (fa *CIMapImpl[Val]) GetOriginalKeys() []string

GetKeys returns the stored key in original case

func (*CIMapImpl[Val]) GetValues added in v1.3.0

func (fa *CIMapImpl[Val]) GetValues(key string) []Val

GetValues returns all values for the given key

func (*CIMapImpl[Val]) Has added in v1.3.0

func (fa *CIMapImpl[Val]) Has(key string) bool

Has returns true if the given key is present in any variant

func (*CIMapImpl[Val]) IsEmpty added in v1.3.0

func (fa *CIMapImpl[Val]) IsEmpty() bool

IsEmpty returns true if there are no keys in the map

func (*CIMapImpl[Val]) MarshalJSON added in v1.3.0

func (fa *CIMapImpl[Val]) MarshalJSON() ([]byte, error)

MarshalJSON provides a custom marshaller with better readable time formats

type ConfigIncluder

type ConfigIncluder struct {
	Include        string
	IncludeFromURL string
}

type ConfigReader

type ConfigReader struct {
	// contains filtered or unexported fields
}

func NewConfigReader

func NewConfigReader(logger logrus.FieldLogger, configFilePath string) (*ConfigReader, error)

func (*ConfigReader) GetConfigDir added in v1.0.0

func (c *ConfigReader) GetConfigDir() string

GetConfigDir returns the basepath of the config file

func (*ConfigReader) GetConfigFileContent added in v1.0.0

func (c *ConfigReader) GetConfigFileContent() string

GetConfigFileContent returns the content of the config file

func (*ConfigReader) GetSectionBytes added in v0.9.1

func (c *ConfigReader) GetSectionBytes(sectionName string) ([]byte, error)

GetSectionBytes returns the bytes of the section given by sectionName

func (*ConfigReader) GetSectionNames added in v0.9.1

func (c *ConfigReader) GetSectionNames() ([]string, error)

GetSectionNames returns the names of all sections in the config file

func (*ConfigReader) GetSectionNamesWithPrefix added in v1.3.0

func (c *ConfigReader) GetSectionNamesWithPrefix(prefix string) ([]string, error)

GetSectionNamesWithPrefix returns the names of all sections in the config file that start with prefix

func (*ConfigReader) ReadObjectFromFile added in v1.0.0

func (c *ConfigReader) ReadObjectFromFile(obj interface{}, filename string) error

func (*ConfigReader) ReadObjectFromURL added in v1.0.0

func (c *ConfigReader) ReadObjectFromURL(obj interface{}, url string) error

func (*ConfigReader) ReadSectionWithDefaults

func (c *ConfigReader) ReadSectionWithDefaults(sectionName string, configStruct interface{}) error

func (*ConfigReader) RemoveFile added in v1.2.0

func (c *ConfigReader) RemoveFile(filename string) error

func (*ConfigReader) RemoveSection added in v1.3.0

func (c *ConfigReader) RemoveSection(sectionName string) error

func (*ConfigReader) SetConfigFileContent added in v1.0.0

func (c *ConfigReader) SetConfigFileContent(newContent string) error

SetConfigFileContent validates the new Content and sets it if valid

func (*ConfigReader) WriteBackConfigIfChanged

func (c *ConfigReader) WriteBackConfigIfChanged() error

func (*ConfigReader) WriteObjectToFile added in v1.0.0

func (c *ConfigReader) WriteObjectToFile(obj interface{}, filename string) error

func (*ConfigReader) WriteSection added in v0.9.1

func (c *ConfigReader) WriteSection(sectionName string, configStruct interface{}, persistImmediately bool) error

func (*ConfigReader) WriteSectionBytes added in v1.3.0

func (c *ConfigReader) WriteSectionBytes(sectionName string, sectionBytes []byte) error

type TopKEntry added in v1.3.0

type TopKEntry struct {
	Value     string
	Timestamp time.Time
}

TopKEntry is an entry in a TopKList

type TopKList added in v1.3.0

type TopKList struct {
	// contains filtered or unexported fields
}

TopKList is a top-k-list of strings with timestamps sorted by timestamp

func NewTopKList added in v1.3.0

func NewTopKList(maxLen int) *TopKList

NewTopKList creates a new TopKList

func (*TopKList) Add added in v1.3.0

func (tkl *TopKList) Add(value string, timestamp time.Time) *string

Add adds a value to the TopKList and returns the value that was removed, nil if the list wasn't full

Jump to

Keyboard shortcuts

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