Documentation ¶
Index ¶
- func Clear()
- func Get(key string) interface{}
- func GetBool(key string) bool
- func GetBoolSlice(key string) []bool
- func GetFloat(key string) float64
- func GetFloatSlice(key string) []float64
- func GetInt(key string) int
- func GetIntSlice(key string) []int
- func GetString(key string) string
- func GetStringSlice(key string) []string
- func Has(key string) bool
- func IsLoaded() bool
- func Load() error
- func LoadFrom(path string) error
- func LoadJSON(cfg string) error
- func Register(key string, entry Entry)
- func Set(key string, value interface{})
- type Entry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetBool ¶
GetBool a config entry as bool. Panics if entry is not a bool or if it doesn't exist.
func GetBoolSlice ¶
GetBoolSlice a config entry as []bool. Panics if entry is not a bool slice or if it doesn't exist.
func GetFloat ¶
GetFloat a config entry as float64. Panics if entry is not a float64 or if it doesn't exist.
func GetFloatSlice ¶
GetFloatSlice a config entry as []float64. Panics if entry is not a float slice or if it doesn't exist.
func GetIntSlice ¶
GetIntSlice a config entry as []int. Panics if entry is not an int slice or if it doesn't exist.
func GetString ¶
GetString a config entry as string. Panics if entry is not a string or if it doesn't exist.
func GetStringSlice ¶
GetStringSlice a config entry as []string. Panics if entry is not a string slice or if it doesn't exist.
func Load ¶
func Load() 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 LoadJSON ¶
LoadJSON load a configuration file from raw JSON. Can be used in combination with Go's 1.16 embed directive.
var ( //go:embed config.json cfg string ) func main() { if err := config.LoadJSON(cfg); err != nil { goyave.ErrLogger.Println(err) os.Exit(goyave.ExitInvalidConfig) } if err := goyave.Start(route.Register); err != nil { os.Exit(err.(*goyave.Error).ExitCode) } }
func Register ¶
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.
func Set ¶
func Set(key string, value interface{})
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.
Types ¶
type Entry ¶
type Entry struct { Value interface{} AuthorizedValues []interface{} // 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.