vtree

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package vtree is an interface for C compatible vtrees used in sebase.

The vtrees in this package can be passed to C functions that accept those. By necessity they deal a lot with C pointers, thus you must make sure to call Close on them when you're done using them to release the C resources.

Often you will use bconf.BConfNode.Vtree to create these rather than the functions in this package.

Index

Constants

View Source
const (
	// Unknown type of node, you will have to inspect the keys
	// to determine if it's a list of a dictionary.
	VktUnknown VktType = C.vktUnknown
	VktDict            = C.vktDict
	VktList            = C.vktList
)

Variables

This section is empty.

Functions

This section is empty.

Types

type VktType

type VktType C.enum_vtree_keyvals_type

type VkvElement

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

An iterator to access elements in a loop or similar.

func (*VkvElement) Key

func (elem *VkvElement) Key() string

Access element key, returns empty string for VktList node types.

func (*VkvElement) Step

func (elem *VkvElement) Step(steps int) *VkvElement

Update elem to point to another element in the list. Returns elem if in range, otherwise returns nil.

func (*VkvElement) Value

func (elem *VkvElement) Value() VtreeValue

Access element value, which is a string, a *Vtree or nil.

type Vtree

type Vtree C.struct_vtree_chain

A vtree node you can use to call C functions that accept them. When calling to C, you will have to use CPtr() and a cast:

(*C.struct_vtree_chain)(vt.CPtr())

where vt is a *Vtree.

func (*Vtree) CPtr

func (vt *Vtree) CPtr() unsafe.Pointer

Casts vt. It's still a Go pointer and you have to obey the rules for those.

func (*Vtree) Close

func (vt *Vtree) Close()

Wrapper for C.vtree_free

func (*Vtree) Get

func (vt *Vtree) Get(key ...string) string

Get the string value at the path. Empty string will be returned if a value can't be found at this specific node.

func (*Vtree) GetInt

func (vt *Vtree) GetInt(key ...string) int

Get the integer value at the path, which can be 0. Defaults to returning 0 if the path is not a leaf node.

func (*Vtree) GetNode

func (vt *Vtree) GetNode(key ...string) *Vtree

Return a subnode. You can use this for leaf node paths, even though they have 0 length.

func (*Vtree) HasKey

func (vt *Vtree) HasKey(key ...string) bool

Returns true if the path is a valid node, otherwise false.

func (*Vtree) Keys

func (vt *Vtree) Keys(key ...string) []string

Fetch the subnode keys for the node at path, if any. Will return nil rather than an empty slice.

func (*Vtree) KeysAndValues

func (vt *Vtree) KeysAndValues(before []string, after []string) *VtreeKeyValue

Fetch both keys and values at the given paths (keys at before, values at before + after). Commonly used to serialize a vtree, when both before and after are nil. The returned structure must be closed when no longer used.

func (*Vtree) KeysByValue

func (vt *Vtree) KeysByValue(before []string, after []string, value string) []string

Fetch the keys at the before path, but filter on the value at after, and only return those nodes matching the given value. Will return nil rather than an empty slice.

func (*Vtree) Length

func (vt *Vtree) Length(key ...string) int

The number of nodes at the path, which can be 0 length. A path for a value has length 0, otherwise it's the number of subnodes. Non-existing paths also return 0.

func (*Vtree) Nodes

func (vt *Vtree) Nodes(key ...string) []*Vtree

Fetch the subnodes at the given path, if any. Will return nil rather than an empty slice.

func (*Vtree) Values

func (vt *Vtree) Values(before []string, after []string) []string

Fetch the values at the given path which is split in a before and after loop point. Will always return a list with the number of nodes at the before path, but each value will be that of the combination of before and after. Uses empty string if the after path does not exist at a given node. Will return nil rather than an empty slice.

type VtreeKeyValue

type VtreeKeyValue C.struct_vtree_keyvals

A type that can be iterated on. This is an ordered list of vtree keys and values, although keys are unset in case the list type is VktList.

While normally extracted from a Vtree you can also build these manually. Then you can convert them into a vtree. Often bconf is a nicer interface however.

func VtreeBuildKeyvals

func VtreeBuildKeyvals(vktype VktType, keys []string, values []VtreeValue) *VtreeKeyValue

Build a VtreeKeyValue from key value pairs. keys will be ignored if you're creating a list. Otherwise keys and values have to be the same length, or the function panics. Will also panic if a value is not nil, a string or a *Vtree. The returned VtreeKeyValue must be closed to release resources, and will call Close any Vtrees in the values when that is done.

func VtreeBuildKeyvalsMap

func VtreeBuildKeyvalsMap(m map[string]VtreeValue) *VtreeKeyValue

Sorts the map using the bconf sort order, then calls VtreeBuildKeyvals.

func (*VtreeKeyValue) CList

func (loop *VtreeKeyValue) CList() (unsafe.Pointer, int)

Access the underlying array. An example use could be to reorder it. You have to cast the pointer to a C.struct_vtree_keyvals_elem slice of the given length.

func (*VtreeKeyValue) Close

func (loop *VtreeKeyValue) Close() error

Free up resources.

func (*VtreeKeyValue) Index

func (loop *VtreeKeyValue) Index(idx int) *VkvElement

Access an element by index. Returns nil if out of bounds.

func (*VtreeKeyValue) Keys

func (loop *VtreeKeyValue) Keys() []string

The list of keys for subnodes. Returns nil on VktList node types.

func (*VtreeKeyValue) Len

func (loop *VtreeKeyValue) Len() int

Number of elements in list.

func (*VtreeKeyValue) Map

func (loop *VtreeKeyValue) Map() map[string]VtreeValue

A convenience map conversion. Vtree nodes are normally ordered, the returned map will not keep the ordering. Will return nil for VktList node types.

func (*VtreeKeyValue) Type

func (loop *VtreeKeyValue) Type() VktType

Type of list. Not used by bconf which always return VktUnknown.

func (*VtreeKeyValue) Values

func (loop *VtreeKeyValue) Values() []VtreeValue

The list of values for subnodes.

func (*VtreeKeyValue) Vtree

func (loop *VtreeKeyValue) Vtree() *Vtree

Convert to Vtree. You must Close the resulting Vtree to free its memory. Closing the vtree will also Close the receiver.

type VtreeValue

type VtreeValue interface{}

Will be either a string, a *Vtree or nil. It's possible that a key exists but has a nil value, e.g. if the after path points to a non-existing node.

Jump to

Keyboard shortcuts

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