utils

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetBoolEnvOrDefault

func GetBoolEnvOrDefault(env string, defaultValue bool) bool

GetBoolEnvOrDefault retrieves a boolean environment variable value or returns a default. It converts the environment variable string value to a boolean, considering "true" (case-insensitive) as true and all other values as false.

Parameters:

  • env: The name of the environment variable to retrieve
  • defaultValue: The default boolean value to return if the environment variable is not set or empty

Returns:

  • The boolean value of the environment variable if set and not empty
  • The defaultValue if the environment variable is not set or empty

func GetEnvOrDefault

func GetEnvOrDefault(env string, defaultValue string) string

GetEnvOrDefault retrieves the value of an environment variable. If the environment variable is not set or is empty, it returns the provided default value.

Parameters:

  • env: The name of the environment variable to retrieve.
  • defaultValue: The default value to return if the environment variable is not set or is empty.

Returns:

  • The value of the environment variable if it is set and not empty.
  • The defaultValue if the environment variable is not set or is empty.

Example Usage:

// Load the "API_KEY" environment variable, defaulting to "defaultKey123" if not set.
apiKey := GetEnvOrDefault("API_KEY", "defaultKey123")
fmt.Println("API Key:", apiKey)

func GetRequiredBoolEnv

func GetRequiredBoolEnv(key string) bool

GetRequiredBoolEnv retrieves a required boolean environment variable value. If the environment variable is not set, empty, or cannot be converted to a boolean, it logs a fatal error and exits the program.

Parameters:

  • key: The name of the required boolean environment variable

Returns:

  • The boolean value of the environment variable

Exits with log.Fatal if the environment variable is invalid

func GetRequiredEnv

func GetRequiredEnv(key string) string

GetRequiredEnv retrieves a required environment variable value. If the environment variable is not set or empty, it logs a fatal error and exits the program.

Parameters:

  • key: The name of the required environment variable

Returns:

  • The value of the environment variable if set and not empty

Exits with log.Fatal if the environment variable is not set or empty

func GetRequiredFloat64Env

func GetRequiredFloat64Env(key string) float64

GetRequiredFloat64Env retrieves a required 64-bit floating-point environment variable value. If the environment variable is not set, empty, or cannot be converted to a float64, it logs a fatal error and exits the program.

Parameters:

  • key: The name of the required float64 environment variable

Returns:

  • The float64 value of the environment variable

Exits with log.Fatal if the environment variable is invalid

func GetRequiredInt64Env

func GetRequiredInt64Env(key string) int64

GetRequiredInt64Env retrieves a required 64-bit integer environment variable value. If the environment variable is not set, empty, or cannot be converted to an int64, it logs a fatal error and exits the program.

Parameters:

  • key: The name of the required int64 environment variable

Returns:

  • The int64 value of the environment variable

Exits with log.Fatal if the environment variable is invalid

func GetRequiredIntEnv

func GetRequiredIntEnv(key string) int

GetRequiredIntEnv retrieves a required integer environment variable value. If the environment variable is not set, empty, or cannot be converted to an integer, it logs a fatal error and exits the program.

Parameters:

  • key: The name of the required integer environment variable

Returns:

  • The integer value of the environment variable

Exits with log.Fatal if the environment variable is invalid

func StructValidation

func StructValidation(target interface{}, setting ...ValidationSetting) error

func ToIntOrElse

func ToIntOrElse(value *string, defaultValue int) *int

func ToIntOrElseNil

func ToIntOrElseNil(value *string) *int

func UserHomePathFix

func UserHomePathFix(path string) (string, error)

UserHomePathFix resolves a path that starts with '~' to the user's home directory. If the home directory cannot be determined, it logs an error and exits the application. Parameters:

  • path: The file path string, potentially starting with '~'.

Returns:

  • The resolved file path with '~' replaced by the user's home directory, or the original path if no substitution is needed.

Types

type RTranslation

type RTranslation struct {
	Tag        string
	Message    string
	Validation func(validator.FieldError) []string
}

func DefaultTranslations

func DefaultTranslations() []RTranslation

type RValidation

type RValidation struct {
	Tag       string
	Validator func(fl validator.FieldLevel) bool
}

func DefaultPasswordValidations

func DefaultPasswordValidations() RValidation

type ValidationSetting

type ValidationSetting struct {
	Language      string
	SpecSeparator string
	Translations  []RTranslation
	Validators    []RValidation
}

func Default

func Default() *ValidationSetting

Jump to

Keyboard shortcuts

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