ycfg

package
v0.0.0-...-56472e5 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type YCfg

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

YAML configuration object. This is a substitute for a viper configuration object, with the following newt-specific advantages: 1. Case sensitive. 2. Efficient conditionals based on syscfg values.

A single YCfg setting is implemented as a tree of nodes. Each word in the setting name represents a node; each "." in the name is a link in the tree. For example, the following syscfg lines:

OS_MAIN_STACK_SIZE: 100 OS_MAIN_STACK_SIZE.BLE_DEVICE: 200 OS_MAIN_STACK_SIZE.SHELL_TASK: 300

Is represented as the following tree:

          [OS_MAIN_STACK_SIZE (100)]
         /                          \
[BLE_DEVICE (200)]           [SHELL_TASK (300)]

This allows us to quickly determine the value of OS_MAIN_STACK_SIZE. After finding the OS_MAIN_STACK_SIZE node, the logic is something like this:

Is BLE_DEVICE true? --> 200 Is SHELL_TASK true? --> 300 Else: --> 100

The tree structure also allows for arbitrary expressions as conditionals, as opposed to simple setting names. For example:

OS_MAIN_STACK_SIZE: 100 OS_MAIN_STACK_SIZE.'(BLE_DEVICE && !SHELL_TASK): 200 OS_MAIN_STACK_SIZE.'(SHELL_TASK && !BLE_DEVICE): 300 OS_MAIN_STACK_SIZE.'(SHELL_TASK && BLE_DEVICE): 400

Since each expression is a child node of the setting in question, they are all known at the time of the lookup. To determine the value of the setting, each expression is parsed, and only the one evaluating to true is selected.

func NewYCfg

func NewYCfg(name string) YCfg

func (*YCfg) AllSettings

func (yc *YCfg) AllSettings() map[string]interface{}

AllSettings converts the YCfg into a map with the following form:

<node-full-name>: <node-value>

func (*YCfg) AllSettingsAsStrings

func (yc *YCfg) AllSettingsAsStrings() map[string]string

AllSettingsAsStrings converts the YCfg into a map with the following form:

<node-full-name>: <node-value>

All values in the map have been coerced to strings.

func (*YCfg) Clear

func (yc *YCfg) Clear()

Clear removes all entries from the YCfg.

func (*YCfg) Delete

func (yc *YCfg) Delete(key string)

Delete deletes all entries with the specified key.

func (*YCfg) Get

func (yc *YCfg) Get(key string,
	settings map[string]string) ([]YCfgEntry, error)

Get retrieves all nodes with the specified key. If it encounters a parse error in the tree, it ignores the bad node and continues the search. All bad nodes are indicated in the returned error. In this sense, the returned object is valid even if there is an error, and the error can be thought of as a set of warnings.

func (*YCfg) GetFirst

func (yc *YCfg) GetFirst(key string,
	settings map[string]string) (YCfgEntry, bool, error)

GetFirst retrieves the first entry with the specified key. The bool return value is true if a matching entry was found. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetFirstVal

func (yc *YCfg) GetFirstVal(key string,
	settings map[string]string) (interface{}, error)

GetFirstVal retrieves the first entry with the specified key and returns its value. It returns nil if no matching entry is found. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetSlice

func (yc *YCfg) GetSlice(key string, settings map[string]string) ([]YCfgEntry, error)

GetSlice retrieves all entries with the specified key and coerces their values to type []interface{}. The returned []YCfgEntry is formed from the union of all these slices. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetStringMap

func (yc *YCfg) GetStringMap(
	key string, settings map[string]string) (map[string]YCfgEntry, error)

GetStringMap retrieves all entries with the specified key and coerces their values to type map[string]interface{}. The returned map[string]YCfgEntry is formed from the union of all these maps. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetStringMapString

func (yc *YCfg) GetStringMapString(key string,
	settings map[string]string) (map[string]YCfgEntry, error)

GetStringMapString retrieves all entries with the specified key and coerces their values to type map[string]string. The returned map[string]YCfgEntry is formed from the union of all these maps. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetStringSlice

func (yc *YCfg) GetStringSlice(key string,
	settings map[string]string) ([]YCfgEntry, error)

GetStringSlice retrieves all entries with the specified key and coerces their values to type []string. The returned []YCfgEntry is formed from the union of all these slices. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValBool

func (yc *YCfg) GetValBool(key string,
	settings map[string]string) (bool, error)

GetValBoolDflt retrieves the first entry with the specified key and returns its value coerced to a bool. It returns false if no matching entry is found. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValBoolDflt

func (yc *YCfg) GetValBoolDflt(key string, settings map[string]string,
	dflt bool) (bool, error)

GetValBoolDflt retrieves the first entry with the specified key and returns its value coerced to a bool. It returns the specified default if no matching entry is found. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValInt

func (yc *YCfg) GetValInt(key string, settings map[string]string) (int, error)

GetValInt retrieves the first entry with the specified key and returns its value coerced to an int. It returns 0 if no matching entry is found. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValSlice

func (yc *YCfg) GetValSlice(
	key string, settings map[string]string) ([]interface{}, error)

GetValSlice retrieves all entries with the specified key and coerces their values to type []interface{}. The returned slice is the union of all these slices. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValString

func (yc *YCfg) GetValString(key string,
	settings map[string]string) (string, error)

GetValString retrieves the first entry with the specified key and returns its value coerced to a string. It returns "" if no matching entry is found. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValStringMap

func (yc *YCfg) GetValStringMap(
	key string, settings map[string]string) (map[string]interface{}, error)

GetValStringMap retrieves all entries with the specified key and coerces their values to type map[string]interface{}. The returned map[string]YCfgEntry is the union of all these maps. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValStringMapString

func (yc *YCfg) GetValStringMapString(key string,
	settings map[string]string) (map[string]string, error)

GetStringMapString retrieves all entries with the specified key and coerces their values to type map[string]string. The returned map[string]YCfgEntry is the union of all these maps. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValStringSlice

func (yc *YCfg) GetValStringSlice(
	key string, settings map[string]string) ([]string, error)

GetValStringSlice retrieves all entries with the specified key and coerces their values to type []string. The returned []string is the union of all these slices. The returned error is a set of warnings just as in `Get`.

func (*YCfg) GetValStringSliceNonempty

func (yc *YCfg) GetValStringSliceNonempty(
	key string, settings map[string]string) ([]string, error)

GetValStringSliceNonempty retrieves all entries with the specified key and coerces their values to type []string. The returned []string is the union of all these slices. Empty strings are excluded from this union. The returned error is a set of warnings just as in `Get`.

func (*YCfg) HasKey

func (yc *YCfg) HasKey(key string) bool

func (*YCfg) MergeFromFile

func (yc *YCfg) MergeFromFile(key string, val interface{},
	fileInfo *util.FileInfo) error

MergeFromFile merges the given value into a tree node. Only two value types can be merged:

map[interface{}]interface{}
[]interface{}

The node's current value must have the same type as the value being merged. In the map case, each key-value pair in the given value is inserted into the current value, overwriting as necessary. In the slice case, the given value is appended to the current value.

If no node with the specified key exists, a new node is created containing the given value.

func (*YCfg) Replace

func (yc *YCfg) Replace(key string, val interface{}) error

func (*YCfg) ReplaceFromFile

func (yc *YCfg) ReplaceFromFile(key string, val interface{},
	fileInfo *util.FileInfo) error

func (*YCfg) String

func (yc *YCfg) String() string

String produces a user-friendly string representation of the YCfg.

func (*YCfg) Traverse

func (yc *YCfg) Traverse(cb func(node *YCfgNode, depth int))

Traverse performs an in-order traversal of the YCfg tree. The specified function is applied to each node.

func (*YCfg) Tree

func (yc *YCfg) Tree() YCfgTree

func (*YCfg) YAML

func (yc *YCfg) YAML() string

YAML converts the YCfg to a map and encodes the map as YAML.

type YCfgEntry

type YCfgEntry struct {
	Value interface{}
	Expr  *parse.Node
}

type YCfgNode

type YCfgNode struct {
	Overwrite bool
	Name      string
	Value     interface{}
	Children  YCfgTree
	Parent    *YCfgNode
	FileInfo  *util.FileInfo
}

func NewYCfgNode

func NewYCfgNode() *YCfgNode

func (*YCfgNode) FullName

func (node *YCfgNode) FullName() string

FullName calculates a node's name with the following form:

[...].<grandparent>.<parent>.<node>

type YCfgTree

type YCfgTree map[string]*YCfgNode

Jump to

Keyboard shortcuts

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