Documentation ¶
Overview ¶
Package config provides methods to read, write and update configurations. The intended use case is in programs that need to make use of API's that accept POST requests with JSON payloads. The package is designed to be simple and easy to use. It is not intended to be a general-purpose configuration library with robust type checking and validation.
Index ¶
- func ConfigFromString(s string) (map[string]any, error)
- func Get(c map[string]any, keys ...string) (value any, ok bool)
- func HasKey(c map[string]any, key string) bool
- func HasKeyNested(c map[string]any, keys ...string) bool
- func Read(filepath string) (c map[string]any, err error)
- func Set(c map[string]any, value any, keys ...string)
- func Update(source, target map[string]any)
- func Write(cfg map[string]any, filepath string) (err error)
- type Config
- type Translation
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigFromString ¶ added in v0.10.3
ConfigFromString returns a config from a string The string should be in the form of a JSON object. Opening and closing braces are optional.
func Get ¶ added in v0.10.3
Get returns the value of a (nested) key in the configuration and a boolean indicating whether the key exists.
func HasKeyNested ¶ added in v0.10.3
HasKeyNested returns true if the nested key exists in the configuration.
func Read ¶ added in v0.10.3
Read reads a configuration from a file. If the read is successful and the file text can be unmarshalled, Read overwrites the target configuration. Otherwise, an error is returned and the target configuration is not affected. Note that the behavior of json.Unmarshal is such that Unmarshal stores one of these in interface values:
bool for JSON booleans, float64 for JSON numbers, string for JSON strings, []any for JSON arrays, map[string]any, for JSON objects, nil for JSON null.
Thus, you can't get an integer back from a JSON number and any code that needs an integer will have to do the conversion itself.
func Set ¶ added in v0.10.3
Set sets the value of a (nested) key in the configuration. If the key does not exist, it is added to the configuration. If the key exists, the value is updated. If the key is nested, the nested map is created if it does not exist.
func Update ¶ added in v0.10.3
Update updates a target configuration with the values from another configuration (source). If a key exists only in the source configuration, It is added to target. If a key exists in both configurations, the value from the source configuration is used. Keys that are only present in the target configuration are not affected. Map values are updated recursively.
Types ¶
type Config ¶
In this package, a configuration is represented as a map[string]any and serialized to a file in JSON format.
func (Config) CompareTypes ¶ added in v0.10.3
func (c Config) CompareTypes(refCfg Config, prefix string, mismatches *[]string, notFound *[]string)
CompareTypes compares the types of keys in two Config objects. The function compares the types of keys in the Config object with the keys in the reference Config object. The function returns two slices of strings. The first slice contains the keys where the types do not match. The second slice contains the keys that are in the Config object but not in the reference Config object.
func (Config) UniqueKeyMatchOf ¶
UniqueKeyMatchOf returns the unique key, if any, that matches the given shortcut, k. If there is no match or more than one match, the function returns an empty string. The comparison is case-insensitive and ignores the characters in the ignore slice. Thus, frob would match FROB, fRoB, and frobish. If the ignore slice contains '_', frob would also match _f_ro_b_.
type Translation ¶ added in v0.10.3
func (Translation) Apply ¶ added in v0.10.3
func (t Translation) Apply(cFrom, cTo map[string]any, sep string) error
Apply() interprets the keys and values of a Translation object as paths to nested values in a Config object. It splits the keys and values by the separator and then sets the values in cTo to the corresponding values in cFrom.