xutils

package
v0.0.0-...-17f8324 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: BSD-3-Clause, MPL-2.0 Imports: 4 Imported by: 8

Documentation

Index

Constants

View Source
const (
	EOF = 0
	ERR = 0xF000 + iota
	NUM
	FUNC
	DOTDOT
	DBLSLASH
	DBLCOLON
	GT
	GE
	LT
	LE
	EQ
	NE
	NODETYPE
	AXISNAME
	NAMETEST
	LITERAL
	OR
	AND
	MOD
	DIV
)

The parser expects the lexer to return 0 on EOF. Give it a name for clarity. Also define 'special' tokens so they won't clash with other token values (ASCII value in most cases). Just to ensure mapping is working correctly, YACC starts off at 0xE000 or thereabouts, so this enum is deliberately starting at a different place to catch bugs where we've forgotten to map and some tokens are ok, some not.

View Source
const (
	StripPrefix     = true
	DontStripPrefix = false
)

Variables

View Source
var AllCfgChildren = NewXFilterConfigOnly(xml.Name{Local: "*"})
View Source
var AllChildren = NewXFilterFullTree(xml.Name{Local: "*"})

This is the global wildcard, representing all child nodes regardless of module.

Functions

func GetStringValue

func GetStringValue(nodes []XpathNode) string

If no nodes, return empty string. Otherwise return string-value of first node in nodeset.

Just as a reminder, the string-value for anything other than a leaf node is (a) variable (depends what child nodes are configured) and (b) not unique to a node (imagine a chain of node -> child -> grandchild -> leaf which, in the absence of any defaults, or multiple nodes at any level, would all have the same value).

func GetStringValues

func GetStringValues(nodes []XpathNode, addEmptyStr bool) []string

Return a slice of strings containing the string-value for each node in <nodes>. If <addEmptyStr> is true, return a slice with a single empty string instead of an empty slice when <nodes> is empty. This is useful when using the strings with equality or relational operators.

func GetTokenName

func GetTokenName(token int) string

func MatchFilter

func MatchFilter(filter XFilter, target XTarget) bool

Return true (match) if filter matches target or filter is AllChildren Note that filter may be '<namespaceName>:*'. Note also that we ignore prefix as this is local to a single namespace, and is converted to a namespace name for global uniqueness *and* global consistency of naming.

The filter may or may not have a prefix - if not then we only match on localPart.

Additionally, if the node we are matching is not a config node, and the filter indicates we can only match config nodes, no match will be made.

func NodeString

func NodeString(xNode XpathNode) string

Generate a unique string representation of a node, in 'leafref' format, including all key values for list entries, and ending with a leaf or leaf-list value if the node is of those types.

NB: This function is used to determine if 2 nodes are identical, not

just for pretty-printing.  If in any doubt that changes may impact
performance, try out TestFWPerformance in configd/session directory
with a large number of rules and compare old and new times.

NB: This is NOT string-value, which is not unique to a specific node,

and which can vary for a single node depending on what is configured
under it!

func NodesEqual

func NodesEqual(n1, n2 XpathNode) error

If 2 nodes have the same NodeString then they are identical. Two separate list elements may have the same path, but add in the key values and they differ again.

func NodesetsEqual

func NodesetsEqual(ns1, ns2 []XpathNode) error

func PrintTree

func PrintTree(startNode XpathNode)

func ValidateTree

func ValidateTree(root XpathNode) error

Takes root node and verifies at each level: - parent is correct - root is correct - children have correct parent - children have unique NodeString - no child is equal to another child - index is as expected

Types

type MatchType

type MatchType int
const (
	FullTree MatchType = iota
	ConfigOnly
	OpdOnly
)

type NodeRef

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

NodeRef: These allow Leafref-like objects be constructed and manipulated. These objects are references to nodes and so stop short of containing the actual value of leaf / leaf-list elements.

NB:

(1) Root node is represented by a NodeRef of length 0.

(2)All NodeRefs are absolute.

func NewNodeRef

func NewNodeRef(entries int) NodeRef

func (*NodeRef) AddElem

func (yp *NodeRef) AddElem(name string, keys []NodeRefKey)

func (NodeRef) EqualTo

func (yp1 NodeRef) EqualTo(yp2 NodeRef) bool

func (NodeRef) String

func (yp NodeRef) String() string

type NodeRefElem

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

func (NodeRefElem) EqualTo

func (ype1 NodeRefElem) EqualTo(ype2 NodeRefElem) bool

type NodeRefKey

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

func NewNodeRefKey

func NewNodeRefKey(name, value string) (retKey NodeRefKey)

func (NodeRefKey) EqualTo

func (ypk1 NodeRefKey) EqualTo(ypk2 NodeRefKey) bool

type PathType

type PathType []string

PATHTYPE

Useful to have this for manipulating paths. Provides a comparison function and string function.

func GetAbsPath

func GetAbsPath(expr string, curPath PathType) PathType

func NewPathType

func NewPathType(path string) PathType

func (PathType) EqualTo

func (p1 PathType) EqualTo(p2 PathType) bool

func (PathType) SpacedString

func (p PathType) SpacedString() string

func (PathType) String

func (p PathType) String() (pathStr string)

Generate path string, separated by '/'.

type SortSpec

type SortSpec bool
const (
	Unsorted SortSpec = false
	Sorted            = true
)

type TargetType

type TargetType int

TargetType - node type of the target to be matched Subtly different to MatchType as here we want to specify the type of our node explicitly whereas for MatchType we want to specify a set of types to be matched.

const (
	NotConfigOrOpdTarget TargetType = iota
	ConfigTarget
	OpdTarget
)

type WarnType

type WarnType int
const (
	ValidPath WarnType = iota
	DoesntExist
	MissingOrWrongPrefix
	MustOnNPContainer
	MustOnNPContWithNPChild
	RefNPContainer
	CompilerError
	ConfigdMustCompilerError
)

func (WarnType) String

func (w WarnType) String() string

type Warning

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

func NewWarning

func NewWarning(
	warning WarnType,
	startNode, xpathStmt, xpathLoc, testPath, debugStr string,
) Warning

func RemoveNPContainerWarnings

func RemoveNPContainerWarnings(warns []Warning) []Warning

func (Warning) GetType

func (w Warning) GetType() WarnType

func (Warning) GetUniqueString

func (w Warning) GetUniqueString(stripPrefix bool) string

Uniquely identifies what is being tested, ie module:line source of statement, followed by specific path being tested. Note that testPath can be stripped of its prefix if we are wanting to filter out false positives.

func (Warning) Match

func (w Warning) Match(expWarn Warning) error

'match' returns no error if all non-zero-length expWarn fields match <w>.

func (Warning) MatchDebugContains

func (w Warning) MatchDebugContains(expWarn Warning) error

Less strict on debug string matching - look for substring not exact match

func (Warning) String

func (w Warning) String() string

type XFilter

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

func NewXFilter

func NewXFilter(name xml.Name, matchOn MatchType) XFilter

func NewXFilterConfigOnly

func NewXFilterConfigOnly(name xml.Name) XFilter

func NewXFilterFullTree

func NewXFilterFullTree(name xml.Name) XFilter

func (XFilter) MatchConfigOnly

func (xf XFilter) MatchConfigOnly() bool

func (XFilter) Name

func (xf XFilter) Name() xml.Name

type XTarget

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

func NewXConfigTarget

func NewXConfigTarget(name xml.Name) XTarget

func NewXNonConfigOrOpdTarget

func NewXNonConfigOrOpdTarget(name xml.Name) XTarget

func NewXTarget

func NewXTarget(name xml.Name, targetType TargetType) XTarget

func (XTarget) IsConfig

func (t XTarget) IsConfig() bool

func (XTarget) IsOpd

func (t XTarget) IsOpd() bool

func (XTarget) Name

func (t XTarget) Name() xml.Name

type XpathNode

type XpathNode interface {
	// Return parent node
	XParent() XpathNode

	// Return all children, including list keys.
	// Specify 'Sorted' to get returned nodes in deterministic order.
	// Xpath uses 'document' order, so for YANG, our system sorts in natural
	// sorting order, unless ordered-by-user is specified.
	// 'Unsorted' should be used when order doesn't matter as it is much faster.
	XChildren(filter XFilter, sortSpec SortSpec) []XpathNode

	// Should return {"/"} for root node.  Returns XPATH-compliant path
	// where tagnodes and other list elements are treated as siblings.
	XPath() PathType

	XRoot() XpathNode

	// Node name.  For list entries, eg interfaces/dataplane entries, all list
	// entries would share the 'dataplane' name.  The 'tagnode' children
	// would have 'tagnode' as name etc.
	XName() string

	// Node value, if a text node (ie leaf / leaf list element).
	XValue() string

	// Type check functions to make sure we are operating on the expected
	// type.
	XIsLeaf() bool
	XIsLeafList() bool
	XIsNonPresCont() bool

	// Ephemeral nodes are created for the purposes of evaluating must
	// statements on non-presence, unconfigured, containers.
	XIsEphemeral() bool

	// Return true if node is a ListEntry with a key that has the given value.
	XListKeyMatches(key xml.Name, val string) bool

	// If node is a list entry, return keys.  Otherwise return nil.
	XListKeys() []NodeRefKey
}

To isolate us from node types we may want to work with, we have our own interface. To avoid any namespace collisions with other interfaces, all methods are prefixed with 'X'.

func FilterNodeset

func FilterNodeset(
	ns []XpathNode,
	key xml.Name,
	leafValue string,
) (retNs []XpathNode, debugLog string)

This is the code that actually does the work for leafref predicates. We need to find the nodes (if any) in the nodeset that match the given key/value tuple.

func FindNode

func FindNode(startNode XpathNode, pathToFind NodeRef) XpathNode

Dumb function that uses brute force to find a node with the given path. Designed for use with leafrefs and tab-completion.

func RemoveDuplicateNodes

func RemoveDuplicateNodes(nodes []XpathNode) []XpathNode

Naive implementation is to simply loop through range of nodes and if not already in our return list, add them. However, this quickly multiplies up if we were to have a large number of nodes.

Instead, we create a map with a unique representation of each node, being the NodeString (categorically NOT the string-value which varies for a single node and may be the same across multiple nodes)

NodeString is guaranteed unique for different nodes as it combines both path and (where siblings with the same path exist) value.

func WalkTree

func WalkTree(
	node XpathNode,
	workFn worker,
	index int,
) (retNode XpathNode, finished bool, retErr error)

Walk tree, calling workFn for each node, then children, recursively. If workFn returns error, end walk immediately.

Jump to

Keyboard shortcuts

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