WIP on v1

This commit is contained in:
Dmitry Fedotov
2025-04-09 02:13:47 +03:00
parent 27cb532ec4
commit b88b2fd5e6
5 changed files with 317 additions and 159 deletions

View File

@@ -56,54 +56,57 @@
// quite self-explanatory.
package conf
// Config holds parsed keys and values. Settings and Options can be
// accessed with Config.Settings and Config.Options maps directly.
type Config struct {
// Config holds parsed keys and values.
type Conf struct {
// Settings store key value pairs ("key = value" in config file)
// all key value pairs found when parsing input are accumulated in this map.
Settings map[string]string
// Options map stores single word options ("option" in config file)
Options map[string]struct{}
settings map[string]string
}
// Find looks up a Setting and returns it. If returned error is not nil
// the requested key was not found and returned Setting has empty string in Value
// field.
func (c *Config) Find(key string) (s Setting, err error) {
v, ok := c.Settings[key]
if !ok {
err = ErrNotFound
}
s.Value = v
return
func (c *Conf) Find(key string) (Setting, bool) {
return c.get(key, "")
}
// Get returns a Setting. If key was not found the returned Setting Value
// will be empty string.
func (c *Config) Get(key string) (s Setting) {
s.Value = c.Settings[key]
return
func (c *Conf) Get(key string) Setting {
s, _ := c.get(key, "")
return s
}
// GetDefault looks up a Setting with requested key.
// If lookup fails it returns Setting with Value field set to def.
func (c *Config) GetDefault(key, def string) (s Setting) {
v, ok := c.Settings[key]
switch ok {
case true:
s.Value = v
default:
s.Value = def
}
return
func (c *Conf) GetDefault(key, def string) Setting {
s, _ := c.get(key, def)
return s
}
// HasOption returns true if line:
//
// "key"
//
// was found in the parsed file
func (c *Config) HasOption(option string) (exists bool) {
_, exists = c.Options[option]
return
func (c *Conf) Settings() []Setting {
list := make([]Setting, 0, len(c.settings))
for k, v := range c.settings {
s := Setting{
Name: k,
Value: v,
}
list = append(list, s)
}
return list
}
func (c *Conf) get(key string, def string) (Setting, bool) {
v, ok := c.settings[key]
if def != "" && !ok {
v = def
}
return Setting{
Name: key,
Value: v,
}, ok
}