Documentation
¶
Index ¶
- Variables
- func CleanPath(path string) string
- func EnvBool(name string, def bool) bool
- func FindAnyWith[T any](items []T, matchFn func(item *T) bool) *T
- func FindInSlice[T comparable](items []T, x T) int
- func FromPbJson[T proto.Message](reader io.Reader, obj T) errordeprecated
- func FromPbToYaml[T proto.Message](writer io.Writer, obj T) errordeprecated
- func FromYamlToPb[T proto.Message](reader io.Reader, obj T) errordeprecated
- func Int64(max int64) int64
- func Introspect(v interface{}) string
- func IsEmptyString(s string) bool
- func MapStruct[T any](source interface{}, dest *T) error
- func NewUniqueId() string
- func ParseEnvToStruct[T any]() (T, error)
- func ParseEnvToStructWithOptions[T any](opts env.Options) (T, error)
- func PtrTo[T any](v T) *T
- func SafelyGetValue[T any](target *T) T
- func StringStripQuotes(s string) string
- func ToPbJson[T proto.Message](obj T, indent string) (string, error)deprecated
- func TrimWithEllipsis(s string, maxLength int, centered bool, dots int) string
- func ValidateStruct(st any) error
Constants ¶
This section is empty.
Variables ¶
var (
ErrValidatorNilValue = errors.New("validator cannot validate nil value")
)
Functions ¶
func EnvBool ¶
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 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 Introspect ¶
func Introspect(v interface{}) string
Serialize an interface using JSON or return error string
func IsEmptyString ¶
func NewUniqueId ¶
func NewUniqueId() string
func ParseEnvToStruct ¶
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 ¶
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 SafelyGetValue ¶
func SafelyGetValue[T any](target *T) T
func StringStripQuotes ¶
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 TrimWithEllipsis ¶
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 ¶
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.