Dominik Schulz 2adc544dea
[bugfix] Default to true for core.exportkeys even in substores (#2848)
* [bugfix] Default to true for core.exportkeys even in substores

This PR changes the default for core.exportkeys from false to true
in mounted substores to match the default of the global root store.

It also refactors and simplifies the config package a little bit
by removing special typed lookup methods and replacing them with
conversion helpers that can be applied to any string.

Fixes #2830

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

* Fix config tests

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>

---------

Signed-off-by: Dominik Schulz <dominik.schulz@gauner.org>
2024-03-29 15:28:26 +01:00

68 lines
1.3 KiB
Go

package config
import (
"context"
"strconv"
)
// AsBool converts a string to a bool value.
func AsBool(s string) bool {
return AsBoolWithDefault(s, false)
}
// AsBoolWithDefault converts a string to a bool value with a default value.
func AsBoolWithDefault(s string, def bool) bool {
if s == "" {
return def
}
switch s {
case "1", "true", "yes", "on":
return true
case "0", "false", "no", "off":
return false
default:
return def
}
}
// AsInt converts a string to an integer value.
func AsInt(s string) int {
return AsIntWithDefault(s, 0)
}
// AsIntWithDefault converts a string to an integer value with a default value.
func AsIntWithDefault(s string, def int) int {
if s == "" {
return def
}
i, err := strconv.Atoi(s)
if err != nil {
return def
}
return i
}
// Bool returns a bool value from the config in the context.
func Bool(ctx context.Context, key string) bool {
cfg, mp := FromContext(ctx)
return AsBool(cfg.GetM(mp, key))
}
// String returns a string value from the config in the context.
func String(ctx context.Context, key string) string {
cfg, mp := FromContext(ctx)
return cfg.GetM(mp, key)
}
// Int returns an integer value from the config in the context.
func Int(ctx context.Context, key string) int {
cfg, mp := FromContext(ctx)
return AsInt(cfg.GetM(mp, key))
}