Documentation
¶
Overview ¶
Package utils provides a collection of utility functions, including helpers for making HTTP requests.
Package utils provides a collection of general-purpose utility functions. This file contains utilities for string manipulation, such as wildcard matching.
Package utils provides a collection of utility functions, including helpers for time manipulation.
Index ¶
- func CalculateInterval(from, to Timestamp) (rFrom, rTo Timestamp, interval time.Duration)
- func CreateTimeSeries[T any](name string, from, to Timestamp, interval time.Duration, initValue T) *TimeSeries[T]
- func DockerUtils() *dockerUtils
- func HistogramTimeSeries(name string, from, to Timestamp, interval time.Duration, ...) *TimeSeries[float64]
- func PrintBanner(banner string)
- func RecoverAll(cb func(v any))
- func RecoverAny(errors []error, cb func(v any))
- func RecoverOne(e error, cb func(v any))
- type DockerContainer
- func (c *DockerContainer) AutoRemove(value bool) *DockerContainer
- func (c *DockerContainer) EntryPoint(args ...string) *DockerContainer
- func (c *DockerContainer) Exists() bool
- func (c *DockerContainer) IsRunning() bool
- func (c *DockerContainer) Label(label, value string) *DockerContainer
- func (c *DockerContainer) Labels(labels map[string]string) *DockerContainer
- func (c *DockerContainer) Name(value string) *DockerContainer
- func (c *DockerContainer) Port(external, internal string) *DockerContainer
- func (c *DockerContainer) Ports(ports map[string]string) *DockerContainer
- func (c *DockerContainer) Run() error
- func (c *DockerContainer) Stop() error
- func (c *DockerContainer) Var(key, value string) *DockerContainer
- func (c *DockerContainer) Vars(vars map[string]string) *DockerContainer
- type HashUtilsStruct
- type HttpUtilsStruct
- func (u *HttpUtilsStruct) New(method, url string) *HttpUtilsStruct
- func (u *HttpUtilsStruct) Send() (*http.Response, error)
- func (u *HttpUtilsStruct) WithBody(body string) *HttpUtilsStruct
- func (u *HttpUtilsStruct) WithHeader(key, value string) *HttpUtilsStruct
- func (u *HttpUtilsStruct) WithHeaders(headers map[string]string) *HttpUtilsStruct
- func (u *HttpUtilsStruct) WithTimeout(timeout int) *HttpUtilsStruct
- type JsonUtilsStruct
- type StringUtilsStruct
- type TimeUtilsStruct
- func (t *TimeUtilsStruct) ConvertISO8601Format(format string) string
- func (t *TimeUtilsStruct) DayRange() TimeFrame
- func (t *TimeUtilsStruct) Format(format string) string
- func (t *TimeUtilsStruct) Get() Timestamp
- func (t *TimeUtilsStruct) GetSeries(end Timestamp, interval time.Duration) []Timestamp
- func (t *TimeUtilsStruct) GetSeriesMap(end Timestamp, interval time.Duration) map[Timestamp]int
- func (t *TimeUtilsStruct) GetTimeFrames(end Timestamp, interval time.Duration) []TimeFrame
- func (t *TimeUtilsStruct) GetTimeFramesMap(end Timestamp, interval time.Duration) map[Timestamp]TimeFrame
- func (t *TimeUtilsStruct) LowerBound(duration time.Duration) *TimeUtilsStruct
- func (t *TimeUtilsStruct) MonthRange() TimeFrame
- func (t *TimeUtilsStruct) SetInterval(someFunc func(), milliseconds int, async bool) chan bool
- func (t *TimeUtilsStruct) UpperBound(duration time.Duration) *TimeUtilsStruct
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateInterval ¶ added in v1.2.143
CalculateInterval determines an appropriate time interval for a given time range. The interval is chosen based on the duration of the range (e.g., minutes for an hour, hours for a day).
func CreateTimeSeries ¶ added in v1.2.143
func CreateTimeSeries[T any](name string, from, to Timestamp, interval time.Duration, initValue T) *TimeSeries[T]
CreateTimeSeries creates a new TimeSeries entity with a given name, time range, and interval. The values of the time series are initialized with a provided initial value.
func DockerUtils ¶ added in v1.1.5
func DockerUtils() *dockerUtils
DockerUtils returns a singleton instance of the dockerUtils. This utility uses shell commands to interact with Docker, avoiding direct dependency on the Docker client library.
func HistogramTimeSeries ¶ added in v1.2.143
func HistogramTimeSeries(name string, from, to Timestamp, interval time.Duration, hist map[Timestamp]Tuple[int64, float64]) *TimeSeries[float64]
HistogramTimeSeries converts a histogram (a map of timestamps to values) into a sorted and continuous TimeSeries.
func PrintBanner ¶ added in v1.2.89
func PrintBanner(banner string)
PrintBanner displays the application's banner and lists all configuration variables along with their values in alphabetical order. This function is designed to provide a clear overview of the application's current configuration state at startup or when needed, facilitating easier debugging and configuration verification. It ensures that the configuration variables are presented in a sorted manner for quick reference and readability.
func RecoverAll ¶
func RecoverAll(cb func(v any))
RecoverAll performs recover for all panics.
Sample usage:
defer RecoverAll(func(err any) {
fmt.Printf("got error: %s", err)
})
func RecoverAny ¶
RecoverAny calls the callback function: cb with recovered value in case when recovered value exists in slice errors.
Sample usage:
defer recover.Any([]error{ErrorUsernameBlank, ErrorUsernameAlreadyTaken}, func(err any) {
fmt.Printf("got error: %s", err)
})
func RecoverOne ¶
RecoverOne calls the callback function: cb with recovered value in case when recovered value equals to e otherwise panic won't be recovered and will be propagated.
Sample usage:
defer recover.One(ErrorUsernameBlank, func(err any) {
fmt.Printf("got error: %s", err)
})
Types ¶
type DockerContainer ¶ added in v1.1.5
type DockerContainer struct {
// contains filtered or unexported fields
}
DockerContainer provides a fluent API for configuring and managing a Docker container. It simplifies the process of building and executing `docker` commands.
func (*DockerContainer) AutoRemove ¶ added in v1.1.5
func (c *DockerContainer) AutoRemove(value bool) *DockerContainer
AutoRemove sets whether the container should be automatically removed when it stops.
func (*DockerContainer) EntryPoint ¶ added in v1.1.5
func (c *DockerContainer) EntryPoint(args ...string) *DockerContainer
EntryPoint sets the entrypoint for the container.
func (*DockerContainer) Exists ¶ added in v1.1.5
func (c *DockerContainer) Exists() bool
Exists checks if a container with the configured name exists (either running or stopped).
func (*DockerContainer) IsRunning ¶ added in v1.1.7
func (c *DockerContainer) IsRunning() bool
IsRunning checks if a container with the configured name is currently running.
func (*DockerContainer) Label ¶ added in v1.1.5
func (c *DockerContainer) Label(label, value string) *DockerContainer
Label adds a single label to the container configuration.
func (*DockerContainer) Labels ¶ added in v1.1.5
func (c *DockerContainer) Labels(labels map[string]string) *DockerContainer
Labels adds multiple labels to the container configuration.
func (*DockerContainer) Name ¶ added in v1.1.5
func (c *DockerContainer) Name(value string) *DockerContainer
Name sets the name of the container.
func (*DockerContainer) Port ¶ added in v1.1.5
func (c *DockerContainer) Port(external, internal string) *DockerContainer
Port adds a single port mapping to the container configuration.
func (*DockerContainer) Ports ¶ added in v1.1.5
func (c *DockerContainer) Ports(ports map[string]string) *DockerContainer
Ports adds multiple port mappings to the container configuration.
func (*DockerContainer) Run ¶ added in v1.1.5
func (c *DockerContainer) Run() error
Run executes the `docker run` command based on the configured container settings. If the container is already running, it does nothing.
func (*DockerContainer) Stop ¶ added in v1.1.5
func (c *DockerContainer) Stop() error
Stop stops and removes the container.
func (*DockerContainer) Var ¶ added in v1.1.5
func (c *DockerContainer) Var(key, value string) *DockerContainer
Var adds a single environment variable to the container configuration.
func (*DockerContainer) Vars ¶ added in v1.1.5
func (c *DockerContainer) Vars(vars map[string]string) *DockerContainer
Vars adds multiple environment variables to the container configuration.
type HashUtilsStruct ¶ added in v1.2.169
type HashUtilsStruct struct{}
HashUtilsStruct is a struct that provides hashing utility functions. It is used as a singleton to offer a centralized and efficient way to perform hashing operations.
func HashUtils ¶
func HashUtils() *HashUtilsStruct
HashUtils returns a singleton instance of HashUtilsStruct. This factory method ensures that the HashUtilsStruct is instantiated only once, providing a single point of access to the hashing utilities.
func (*HashUtilsStruct) Hash ¶ added in v1.2.169
func (t *HashUtilsStruct) Hash(s string) uint32
Hash computes a 32-bit FNV-1a hash of a given string. FNV (Fowler-Noll-Vo) is a non-cryptographic hash function known for its speed and low collision rate.
Parameters:
s: The input string to hash.
Returns:
A uint32 representing the FNV-1a hash of the string.
func (*HashUtilsStruct) HashStringToString ¶ added in v1.2.169
func (t *HashUtilsStruct) HashStringToString(s string) string
HashStringToString computes the SHA-1 hash of a string and returns it as a base64-encoded string. SHA-1 is a cryptographic hash function, though it is no longer considered secure for cryptographic purposes. It is still suitable for non-security-related applications like generating unique identifiers.
Parameters:
s: The input string to hash.
Returns:
A string containing the raw, standard base64-encoded SHA-1 hash.
type HttpUtilsStruct ¶ added in v1.2.106
type HttpUtilsStruct struct {
TimeoutSec int
// contains filtered or unexported fields
}
HttpUtilsStruct provides a fluent interface for building and sending HTTP requests. It allows for setting the method, URL, headers, body, and timeout for a request.
func HttpUtils ¶ added in v1.2.106
func HttpUtils() *HttpUtilsStruct
HttpUtils is a factory function that creates a new instance of HttpUtilsStruct. This is the entry point for using the HTTP utility.
func (*HttpUtilsStruct) New ¶ added in v1.2.106
func (u *HttpUtilsStruct) New(method, url string) *HttpUtilsStruct
New initializes a new request with a specified method and URL.
Parameters:
method: The HTTP method (e.g., "GET", "POST", "PUT"). url: The URL for the request.
Returns:
The HttpUtilsStruct instance for chaining.
func (*HttpUtilsStruct) Send ¶ added in v1.2.106
func (u *HttpUtilsStruct) Send() (*http.Response, error)
Send constructs and sends the HTTP request based on the configured parameters. It handles URL parsing, basic authentication from the URL, and header setup.
IMPORTANT: The caller is responsible for closing the response body, e.g., `defer res.Body.Close()`.
Returns:
A pointer to the http.Response and an error if one occurred. An error is also returned for non-successful status codes (i.e., not 2xx or 3xx).
func (*HttpUtilsStruct) WithBody ¶ added in v1.2.106
func (u *HttpUtilsStruct) WithBody(body string) *HttpUtilsStruct
WithBody sets the body of the request.
Parameters:
body: The request body as a string.
Returns:
The HttpUtilsStruct instance for chaining.
func (*HttpUtilsStruct) WithHeader ¶ added in v1.2.106
func (u *HttpUtilsStruct) WithHeader(key, value string) *HttpUtilsStruct
WithHeader adds a single header to the request.
Parameters:
key: The header name. value: The header value.
Returns:
The HttpUtilsStruct instance for chaining.
func (*HttpUtilsStruct) WithHeaders ¶ added in v1.2.106
func (u *HttpUtilsStruct) WithHeaders(headers map[string]string) *HttpUtilsStruct
WithHeaders adds multiple headers to the request from a map.
Parameters:
headers: A map of header names to header values.
Returns:
The HttpUtilsStruct instance for chaining.
func (*HttpUtilsStruct) WithTimeout ¶ added in v1.2.106
func (u *HttpUtilsStruct) WithTimeout(timeout int) *HttpUtilsStruct
WithTimeout sets the timeout in seconds for the request. If not set or set to 0, there is no timeout.
Parameters:
timeout: The timeout duration in seconds.
Returns:
The HttpUtilsStruct instance for chaining.
type JsonUtilsStruct ¶ added in v1.2.169
type JsonUtilsStruct struct {
}
JsonUtilsStruct provides utility functions for converting between structured entities and raw JSON maps. This can be useful for scenarios where you need to work with JSON data in a more dynamic, less-structured way.
func JsonUtils ¶
func JsonUtils() *JsonUtilsStruct
JsonUtils is a factory method that returns a new instance of JsonUtilsStruct.
func (*JsonUtilsStruct) FromJson ¶ added in v1.2.169
func (t *JsonUtilsStruct) FromJson(factory EntityFactory, raw map[string]any) (entity Entity, err error)
FromJson converts a raw JSON map (map[string]any) into a structured entity. This is achieved by first marshalling the map to a JSON byte slice and then unmarshalling it into a new entity created by the provided factory function.
Parameters:
factory: A function that returns a new instance of the target entity (e.g., `func() Entity { return &MyEntity{} }`).
raw: The raw JSON map to convert.
Returns:
The populated entity. An error if marshalling or unmarshalling fails.
func (*JsonUtilsStruct) ToJson ¶ added in v1.2.169
func (t *JsonUtilsStruct) ToJson(entity any) (raw map[string]any, err error)
ToJson converts a given entity (which should be a struct or a pointer to a struct) into a raw JSON map (map[string]any). This is done by first marshalling the entity to a JSON byte slice and then unmarshalling it into the map.
Parameters:
entity: The entity to convert. It should be marshallable to JSON.
Returns:
A map[string]any representing the entity as raw JSON. An error if marshalling or unmarshalling fails.
type StringUtilsStruct ¶ added in v1.2.169
type StringUtilsStruct struct {
}
StringUtilsStruct provides a set of utility functions for working with strings. It is used as a singleton to offer a centralized and efficient way to perform string operations.
func StringUtils ¶ added in v1.2.74
func StringUtils() *StringUtilsStruct
StringUtils is a factory method that returns a singleton instance of StringUtilsStruct. This provides a single point of access to the string utilities.
func (*StringUtilsStruct) WildCardMatch ¶ added in v1.2.169
func (t *StringUtilsStruct) WildCardMatch(source string, pattern string) bool
WildCardMatch checks if a given source string matches a wildcard pattern. The pattern can contain '*' to match any sequence of characters.
Parameters:
source: The string to test. pattern: The wildcard pattern to match against.
Returns:
`true` if the source string matches the pattern, `false` otherwise.
func (*StringUtilsStruct) WildCardToRegexp ¶ added in v1.2.169
func (t *StringUtilsStruct) WildCardToRegexp(pattern string) string
WildCardToRegexp converts a wildcard pattern string into a regular expression string. This implementation supports the '*' wildcard, which matches any sequence of characters. Other characters are treated as literals. The resulting regex is anchored to match the entire string.
For example:
"data*.log" -> "^data.*\.log$" "report.*" -> "^report\..*$"
Parameters:
pattern: The wildcard string to convert.
Returns:
A string containing the equivalent regular expression.
type TimeUtilsStruct ¶ added in v1.2.143
type TimeUtilsStruct struct {
SECOND uint64
MINUTE uint64
HOUR uint64
DAY uint64
// contains filtered or unexported fields
}
TimeUtilsStruct provides a fluent interface for time-related operations. It encapsulates a base time and offers methods for formatting, manipulation, and time series generation.
func TimeUtils ¶ added in v1.2.26
func TimeUtils(ts Timestamp) *TimeUtilsStruct
TimeUtils is a factory function that creates a new TimeUtilsStruct with a given base time.
Parameters:
ts: The base timestamp for time operations.
Returns:
A new TimeUtilsStruct instance.
func (*TimeUtilsStruct) ConvertISO8601Format ¶ added in v1.2.143
func (t *TimeUtilsStruct) ConvertISO8601Format(format string) string
ConvertISO8601Format converts a date-time format string from ISO 8601 style to Go's reference time format. This allows for more intuitive format strings, such as "YYYY-MM-DD" instead of "2006-01-02".
Parameters:
format: The ISO 8601-style format string.
Returns:
The equivalent Go reference time format string.
func (*TimeUtilsStruct) DayRange ¶ added in v1.2.143
func (t *TimeUtilsStruct) DayRange() TimeFrame
DayRange returns a TimeFrame representing the start and end of the day for the base time.
func (*TimeUtilsStruct) Format ¶ added in v1.2.143
func (t *TimeUtilsStruct) Format(format string) string
Format converts the base timestamp to a formatted string. If no format is provided, it defaults to RFC3339.
Parameters:
format: The desired output format, e.g., "YYYY-MM-DD hh:mm:ss".
Returns:
The formatted time string.
func (*TimeUtilsStruct) Get ¶ added in v1.2.143
func (t *TimeUtilsStruct) Get() Timestamp
Get returns the current base timestamp of the TimeUtilsStruct.
func (*TimeUtilsStruct) GetSeries ¶ added in v1.2.143
func (t *TimeUtilsStruct) GetSeries(end Timestamp, interval time.Duration) []Timestamp
GetSeries creates a slice of timestamps from the base time to a specified end time, at a given interval.
Parameters:
end: The end time of the series. interval: The duration between each timestamp in the series.
Returns:
A slice of timestamps.
func (*TimeUtilsStruct) GetSeriesMap ¶ added in v1.2.143
func (t *TimeUtilsStruct) GetSeriesMap(end Timestamp, interval time.Duration) map[Timestamp]int
GetSeriesMap creates a map of timestamps from the base time to a specified end time, at a given interval. The map values are initialized to zero.
Parameters:
end: The end time of the series. interval: The duration between each timestamp.
Returns:
A map where keys are timestamps and values are 0.
func (*TimeUtilsStruct) GetTimeFrames ¶ added in v1.2.143
func (t *TimeUtilsStruct) GetTimeFrames(end Timestamp, interval time.Duration) []TimeFrame
GetTimeFrames creates a slice of time frames from the base time to a specified end time, at a given interval.
Parameters:
end: The end time of the series. interval: The duration of each time frame.
Returns:
A slice of TimeFrame structs.
func (*TimeUtilsStruct) GetTimeFramesMap ¶ added in v1.2.143
func (t *TimeUtilsStruct) GetTimeFramesMap(end Timestamp, interval time.Duration) map[Timestamp]TimeFrame
GetTimeFramesMap creates a map of time frames from the base time to a specified end time, at a given interval.
Parameters:
end: The end time of the series. interval: The duration of each time frame.
Returns:
A map where keys are the start of the time frame and values are the TimeFrame structs.
func (*TimeUtilsStruct) LowerBound ¶ added in v1.2.143
func (t *TimeUtilsStruct) LowerBound(duration time.Duration) *TimeUtilsStruct
LowerBound rounds the base time down to the nearest specified duration.
Parameters:
duration: The duration to round down to (e.g., time.Minute, time.Hour).
Returns:
The TimeUtilsStruct instance for chaining.
func (*TimeUtilsStruct) MonthRange ¶ added in v1.2.143
func (t *TimeUtilsStruct) MonthRange() TimeFrame
MonthRange returns a TimeFrame representing the start and end of the month for the base time.
func (*TimeUtilsStruct) SetInterval ¶ added in v1.2.143
func (t *TimeUtilsStruct) SetInterval(someFunc func(), milliseconds int, async bool) chan bool
SetInterval executes a function at a specified interval. It can run the function synchronously or asynchronously.
Parameters:
someFunc: The function to execute. milliseconds: The interval in milliseconds. async: If true, the function is executed in a new goroutine.
Returns:
A channel that can be used to stop the interval by sending a boolean value.
func (*TimeUtilsStruct) UpperBound ¶ added in v1.2.143
func (t *TimeUtilsStruct) UpperBound(duration time.Duration) *TimeUtilsStruct
UpperBound rounds the base time up to the nearest specified duration.
Parameters:
duration: The duration to round up to (e.g., time.Minute, time.Hour).
Returns:
The TimeUtilsStruct instance for chaining.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cache Cache item implementation Based on https://github.com/ReneKroon/ttlcache
|
Package cache Cache item implementation Based on https://github.com/ReneKroon/ttlcache |
|
Package cache2 Cache item old implementation Based on https://github.com/ReneKroon/ttlcache
|
Package cache2 Cache item old implementation Based on https://github.com/ReneKroon/ttlcache |
|
Package collections provides a thread-safe implementation of a FIFO (First-In, First-Out) queue data structure.
|
Package collections provides a thread-safe implementation of a FIFO (First-In, First-Out) queue data structure. |