utils

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateGitlabTTL

func CalculateGitlabTTL(duration time.Duration, start time.Time) (ttl time.Duration, exp time.Time, err error)

CalculateGitlabTTL calculates the Time-To-Live (TTL) and expiration time for a GitLab-related operation based on a specified duration and start time.

The function ensures that the calculated expiration does not exceed one year from the start time. It computes the expiration to be at the midnight following the calculated expiration date.

func ConvertToInt

func ConvertToInt(num any) (int, error)

ConvertToInt attempts to convert various numeric types to an int.

This function handles conversions from several numeric types (including int, int8, int16, int32, int64, float32, and float64) to a standard int. It utilizes type assertion to check the underlying type of the input. If the input is not a supported numeric type, it returns an error.

func HttpClientFromContext

func HttpClientFromContext(ctx context.Context) (*http.Client, bool)

HttpClientFromContext extracts the http.Client from a given context.

This function retrieves an HTTP client that was previously embedded in the context. If the context does not contain an HTTP client or it cannot be asserted as an *http.Client, the function returns nil and false.

func HttpClientNewContext

func HttpClientNewContext(ctx context.Context, httpClient *http.Client) context.Context

HttpClientNewContext returns a new context.Context that carries the provided http.Client.

This function embeds a given HTTP client into the provided context, allowing it to be passed through the application and retrieved later. This is useful for managing HTTP client configurations and dependency injection across different parts of an application that require HTTP clients.

func TimeFromContext

func TimeFromContext(ctx context.Context) time.Time

TimeFromContext extracts a time.Time from the given context.

This function retrieves a time value that was previously embedded in the context. If the context does not contain such a time value, it defaults to returning time.Now(), effectively providing the current system time.

func ToAny

func ToAny[T int | string](values ...T) (ret []any)

ToAny converts a slice of values of type int or string to a slice of empty interfaces (any).

This function is a generic utility that allows for the conversion of a variadic list of integers or strings into a slice of empty interfaces (`[]any`). This is particularly useful when you need to pass mixed-type values around, or when API requirements dictate an `any` type.

func TokenName

func TokenName(role TokenNameData) (name string, err error)

TokenName generates a token name by executing the template defined in TokenNameData.

This function retrieves the template string from the TokenNameData, parses it, and then executes it while substituting placeholders with the logical response data provided by the token role. An additional "unix_timestamp_utc" field is added to the data map, representing the current UTC Unix timestamp.

func ValidateTokenNameName

func ValidateTokenNameName(role TokenNameData) (err error)

ValidateTokenNameName validates the template syntax of a token name.

This function checks if the provided TokenNameData instance is non-nil and executes basic validation of the token name's syntax by parsing it as a template. This helps ensure the token name format adheres to expected patterns and contains no syntax errors.

func WithStaticTime

func WithStaticTime(ctx context.Context, t time.Time) context.Context

WithStaticTime returns a new context.Context that carries a specific static time.

This function embeds a given time.Time value into the provided context, allowing parts of an application to operate with a fixed notion of the current time. This can be particularly useful in testing scenarios where you need to control or simulate time progression.

Types

type TokenNameData

type TokenNameData interface {
	// GetName returns the token's name as a string
	GetName() string
	// LogicalResponseData returns a map containing relevant data that can be used in template operations or logical evaluations
	LogicalResponseData() map[string]any
	// IsNil returns a boolean indicating whether the instance is considered nil or invalid
	IsNil() bool
}

TokenNameData defines an interface for objects that contain a token name and methods for obtaining data relevant to token-based operations.

This interface provides a contract for structures that need to offer token name data and conversion capabilities. It is used to ensure consistent handling of token names and associated logic.

Jump to

Keyboard shortcuts

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