Documentation ¶
Index ¶
- Constants
- func ConfigParser[T any](parsers ...ConfigParserOption[T]) (T, []error)
- func ConfigParserWithDefaults[T any](file ...string) (T, []error)
- func ContentTypeJson(w http.ResponseWriter)
- func CtrlCchannel() chan struct{}
- func Difference[T comparable](slice1 []T, slice2 []T) []T
- func DownloadLocalFile[T any](fn string) (result T, err error)
- func DownloadUrl[T any](link string) (result T, err error)
- func DownloadUrlWithContext[T any](ctx context.Context, link string, headers map[string]string) (result T, err error)
- func Goid() int
- func HighestValue[T Ordered](input []T) (result T)
- func HttpRequest[T any](ctx context.Context, headers map[string]string, method, url string, ...) (result T, httpresponse *http.Response, err error)
- func IsIntInInts(value int, list *[]int) booldeprecated
- func IsStringInStrings(value string, list *[]string) booldeprecated
- func IsValueIn[T comparable](value T, list *[]T) bool
- func LoadConfigDefaults(data interface{}, tag string) error
- func LowestValue[T Ordered](input []T) (result T)
- func MapDeepCopy[T comparable, V any](source map[T]V) (result map[T]V)
- func OnlyAsciiText(input string) (output string)
- func OnlyInFirstList[T comparable](slice1 []T, slice2 []T) []T
- func ParseJsonFromSlice[T any](data []byte) (result T, err error)
- func ReadConfig(fn []string, env string, data interface{}) bool
- func ReadJsonFile(fn string, data interface{}) error
- func ReadLines(path string, skipBlank bool) ([]string, error)
- func RemoveDuplicate[T comparable](tSlice []T) []T
- func SetHttpContentType(w *http.Response, content string)
- func SetHttpContentTypeJson(w *http.Response)
- func ToHex(input []byte) string
- type Broadcast
- type Cache
- type ConfigParserOption
- type DiscardSlog
- type Errors
- type FS
- type Joke
- type Memo
- type Module
- type Modules
- type Ordered
- type Searcher
- type StringArray
- type Sv443Joke
Constants ¶
const ( TagDefault = "default" // default tag name when parsing tags in struct EnvDefault = "env" // default tag for environment variables )
const ( HttpHeaderAuthorization = "Authorization" // HttpHeaderAuthorization is the header that is used to authorize a client. HttpHeaderHost = "Host" // HttpHeaderHost is the host header. Remember that net/http sets this using a separate call. HttpHeaderContentType = "Content-Type" // HttpHeaderContentType is the header for content types HttpContentJson = "application/json" // HttpContentJson is the content type for JSON HttpBearer = "Bearer" // bearer authorization )
const ( JokeProviderChuckNorris = 0 JokeProviderJokeAPI = 1 JokeProviderLocal = 2 // should be last element )
Variables ¶
This section is empty.
Functions ¶
func ConfigParser ¶
func ConfigParser[T any](parsers ...ConfigParserOption[T]) (T, []error)
ConfigParser parses T and returns a new configuration object based on the various options passed in.
func ConfigParserWithDefaults ¶
ConfigParserWithDefaults allows you to specify one or more files that are read in order, after defaults and environment. If no file is specified then "config.json" is used as the config file.
func ContentTypeJson ¶
func ContentTypeJson(w http.ResponseWriter)
ContentTypeJson sets the content type to be JSON
func CtrlCchannel ¶
func CtrlCchannel() chan struct{}
CtrlCchannel returns a channel that is closed when pressing CTRL+C
func Difference ¶
func Difference[T comparable](slice1 []T, slice2 []T) []T
Difference compares two slices and returns a list of all items that are not in both lists
func DownloadLocalFile ¶
DownloadLocalFile reads a file on the local file system and parses it into T
func DownloadUrl ¶
DownloadUrl will download an url, parse it and return it in a struct. Deprecated: use DownloadUrlWithContext instead. This uses context.Background() as its context.
func DownloadUrlWithContext ¶
func DownloadUrlWithContext[T any](ctx context.Context, link string, headers map[string]string) (result T, err error)
DownloadUrlWithContext will download an url, parse it and return it in a struct.
func HighestValue ¶
func HighestValue[T Ordered](input []T) (result T)
HighestValue returns the highest value in the list
func HttpRequest ¶
func HttpRequest[T any](ctx context.Context, headers map[string]string, method, url string, body io.Reader) (result T, httpresponse *http.Response, err error)
HttpRequest is a generic HTTP call function that returns a JSON struct back to the application. The application should check both err and httpresponse.StatusCode to determine if the call was successful.
func IsIntInInts
deprecated
func IsStringInStrings
deprecated
func IsValueIn ¶
func IsValueIn[T comparable](value T, list *[]T) bool
IsValueIn is a generic call to check if the value T is in an array of that type.
func LoadConfigDefaults ¶
LoadConfigDefaults puts defaults values into a struct from tags. Bool, int and floats are supported, loading is aborted on first error
func LowestValue ¶
func LowestValue[T Ordered](input []T) (result T)
LowestValue returns the lowest value in the list
func MapDeepCopy ¶
func MapDeepCopy[T comparable, V any](source map[T]V) (result map[T]V)
MapDeepCopy copies the content of one map into another map
func OnlyAsciiText ¶
OnlyAsciiText removes any non-ASCII (ASCII is: a-z, A-Z, 0-9) from input. If the input text is only special characters then an empty string is returned.
func OnlyInFirstList ¶
func OnlyInFirstList[T comparable](slice1 []T, slice2 []T) []T
OnlyInFirstList returns T where T is only in slice1
func ParseJsonFromSlice ¶
ParseJsonFromSlice parses JSON passed as a byte slice
func ReadConfig ¶
ReadConfig tries to load configuration from disk. Lookup is done from first to last file specified and then from the environment variable. It returns true if config is loaded. Any error is hidden from this call. Default values are also loaded.
func ReadJsonFile ¶
ReadJsonFile reads the content of a JSON file and parses it into data. Unless you have an error data is populated with data from the file specified. Deprecated: use DownloadLocalFile instead
func ReadLines ¶
readLines reads a whole file into memory and returns a slice of its lines. source: https://stackoverflow.com/questions/5884154/read-text-file-into-string-array-and-write
func RemoveDuplicate ¶
func RemoveDuplicate[T comparable](tSlice []T) []T
RemoveDuplicate removes duplicates from a slice.
func SetHttpContentType ¶
SetHttpContentType sets content type of response to content
func SetHttpContentTypeJson ¶
SetHttpContentTypeJson sets the content type to be JSON
Types ¶
type Broadcast ¶
type Broadcast[T any] struct { // contains filtered or unexported fields }
Broadcast is a tool to let you notify many receivers on a change of a value using signals. This is implemented using generics, so you can pass your own type in a type-safe manner. Note that if you have many / rapid updates to the value, the readers can get an outdated value or miss an update.
func (*Broadcast[T]) GetChannel ¶
func (b *Broadcast[T]) GetChannel() chan struct{}
GetChannel returns the current channel that notify on changes
func (*Broadcast[T]) GetChannelWithEpoch ¶
GetChannelWithEpoch returns the current channel that notify on changes. In case it is not the current epoch that is passed in, a closed channel will be returned. This way you can detect updates between each wait when also using GetValueWithEpoch.
func (*Broadcast[T]) GetValue ¶
func (b *Broadcast[T]) GetValue() T
GetValue returns the current value
func (*Broadcast[T]) GetValueWithEpoch ¶
GetValueWithEpoch returns the current value
type Cache ¶
type Cache[T any] struct { // contains filtered or unexported fields }
Cache represents an object that can be cached
func NewCacheWithoutInitialValue ¶
func NewCacheWithoutInitialValue[T any](expire time.Duration, defaultValue T, callback func() (T, bool)) *Cache[T]
NewCacheWithoutInitialValue creates a new cached object where there is no initial value, and GetValue blocks until some data is available. In the case when the first fetch fails, a default value will be passed back instead.
func (*Cache[T]) Expire ¶
func (c *Cache[T]) Expire()
Expire expires the object and also initiates retrieving new data
func (*Cache[T]) GetExpiration ¶
GetExpiration returns when the cached object expires
type ConfigParserOption ¶
ConfigParserOption is a method that can be called that tries to get some values into T in some way.
func ParseConfigDefaults ¶
func ParseConfigDefaults[T any]() ConfigParserOption[T]
ParseConfigDefaults set default values defined in a struct, should be first config option passed.
func ParseConfigEnvironment ¶
func ParseConfigEnvironment[T any]() ConfigParserOption[T]
ParseConfigEnvironment get values from the environment variables and put into T.
func ParseConfigJsonFile ¶
func ParseConfigJsonFile[T any](fn string) ConfigParserOption[T]
ParseConfigJsonFile reads a configuration file from disk, and merges it into the current configuration.
func ParseConfigJsonHttp ¶
func ParseConfigJsonHttp[T any](url string, headers map[string]string) ConfigParserOption[T]
ParseConfigJsonHttp get values from HTTP
type DiscardSlog ¶
type DiscardSlog struct { }
type Errors ¶
type Errors struct {
// contains filtered or unexported fields
}
Errors let you store many errors, and return them together.
func (*Errors) As ¶
As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.
func (*Errors) ErrOrNil ¶
ErrOrNil returns either nil if there are no errors, or the error. If it is only one error then the actual error is returned.
type FS ¶
type FS interface { Put(ctx context.Context, objectName string, metadata map[string]string, r io.Reader) error // put an object to storage, caller has to close the Reader themselves Get(ctx context.Context, objectName string) (io.ReadCloser, map[string]string, error) // get an object from storage Head(ctx context.Context, objectName string) (map[string]string, error) // get metadata from storage Delete(ctx context.Context, objectName string) error // remove an object from storage }
FS is an interface to access files in object storage. os.ErrNotExist is given when an object does not exist (checked with errors.Is().)
func NewLocalFS ¶
NewLocalFS returns a FS that uses a directory on the local filesystem. This file system is for proof-of-concept only, as it is expected to use a real object storage as the backend. localFS can be escaped using relative paths (../../../etc)
type Joke ¶
type Joke struct { Icon string `json:"icon_url"` ID string `json:"id"` URL string `json:"url"` Value string `json:"value"` SourceProvider int `json:"sourceProvider"` // internal value, indicating what provider it os from }
Joke is a joke as returned from the API https://api.chucknorris.io/
func GetChuckNorrisJoke ¶
GetChuckNorrisJoke returns a new joke or an error on error
func GetRandomJoke ¶
func GetRandomJoke() (j Joke)
GetRandomJoke returns a random joke from any provider
func JokeCallback ¶
JokeCallback is intended to be used as a callback on Cache, and returns a new joke.
type Memo ¶
type Memo[V any, T comparable] struct { // contains filtered or unexported fields }
Memo lets you cache any result as long as you send in the same T every time.
func NewMemo ¶
func NewMemo[V any, T comparable](callback func(T) V, initialValue T) Memo[V, T]
NewMemo returns a new Memo object with a computed result for T.
type Module ¶
type Module struct { Name string // name of module Runner func() // long-running task that is started in its own goroutine Description string // a brief help for the command line Hidden bool // true if this module should not be printed on command line and you need to know about it to launch it IncludeInAll bool // if true, this module is launched with "all" Order int // startup order, defaults to 0, negative go last, positive go first StartupDelay time.Duration // if positive, will case a delay after starting up this module }
Module is a module in a list of Modules
type Modules ¶
type Modules struct {
// contains filtered or unexported fields
}
Modules are the container for modules that you launch your work from.
func (*Modules) PrintHelp ¶
func (m *Modules) PrintHelp()
PrintHelp prints flag.PrintDefaults() and a list of all non-hidden modules in no particular order
func (*Modules) StartAllModules ¶
func (c *Modules) StartAllModules()
StartAllModules will make a list of modules to start ordered by Order and start them
func (*Modules) StartModules ¶
StartModules starts all configured modules. As this list is ordered, the modules are started in the order defined by the list.
type Ordered ¶
type Ordered interface { int | int8 | int16 | int32 | int64 | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64 }
Ordered is a types that their values can be compared with < and >
type Searcher ¶
type Searcher interface {
IsInList(interface{}) bool // search for a match, returns true if found
}
Searcher is an interface that defines how to search for a match of data. All Searches has to be thread safe. The input can be any input supported by the implementation and the result is a bool to confirm
type StringArray ¶
type StringArray []string // represents an array of strings
func (*StringArray) IsInList ¶
func (s *StringArray) IsInList(input interface{}) bool
IsInList implements Searcher on StringArray and returns true if input is in StringArray
type Sv443Joke ¶
type Sv443Joke struct { Error bool `json:"error"` // should be false InternalError bool `json:"internalError,omitempty"` // when Error==true Code int `json:"code,omitempty"` // when Error==true Message string `json:"message,omitempty"` // when Error==true CausedBy []string `json:"causedBy,omitempty"` // when Error==true AdditionalInfo string `json:"additionalInfo,omitempty"` // when Error==true Category string `json:"category"` Type string `json:"type"` // [ single, twopart ] Setup string `json:"setup,omitempty"` // when Type == twopart Delivery string `json:"delivery,omitempty"` // when Type == twopart Joke string `json:"joke"` // when Type == single Flags struct { SafeForWork bool `json:"nsfw"` Religious bool `json:"religious"` Political bool `json:"political"` Racist bool `json:"racist"` Sexist bool `json:"sexist"` Explicit bool `json:"explicit"` } `json:"flags"` ID int `json:"id"` Safe bool `json:"safe"` Lang string `json:"lang"` }
Sv443Joke is the JSON from https://sv443.net/jokeapi/v2/
func GetSv443Joke ¶
GetSv443Joke returns a random joke from SV443