config

package
v6.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AvailableExecutable

func AvailableExecutable(value string) error

AvailableExecutable checks that the given value is the name of an executable file, in PATH.

Types

type Error

type Error struct {
	Name   string // The name of the key this error is associated with.
	Value  any    // The value that the key was tried to be set to.
	Reason string // Human-readable reason of the error.
}

Error generated when trying to set a certain config key to certain value.

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

type ErrorList

type ErrorList []*Error

ErrorList is a list of configuration Errors occurred during Load() or Map.Change().

func (ErrorList) Error

func (l ErrorList) Error() string

ErrorList implements the error interface.

func (ErrorList) Len

func (l ErrorList) Len() int

ErrorList implements the sort Interface.

func (ErrorList) Less

func (l ErrorList) Less(i, j int) bool

func (ErrorList) Swap

func (l ErrorList) Swap(i, j int)

type Key

type Key struct {
	Type       Type   // Type of the value. It defaults to String.
	Default    string // If the key is not set in a Map, use this value instead.
	Deprecated string // Optional message to set if this config value is deprecated.

	// Optional function used to validate the values. It's called by Map
	// all the times the value associated with this Key is going to be
	// changed.
	Validator func(string) error

	// Optional function to manipulate a value before it's actually saved
	// in a Map. It's called only by Map.Change(), and not by Load() since
	// values passed to Load() are supposed to have been previously
	// processed.
	Setter func(string) (string, error)
}

Key defines the type of the value of a particular config key, along with other knobs such as default, validator, etc.

type Map

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

Map is a structured map of config keys to config values.

Each legal key is declared in a config Schema using a Key object.

func Load

func Load(schema Schema, values map[string]string) (Map, error)

Load creates a new configuration Map with the given schema and initial values. It is meant to be called with a set of initial values that were set at a previous time and persisted to some storage like a database.

If one or more keys fail to be loaded, return an ErrorList describing what went wrong. Non-failing keys are still loaded in the returned Map.

func SafeLoad

func SafeLoad(schema Schema, values map[string]string) (Map, error)

SafeLoad is a wrapper around Load() that does not error when invalid keys are found, and just logs warnings instead. Other kinds of errors are still returned.

func (*Map) Change

func (m *Map) Change(changes map[string]string) (map[string]string, error)

Change the values of this configuration Map.

Return a map of key/value pairs that were actually changed. If some keys fail to apply, details are included in the returned ErrorList.

func (*Map) Dump

func (m *Map) Dump() map[string]string

Dump the current configuration held by this Map.

Keys that match their default value will not be included in the dump.

func (*Map) GetBool

func (m *Map) GetBool(name string) bool

GetBool returns the value of the given key, which must be of type Bool.

func (*Map) GetInt64

func (m *Map) GetInt64(name string) int64

GetInt64 returns the value of the given key, which must be of type Int64.

func (*Map) GetRaw

func (m *Map) GetRaw(name string) string

GetRaw returns the value of the given key, which must be of type String.

func (*Map) GetString

func (m *Map) GetString(name string) string

GetString returns the value of the given key, which must be of type String.

type Schema

type Schema map[string]Key

Schema defines the available keys of a config Map, along with the types and options for their values, expressed using Key objects.

func (Schema) Defaults

func (s Schema) Defaults() map[string]string

Defaults returns a map of all key names in the schema along with their default values.

func (Schema) Keys

func (s Schema) Keys() []string

Keys returns all keys defined in the schema.

type Type

type Type int

Type is a numeric code indetifying a node value type.

const (
	String Type = iota
	Bool
	Int64
)

Possible Value types.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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