config

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Provides the means to open and parse a sectioned configuration file.

Index

Constants

View Source
const (
	ET_INT int = iota
	ET_INT64
	ET_UINT
	ET_FLOAT
	ET_STRING
	ET_STRINGP
	ET_BOOL
)

Variables

This section is empty.

Functions

func Parse

func Parse(sectmap map[string]map[string]interface{}, fname string, all_str bool) (m map[string]map[string]interface{}, err error)

Parse reads and parsess a configuration file containing sections and key/value pairs within the sections. Returns a map of sections (by name) with each entry in the map being a map[string]interface{}. Key/values are converted and stored by the key name as either string pointers or float64s. If the value is quoted, the quotes are removed.

Section names may be duplicated which causes values appearing later in subsequent sections to be added to the previously encountered values. Keys within each section must be uniqueue. If a duplicate key is encountered, the last one read will be the one that ends up in the map.

If all_str is true, then all values are returned as strings; no attempt is made to convert values that seem to be numeric into actual values as it might make logic in the user programme a bit easier (no interface dreferences).

func Parse2strs

func Parse2strs(sectmap map[string]map[string]*string, fname string) (m map[string]map[string]*string, err error)

Parse2strs parsees the named file keeping the values as strings, and then we convert each section map into a map of strings, allowing for a simpler interface if the user doesn't want us to do the numeric conversions.

This may be easier for the caller to use as the returned values can be referenced with this syntax (assuming m is the returned map):

m[sect][key]

Types

type Config

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

Structure to wrap the map generated by Parse() allowing for the Extract_* funicton set to provide a simple interface to the user programme.

func Mk_config

func Mk_config(fname string) (conf *Config, err error)

Mk_config creates a config structure which provides an interface to the raw map loaded by the previous functions.

func (*Config) Dump

func (cfg *Config) Dump()

Dump will spit the hashes to stder.

func (*Config) Extract_bool

func (cfg *Config) Extract_bool(sect_list string, key string, def_value interface{}) (val bool)

Extract_bool will search the configuration in sect_list order for the given key string. The value associated with the key is converted to a boolean and returned to the caller. If the key is not found in any named section, then the default value is returned. The following shows what is considered 'true' if the value in the map is not a boolean:

string 'true'
*string *'true'
float  != 0.0
int		!= 0

Case matters for the string types.

func (*Config) Extract_float

func (cfg *Config) Extract_float(sect_list string, key string, def_value interface{}) (val float64)

Extract_float will search the configuration in sect_list order for the given key string. The value associated with the key is converted to a float64 value and returned to the caller. If the key is not found in any named section, then the default value is returned.

func (*Config) Extract_int

func (cfg *Config) Extract_int(sect_list string, key string, def_value interface{}) (val int)

Extract_int will search the configuration in sect_list order for the given key string. The value associated with the key is converted to a integer value and returned to the caller. If the key is not found in any named section, then the default value is returned.

func (*Config) Extract_int64

func (cfg *Config) Extract_int64(sect_list string, key string, def_value interface{}) (val int64)

Extract_int64 will search the configuration in sect_list order for the given key string. The value associated with the key is converted to a 64 bit integer value and returned to the caller. If the key is not found in any named section, then the default value is returned.

func (*Config) Extract_p2str

func (cfg *Config) Extract_p2str(sect_list string, key string, def_value interface{}) (val *string)

Extract_p2str will search the configuration in sect_list order for the given key string. The value associated with the key is converted to a string, and a pointer to that string is returned. If the key does not exist in any of the sections listed, then the default value is returned. The default value (def_val) may be either a string, or a pointer which should make the invoking code more straight forward. The default value may be a nil pointer.

func (*Config) Extract_str

func (cfg *Config) Extract_str(sect_list string, key string, def_value interface{}) (val string)

Extract_str will search the configuration in sect_list order for the given key string. The value associated with the key is converted to a string and returned to the caller. If the key is not found in any named section, then the default value is returned.

func (*Config) Extract_uint

func (cfg *Config) Extract_uint(sect_list string, key string, def_value interface{}) (val uint)

Extract_uint will search the configuration in sect_list order for the given key string. The value associated with the key is converted to an unsigned integerinteger value and returned to the caller. If the key is not found in any named section, then the default value is returned.

type Jconfig

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

func Mk_jconfig

func Mk_jconfig(fname string, sections string) (jc *Jconfig, err error)

Parse a file which we assume is json with a configuration. We generate a json tree which can be used to extract information. The sections is a space separated list of section names that any get requests will search.

We assume the conifig is small enough to read into core without issues.

func (*Jconfig) Dump

func (cfg *Jconfig) Dump()

Write to stderr a dump of the config.

func (*Jconfig) Extract_bool

func (cfg *Jconfig) Extract_bool(sects string, name string, def bool) bool

Suss out the named value as an integer and return if there; not in the config, returns the default.

func (*Jconfig) Extract_int

func (cfg *Jconfig) Extract_int(sects string, name string, def int) int

Various int wrapper functions which rely on the previous function.

func (*Jconfig) Extract_int32

func (cfg *Jconfig) Extract_int32(sects string, name string, def int32) int32

func (*Jconfig) Extract_int64

func (cfg *Jconfig) Extract_int64(sects string, name string, def int64) int64

Suss out the named value as a 64bit integer and return if there; not in the config, returns the default.

func (*Jconfig) Extract_posint

func (cfg *Jconfig) Extract_posint(sects string, name string, def int) int

Suss out the value for name and return if it is positive (>= 0) value if missing or the first found value in sects is < 0, then the default is returned (def).

func (*Jconfig) Extract_section

func (cfg *Jconfig) Extract_section(parent string, sname string, subsections string) (sect *Jconfig, err error)

Given a parent section, find the underlying section and return that as a Jconfig object that can be used on its own. Err is nil on success. Parent may be a space separated list searched in order given. Subsections are the list of sections under sname that are to be searched. If there are none, the 'default' section can be used to extract values from sname.

{
	"v1": 1,
	"v2": 2,
	"foo": {
			"fv1": 1,
			"fv2": 2,
			"fs1": {
				"something": 1,
				"else":		2
				"deeper_sect": {
					"dsv1":	10,
					"dsv2": 20
				}
			}
	}
}

To get a Jconfig for foo/fs1, assuming that cfg points at the whole tree, call:

fss := cfg.Get_section( "foo", fs1, "" )  // no subsections

Then fss can be used to get values from the default section:

fss.Get_int( "default", "something", 0 )

If fs1 has subobjects, they can be defined in the sections list on the Get_section() call and referenced instead of default.

func (*Jconfig) Extract_string

func (cfg *Jconfig) Extract_string(sects string, name string, def string) string

Look up a string in the config and return its value or the default that is provided. Sects is a space separated list of sections to search, searched in the order given (e.g. "rico anolis default").

func (*Jconfig) Extract_stringptr

func (cfg *Jconfig) Extract_stringptr(sects string, name string, def interface{}) *string

Look up a string in the config and return a pointer to the string or the default. The default may be either a string or a pointer; if a string is supplied as the default a pointer to that string is returned (allows constant string to be supplied without having to have a separate dummy variable created before calling this function.)

Jump to

Keyboard shortcuts

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