util

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileExists

func FileExists(filename string) bool

FileExists checks if a file exists and is not a directory before we try using it to prevent further errors.

func Filter

func Filter(selector *regexp.Regexp, slice []string) (out []string, numRemoved int)

Filter removes all matching elements from a string slice based on a regexp selector

func FilterSplit

func FilterSplit(selector *regexp.Regexp, slice []string) (out, removed []string)

FilterSplit removes all matching elements from a string slice based on a regexp selector and returns all the removed items too

func FilterStringMapByKey

func FilterStringMapByKey(selector *regexp.Regexp, m map[string]string) (filtered map[string]string)

FilterStringMapByKey takes a map where a string is the key and filters it by matching the selector

func FilterStringMapByValue

func FilterStringMapByValue(selector *regexp.Regexp, m map[string]string) (filtered map[string]string)

FilterStringMapByValue takes a map where a string is the value and filters it by matching the selector

func GetKeyByValue

func GetKeyByValue(m map[interface{}]interface{}, target interface{}) (interface{}, error)

GetKeyByValue returns the key of a certain value of a map. It returns the first instance it finds that has the given value. It returns an err if the value is not in the map

func IsFileOrDir

func IsFileOrDir(path string) bool

IsFileOrDir returns whether the passed path is an existing file or dir

func MapContains

func MapContains(m map[interface{}]interface{}, target interface{}, asValue bool) bool

MapContains returns whether a map contains a certain element asValue denotes whether to check for values (if false will search for key)

func MergeStringMaps

func MergeStringMaps(maps ...map[string]string) (out map[string]string, err error)

MergeStringMaps merges the passed maps into a single map, errors on key clashes but still returns the merged map. Only final value of the clashed key remains

func PanicIfErr

func PanicIfErr(err error, customMessage string)

PanicIfErr panics in case of an error. If custom message it creates a new error based on that and returns that instead

func ReadJSONFile

func ReadJSONFile(filepath string, data interface{}) error

ReadJSONFile reads json from a file and stores it in the passed interface which should be a pointer

func ReadYAMLFile

func ReadYAMLFile(filepath string, data interface{}) error

ReadYAMLFile reads yaml from a file and stores it in the passed interface which should be a pointer

func ReturnErrOnPanic added in v0.2.0

func ReturnErrOnPanic(perr *error) func()

ReturnErrOnPanic catches panics, expects them to be of type error, then stores it in the pointer as recovery

func ReturnFirstErr

func ReturnFirstErr(errs ...error) error

ReturnFirstErr returns the first not-nil error from a list of errors. Used to prevent many copies of if err != nil { return err } when they are indepedent

func SelectMatching

func SelectMatching(selector *regexp.Regexp, slice []string) (matching []string)

SelectMatching return all matching elements from a string slice based on a regexp selector

func Verify

func Verify(v Validatable, err error) error

Verify calls IsValid on the parameter and saves the first error in case of an error it returns the error in the passed variable, it ensures no if err != nil repetition in code

func WriteJSONFile

func WriteJSONFile(filepath string, data interface{}) error

WriteJSONFile writes an object as json to the specified file path

func WriteYAMLFile

func WriteYAMLFile(filepath string, data interface{}) error

WriteYAMLFile writes an object as yaml to the specified file path

Types

type Parseable

type Parseable interface {
	ParseFromFile(filepath string) error
}

Parseable is an interface to support parsing from a file

type Validatable

type Validatable interface {
	IsValid() error
}

Validatable is a struct that can be checked for validity

Jump to

Keyboard shortcuts

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