cfgloader

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: Apache-2.0 Imports: 4 Imported by: 4

Documentation

Overview

Package cfgloader provides common functionality to load configs. It's meant for loading configs from YAML and/or env vars. Don't use it for initializing configs with defaults.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(ctx context.Context, cfg any, opt ...Option) error

Load loads config into the given config value. The loading order is:

  1. The existing values in the given config.
  2. Unmarshaled yaml bytes
  3. Env vars

The values loaded later will overwrite previously loaded values.

The given config type must have the yaml tags to load from yaml bytes. It must have the env tag to load from env vars. E.g.

type Cfg struct {
	StrVal string `yaml:"str_val,omitempty" env:"STR_VAL,overwrite,default=foo"`
	NumVal int    `yaml:"num_val,omitempty" env:"NUM_VAL,overwrite,default=1"`
}

Types

type Option

type Option func(*options) *options

Option is the config loading option type.

func WithEnvPrefix

func WithEnvPrefix(prefix string) Option

WithEnvPrefix instructs the loader to load config from env vars with the given prefix.

func WithLookuper

func WithLookuper(lookuper envconfig.Lookuper) Option

WithLookuper instructs the loader to use the giver lookuper to find config values.

func WithYAML

func WithYAML(b []byte) Option

WithYAML instructs the loader to load config from the given yaml bytes.

type Validatable

type Validatable interface {
	Validate() error
}

Validatable is the interface to validate a config. In addition to validating inputs, [Validate] may optionally fill or mutate config values.

Jump to

Keyboard shortcuts

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