utils

package
v0.0.0-...-9acd316 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: Apache-2.0 Imports: 22 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValidatorNilValue = errors.New("validator cannot validate nil value")
)

Functions

func CleanPath

func CleanPath(path string) string

func EnvBool

func EnvBool(name string, def bool) bool

EnvBool looks up environment variable by name and converts the string value to bool. It returns default value if env does not exist or conversion to bool fails

func FindAnyWith

func FindAnyWith[T any](items []T, matchFn func(item *T) bool) *T

func FindInSlice

func FindInSlice[T comparable](items []T, x T) int

FindInSlice finds an item in a slice and returns the index. It will return -1 if not found

func FromPbJson deprecated

func FromPbJson[T proto.Message](reader io.Reader, obj T) error

Deprecated: Use `api/pb` package instead

func FromPbToYaml deprecated

func FromPbToYaml[T proto.Message](writer io.Writer, obj T) error

Deprecated: Use `api/pb` package instead

func FromYamlToPb deprecated

func FromYamlToPb[T proto.Message](reader io.Reader, obj T) error

Deprecated: Use `api/pb` package instead

func Int64

func Int64(max int64) int64

Int64 returns a cryptographically secure 64 bit random number

func Introspect

func Introspect(v interface{}) string

Serialize an interface using JSON or return error string

func IsEmptyString

func IsEmptyString(s string) bool

func MapStruct

func MapStruct[T any](source interface{}, dest *T) error

func NewUniqueId

func NewUniqueId() string

func ParseEnvToStruct

func ParseEnvToStruct[T any]() (T, error)

ParseEnvToStruct parses environment variables into a struct using the caarlos0/env package. The struct type T should have appropriate `env` and `envDefault` tags to define the mapping.

Example usage:

type Config struct {
    Port int `env:"PORT" envDefault:"8080"`
    Host string `env:"HOST,required"`
}

config, err := ParseEnvToStruct[Config]()
if err != nil {
    log.Fatal(err)
}

See https://github.com/caarlos0/env for full documentation on supported tags and features.

func ParseEnvToStructWithOptions

func ParseEnvToStructWithOptions[T any](opts env.Options) (T, error)

ParseEnvToStructWithOptions parses environment variables into a struct with custom options. This allows setting a prefix, using custom parsers, etc.

Example usage:

config, err := ParseEnvToStructWithOptions[Config](env.Options{
    Prefix: "APP_",
})

func PtrTo

func PtrTo[T any](v T) *T

func SafelyGetValue

func SafelyGetValue[T any](target *T) T

func StringStripQuotes

func StringStripQuotes(s string) string

StringStripQuotes removes surrounding quote characters from a string From string it removes outer most quote pair - "abc" -> abc - 'abc' -> abc - \'abc\' -> abc - `abc` -> abc

func ToPbJson deprecated

func ToPbJson[T proto.Message](obj T, indent string) (string, error)

Deprecated: Use `api/pb` package instead

func TrimWithEllipsis

func TrimWithEllipsis(s string, maxLength int, centered bool, dots int) string

TrimWithEllipsis trims the string `s` to `maxLength` characters. If `centered` is true, it shows the start and end of the string with ellipsis in the middle. The ellipsis length is controlled by `dots`. If remaining characters after dots are odd, extra character is shown on the prefix side.

func ValidateStruct

func ValidateStruct(st any) error

ValidateStruct validates a Go struct using https://github.com/go-playground/validator v10 Use this to validate config, request, response and other data types that are represented as Go structs instead of manually checking using if/else.

Example:

type SomeConfig {
		MyNotEmptyValue string `validate:"required"`
		MyEmail string `validate:"required,email"`
}

if err := utils.ValidateStruct(someConfig); err != nil {
	// Handle error
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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