config

package
v5.0.0-rcx.14 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 11 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(key string, entry Entry)

Register a new config entry and its validation.

Each module should register its config entries in an "init()" function, even if they don't have a default value, in order to ensure they will be validated. Each module should use its own category and use a name both expressive and unique to avoid collisions. For example, the "auth" package registers, among others, "auth.basic.username" and "auth.jwt.expiry", thus creating a category for its package, and two subcategories for its features.

To register an entry without a default value (only specify how it will be validated), set "Entry.Value" to "nil".

Panics if an entry already exists for this key and is not identical to the one passed as parameter of this function. On the other hand, if the entries are identical, no conflict is expected so the configuration is left in its current state.

Types

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config structure holding a configuration that should be used for a single instance of `goyave.Server`.

This structure is not protected for safe concurrent access in order to increase performance. Therefore, you should never use the `Set()` function when the configuration is in use by an already running server.

func Load

func Load() (*Config, error)

Load loads the config.json file in the current working directory. If the "GOYAVE_ENV" env variable is set, the config file will be picked like so:

  • "production": "config.production.json"
  • "test": "config.test.json"
  • By default: "config.json"

func LoadDefault

func LoadDefault() *Config

LoadDefault loads default config.

func LoadFrom

func LoadFrom(path string) (*Config, error)

LoadFrom loads a config file from the given path.

func LoadJSON

func LoadJSON(cfg string) (*Config, error)

LoadJSON load a configuration file from raw JSON. Can be used in combination with Go's embed directive.

var (
	//go:embed config.json
	cfgJSON string
)

func main() {
	cfg, err := config.LoadJSON(cfgJSON)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
		os.Exit(1)
	}

	server, err := goyave.New(goyave.Options{Config: cfg})
	if err != nil {
		fmt.Fprintln(os.Stderr, err.(*errors.Error).String())
		os.Exit(1)
	}

	// ...
}

func (*Config) Get

func (c *Config) Get(key string) any

Get a config entry using a dot-separated path. Panics if the entry doesn't exist.

func (*Config) GetBool

func (c *Config) GetBool(key string) bool

GetBool a config entry as bool. Panics if entry is not a bool or if it doesn't exist.

func (*Config) GetBoolSlice

func (c *Config) GetBoolSlice(key string) []bool

GetBoolSlice a config entry as []bool. Panics if entry is not a bool slice or if it doesn't exist.

func (*Config) GetFloat

func (c *Config) GetFloat(key string) float64

GetFloat a config entry as float64. Panics if entry is not a float64 or if it doesn't exist.

func (*Config) GetFloatSlice

func (c *Config) GetFloatSlice(key string) []float64

GetFloatSlice a config entry as []float64. Panics if entry is not a float slice or if it doesn't exist.

func (*Config) GetInt

func (c *Config) GetInt(key string) int

GetInt a config entry as int. Panics if entry is not an int or if it doesn't exist.

func (*Config) GetIntSlice

func (c *Config) GetIntSlice(key string) []int

GetIntSlice a config entry as []int. Panics if entry is not an int slice or if it doesn't exist.

func (*Config) GetString

func (c *Config) GetString(key string) string

GetString a config entry as string. Panics if entry is not a string or if it doesn't exist.

func (*Config) GetStringSlice

func (c *Config) GetStringSlice(key string) []string

GetStringSlice a config entry as []string. Panics if entry is not a string slice or if it doesn't exist.

func (*Config) Has

func (c *Config) Has(key string) bool

Has check if a config entry exists.

func (*Config) Set

func (c *Config) Set(key string, value any)

Set a config entry. The change is temporary and will not be saved for next boot. Use "nil" to unset a value.

  • A category cannot be replaced with an entry.
  • An entry cannot be replaced with a category.
  • New categories can be created with they don't already exist.
  • New entries can be created if they don't already exist. This new entry will be subsequently validated using the type of its initial value and have an empty slice as authorized values (meaning it can have any value of its type)

Panics and revert changes in case of error.

This operation is not concurrently safe and should not be used when the configuration is in use by an already running server.

type Entry

type Entry struct {
	Value            any
	AuthorizedValues []any // Leave empty for "any"
	Type             reflect.Kind
	IsSlice          bool
}

Entry is the internal reprensentation of a config entry. It contains the entry value, its expected type (for validation) and a slice of authorized values (for validation too). If this slice is empty, it means any value can be used, provided it is of the correct type.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error returned when the configuration could not be loaded or is invalid. Can be unwraped to get the original error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Jump to

Keyboard shortcuts

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