util

package
v1.3.22 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: MIT Imports: 20 Imported by: 5

Documentation

Overview

Package util provides utility functions for the rest of the project.

Package util provides utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump added in v1.1.0

func Dump(v any) error

Dump the configuration from the environment variables into `v`. It: - Set values from environment variables using the `env` field tag. - Set default values using the `default` field tag. - Validating the values using the `validate` field tag.

Order of operations: 1. Set default values 2. Set values from environment variables 3. Validate values

NOTE: `v` must be a pointer to a struct. NOTE: It only sets default values for fields that are not set. NOTE: It'll set the value from env vars even if it's not empty (precedence).

NOTE: Like the built-in `json` tag, it'll ignore the field if it isn't exported, and if tag is set to `-`.

func DumpToEnv added in v1.1.6

func DumpToEnv(file *os.File, content map[string]string, rawValue bool) error

DumpToEnv dumps `finalValue` to a `.env` file.

func DumpToJSON added in v1.1.6

func DumpToJSON(file *os.File, content map[string]string) error

DumpToJSON dumps `finalValue` to a `configurer.json` file.

func DumpToYAML added in v1.1.6

func DumpToYAML(file *os.File, content map[string]string) error

DumpToYAML dumps `finalValue` to a `configurer.yaml` file.

func GenerateUUID added in v1.1.16

func GenerateUUID() string

GenerateUUID generates a RFC4122 UUID and DCE 1.1: Authentication and Security Services.

func GetValidator added in v1.1.4

func GetValidator() *validator.Validate

GetValidator returns the validator instance. Use that, for example, to add custom validators.

func GetZeroControlChar added in v1.1.26

func GetZeroControlChar() string

GetZeroControlChar returns the zero control character used to set the default value of a field. It's needed because if the tag is empty, there's no way to know if the user wants to set the field to the default value or if the field is not set. Set CONFIGURER_ZERO_CONTROL_CHAR to change the default value which is "zero".

func ParseContent added in v1.3.22

func ParseContent(ctx context.Context, format string, r io.Reader) (map[string]any, error)

ParseContent parses the string data in .env format.

func ParseFile added in v1.3.22

func ParseFile(ctx context.Context, file *os.File) (map[string]any, error)

ParseFile parse file. Extension is used to determine the format.

func ParseFromText added in v1.3.22

func ParseFromText(ctx context.Context, format string, data string) (map[string]any, error)

ParseFromText parses the string data in .env format.

func Process added in v1.1.26

func Process(v any) error

Process `v`: - Set default values using the `default` field tag. - Set values from environment variables using the `env` field tag. - Validating the values using the `validate` field tag.

Order of operations: 1. Set default values 2. Set values from environment variables 3. Validate values

NOTE: `v` must be a pointer to a struct. NOTE: It only sets default values for fields that are not set. NOTE: It'll set the value from env vars even if it's not empty (precedence).

NOTE: Like the built-in `json` tag, it'll ignore the field if it isn't exported, and if tag is set to `-`.

func SetDefault added in v1.1.2

func SetDefault(v any) error

SetDefault For a given struct `v`, set default values based on the struct field tags (`default`).

NOTE: It only sets default values for fields that are not set.

NOTE: Like the built-in `json` tag, it'll ignore the field if it isn't exported, and if tag is set to `-`.

func SetEnv added in v1.1.2

func SetEnv(v any) error

SetEnv For a given struct `v`, set values based on the struct field tags (`env`) and the environment variables.

WARN: It will set the value of the field even if it's not empty.

NOTE: Like the built-in `json` tag, it'll ignore the field if it isn't exported, and if tag is set to `-`.

func SetID added in v1.1.16

func SetID(v any) error

SetID For a given struct `v`, set field with the specified ID type.

NOTE: Currently only UUID is supported.

NOTE: It only sets default values for fields that are not set.

NOTE: Like the built-in `json` tag, it'll ignore the field if it isn't exported, and if tag is set to `-`.

Types

type Func added in v1.1.26

type Func func(v reflect.Value, field reflect.StructField, tag string) error

Func is the callback function type.

Jump to

Keyboard shortcuts

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