utils

package module
v0.0.0-...-0808b5a Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 24 Imported by: 11

README

Utils

This package / repository contains small utilities that I have written and found useful for myself.

  • OnlyAsciiText(string) string - returns only printable ASCII characters without spaces.
  • ReadLines(string,bool) []string,error - reads the content of the file into a string array, each line as its own item. Optionally skips blank lines.
  • ReadJsonFile - reads a JSON config from file
  • ReadConfig - utility to help you in loading configuration files
  • IsStringInString - helper to check if a string is in a slice/array
  • Searcher - interface to search if something is present in something else
  • DLock - a lightweight distributed lock that can be used either locally or via REST API
  • Broadcast - get notified when a variable/object changes
  • Cache - maintains a cached copy of an object that will be refreshed on access
  • FS - an interface to access object storage of some kind
  • Errors - a place to store errors if you don't want or have to check them until the end

Documentation

Index

Constants

View Source
const (
	TagDefault = "default" // default tag name when parsing tags in struct
	EnvDefault = "env"     // default tag for environment variables
)
View Source
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
)
View Source
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

func ConfigParserWithDefaults[T any](file ...string) (T, []error)

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

func DownloadLocalFile[T any](fn string) (result T, err error)

DownloadLocalFile reads a file on the local file system and parses it into T

func DownloadUrl

func DownloadUrl[T any](link string) (result T, err error)

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 Goid

func Goid() int

Goid returns the current goroutine ID

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 IsIntInInts(value int, list *[]int) bool

IsIntInInts checks if value is in the list and returns true if so.

Deprecated: use IsValueIn instead

func IsStringInStrings deprecated

func IsStringInStrings(value string, list *[]string) bool

IsStringInStrings checks if value is in the list and returns true if so.

Deprecated: use IsValueIn instead

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

func LoadConfigDefaults(data interface{}, tag string) error

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

func OnlyAsciiText(input string) (output string)

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

func ParseJsonFromSlice[T any](data []byte) (result T, err error)

ParseJsonFromSlice parses JSON passed as a byte slice

func ReadConfig

func ReadConfig(fn []string, env string, data interface{}) bool

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

func ReadJsonFile(fn string, data interface{}) error

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

func ReadLines(path string, skipBlank bool) ([]string, error)

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

func SetHttpContentType(w *http.Response, content string)

SetHttpContentType sets content type of response to content

func SetHttpContentTypeJson

func SetHttpContentTypeJson(w *http.Response)

SetHttpContentTypeJson sets the content type to be JSON

func ToHex

func ToHex(input []byte) string

ToHex converts input stream to printable hex value

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

func (b *Broadcast[T]) GetChannelWithEpoch(epoch uint) (res chan struct{})

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

func (b *Broadcast[T]) GetValueWithEpoch() (T, uint)

GetValueWithEpoch returns the current value

func (*Broadcast[T]) SetValue

func (b *Broadcast[T]) SetValue(newVal T)

SetValue sets a new value and informs listeners

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

Cache represents an object that can be cached

func NewCache

func NewCache[T any](expire time.Duration, initialValue T, callback func() (T, bool)) *Cache[T]

NewCache creates a new cached object

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

func (c *Cache[T]) GetExpiration() time.Time

GetExpiration returns when the cached object expires

func (*Cache[T]) GetValue

func (c *Cache[T]) GetValue() T

GetValue returns the last value, and if the object has expired will try to retrieve a new object. If used without an initial value, this call will block until the first set of data is returned.

type ConfigParserOption

type ConfigParserOption[T any] func(T) (T, error)

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 {
}

func (DiscardSlog) Enabled

func (d DiscardSlog) Enabled(_ context.Context, _ slog.Level) bool

func (DiscardSlog) Handle

func (d DiscardSlog) Handle(_ context.Context, _ slog.Record) error

func (DiscardSlog) WithAttrs

func (d DiscardSlog) WithAttrs(_ []slog.Attr) slog.Handler

func (DiscardSlog) WithGroup

func (d DiscardSlog) WithGroup(_ string) slog.Handler

type Errors

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

Errors let you store many errors, and return them together.

func (*Errors) Add

func (e *Errors) Add(errs ...error)

Add lets you add one or more errors into the list

func (*Errors) As

func (e *Errors) As(target any) bool

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

func (e *Errors) ErrOrNil() error

ErrOrNil returns either nil if there are no errors, or the error. If it is only one error then the actual error is returned.

func (*Errors) Error

func (e *Errors) Error() string

Error returns the current error as a string.

func (*Errors) Is

func (e *Errors) Is(target error) bool

Is reports whether any error in err's tree matches target.

func (*Errors) Len

func (e *Errors) Len() int

Len returns the number of errors stored

func (*Errors) Reset

func (e *Errors) Reset()

Reset removes all errors from the list

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

func NewLocalFS(basePath string) FS

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

func GetChuckNorrisJoke() (joke Joke, err error)

GetChuckNorrisJoke returns a new joke or an error on error

func GetLocalJoke

func GetLocalJoke() Joke

GetLocalJoke returns a joke from the local store

func GetRandomJoke

func GetRandomJoke() (j Joke)

GetRandomJoke returns a random joke from any provider

func JokeCallback

func JokeCallback() (Joke, bool)

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.

func (*Memo[V, T]) Get

func (m *Memo[V, T]) Get(val T) V

Get returns V if T is the same as before, otherwise it will call your code to get a new value.

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) AddModule

func (c *Modules) AddModule(m ...Module)

AddModule adds a new module to the list of modules

func (Modules) Len

func (T Modules) Len() int

func (Modules) Less

func (T Modules) Less(i, j int) bool

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

func (c *Modules) StartModules(module []string)

StartModules starts all configured modules. As this list is ordered, the modules are started in the order defined by the list.

func (Modules) Swap

func (T Modules) Swap(i, j int)

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

func GetSv443Joke(ctx context.Context) (Sv443Joke, error)

GetSv443Joke returns a random joke from SV443

func (Sv443Joke) ToJoke

func (s Sv443Joke) ToJoke() Joke

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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