Documentation
¶
Overview ¶
Package util implements utlity functions used in ygot.
Index ¶
- Constants
- Variables
- func ChildSchema(schema *yang.Entry, path []string) *yang.Entry
- func DataSchemaTreesString(schema *yang.Entry, dataTree interface{}) string
- func DbgErr(err error) error
- func DbgPrint(v ...interface{})
- func DbgSchema(v ...interface{})
- func Dedent()
- func DeepEqualDerefPtrs(a, b interface{}) bool
- func FieldSchema(schema *yang.Entry, f reflect.StructField) (*yang.Entry, error)
- func FindFirstNonChoiceOrCase(e *yang.Entry) map[string]*yang.Entry
- func FindPathElemPrefix(paths []*gpb.Path) *gpb.Path
- func GetNodes(schema *yang.Entry, root interface{}, path *gpb.Path) ([]interface{}, []*yang.Entry, error)
- func Indent()
- func InitializeStructField(parent interface{}, fieldName string) error
- func InsertIntoMap(parentMap interface{}, key interface{}, value interface{}) error
- func InsertIntoMapStructField(parentStruct interface{}, fieldName string, key, fieldValue interface{}) error
- func InsertIntoSlice(parentSlice interface{}, value interface{}) error
- func InsertIntoSliceStructField(parentStruct interface{}, fieldName string, fieldValue interface{}) error
- func InsertIntoStruct(parentStruct interface{}, fieldName string, fieldValue interface{}) error
- func IsChoiceOrCase(e *yang.Entry) bool
- func IsCompressedSchema(s *yang.Entry) bool
- func IsFakeRoot(e *yang.Entry) bool
- func IsLeafRef(schema *yang.Entry) bool
- func IsNilOrInvalidValue(v reflect.Value) bool
- func IsStructValueWithNFields(v reflect.Value, n int) bool
- func IsTypeInterface(t reflect.Type) bool
- func IsTypeMap(t reflect.Type) bool
- func IsTypeSlice(t reflect.Type) bool
- func IsTypeSliceOfInterface(t reflect.Type) bool
- func IsTypeSlicePtr(t reflect.Type) bool
- func IsTypeStruct(t reflect.Type) bool
- func IsTypeStructPtr(t reflect.Type) bool
- func IsUnkeyedList(e *yang.Entry) bool
- func IsValueInterface(v reflect.Value) bool
- func IsValueInterfaceToStructPtr(v reflect.Value) bool
- func IsValueMap(v reflect.Value) bool
- func IsValueNil(value interface{}) bool
- func IsValueNilOrDefault(value interface{}) bool
- func IsValuePtr(v reflect.Value) bool
- func IsValueScalar(v reflect.Value) bool
- func IsValueSlice(v reflect.Value) bool
- func IsValueStruct(v reflect.Value) bool
- func IsValueStructPtr(v reflect.Value) bool
- func IsValueTypeCompatible(t reflect.Type, v reflect.Value) bool
- func IsYgotAnnotation(s reflect.StructField) bool
- func MatchingNonChoiceCaseSchema(schema *yang.Entry, path []string) *yang.Entry
- func PathMatchesPathElemPrefix(path, prefix *gpb.Path) bool
- func PathMatchesPrefix(path *gpb.Path, prefix []string) bool
- func PopGNMIPath(path *gpb.Path) *gpb.Path
- func ResetIndent()
- func ResolveIfLeafRef(schema *yang.Entry) (*yang.Entry, error)
- func SchemaPaths(f reflect.StructField) ([][]string, error)
- func SchemaTreeString(schema *yang.Entry, prefix string) string
- func SchemaTypeStr(schema *yang.Entry) string
- func StripModulePrefix(s string) string
- func StripModulePrefixes(in []string) []string
- func StripModulePrefixesStr(in string) string
- func ToString(errors []error) string
- func TrimGNMIPathElemPrefix(path, prefix *gpb.Path) *gpb.Path
- func TrimGNMIPathPrefix(path *gpb.Path, prefix []string) *gpb.Path
- func UpdateField(parentStruct interface{}, fieldName string, fieldValue interface{}) error
- func ValueStr(value interface{}) string
- func ValueStrDebug(value interface{}) string
- func ValuesAreSameType(v1 reflect.Value, v2 reflect.Value) bool
- func YangTypeToDebugString(yt *yang.YangType) string
- type Errors
- func AppendErr(errors []error, err error) Errors
- func AppendErrs(errors []error, newErrs []error) Errors
- func ForEachDataField(value, in, out interface{}, iterFunction FieldIteratorFunc) Errors
- func ForEachField(schema *yang.Entry, value interface{}, in, out interface{}, ...) Errors
- func NewErrs(err error) Errors
- func PrefixErrors(errs Errors, pfx string) Errors
- func UniqueErrors(errs Errors) Errors
- type FieldIteratorFunc
- type NodeInfo
Constants ¶
const CompressedSchemaAnnotation string = "isCompressedSchema"
CompressedSchemaAnnotation stores the name of the annotation indicating whether a set of structs were built with -compress_path. It is appended to the yang.Entry struct of the root entity of the structs within the SchemaTree.
Variables ¶
Functions ¶
func ChildSchema ¶
ChildSchema returns the first child schema that matches path from the given schema root. When comparing the path, only nodes that appear in the data tree are considered. It returns nil if no node matches the path.
func DataSchemaTreesString ¶
DataSchemaTreesString outputs a combined data/schema tree string where schema is displayed alongside the data tree e.g.
[device (container)] RoutingPolicy [routing-policy (container)] DefinedSets [defined-sets (container)] PrefixSet [prefix-set (list)] prefix1 prefix1 {255.255.255.0/20 20..24} IpPrefix : "255.255.255.0/20" [ip-prefix (leaf)] MasklengthRange : "20..24" [masklength-range (leaf)] PrefixSetName : "prefix1" [prefix-set-name (leaf)]
func DbgPrint ¶
func DbgPrint(v ...interface{})
DbgPrint prints v if the package global variable debugLibrary is set. v has the same format as Printf. A trailing newline is added to the output.
func DbgSchema ¶
func DbgSchema(v ...interface{})
DbgSchema prints v if the package global variable debugSchema is set. v has the same format as Printf.
func DeepEqualDerefPtrs ¶
func DeepEqualDerefPtrs(a, b interface{}) bool
DeepEqualDerefPtrs compares the values of a and b. If either value is a ptr, it is dereferenced prior to the comparison.
func FieldSchema ¶
FieldSchema returns the schema for the struct field f, if f contains a valid path tag and the schema path is found in the schema tree. It returns an error if the struct tag is invalid, or nil if tag is valid but the schema is not found in the tree at the specified path.
func FindFirstNonChoiceOrCase ¶
FindFirstNonChoiceOrCase recursively traverses the schema tree and returns a map with the set of the first nodes in every path that are neither case nor choice nodes. The keys in the map are the paths to the matching elements from the parent data struct, which always have length 1.
func FindPathElemPrefix ¶ added in v0.6.0
FindPathElemPrefix finds the longest common prefix of the paths specified.
func GetNodes ¶
func GetNodes(schema *yang.Entry, root interface{}, path *gpb.Path) ([]interface{}, []*yang.Entry, error)
GetNodes returns the nodes in the data tree at the indicated path, relative to the supplied root and their corresponding schemas at the same slice index. schema is the schema for root. If the key for a list node is missing, all values in the list are returned. If the key is partial, all nodes matching the values present in the key are returned. If the root is the tree root, the path may be absolute. GetNodes returns an error if the path is not found in the tree, or an element along the path is nil.
func InitializeStructField ¶ added in v0.6.0
InitializeStructField initializes the given field in the given struct. Only pointer fields and some of the composite types are initialized(Map). It initializes to zero value of the underlying type if the field is a pointer. If the field is a slice, no need to initialize as appending a new element will do the same thing. Note that if the field is initialized already, this function doesn't re-initialize it.
func InsertIntoMap ¶
func InsertIntoMap(parentMap interface{}, key interface{}, value interface{}) error
InsertIntoMap inserts value with key into parent which must be a map.
func InsertIntoMapStructField ¶
func InsertIntoMapStructField(parentStruct interface{}, fieldName string, key, fieldValue interface{}) error
InsertIntoMapStructField inserts fieldValue into a field of type map in parentStruct called fieldName (which must exist, but may be nil), using the given key. If the key already exists in the map, the corresponding value is updated.
func InsertIntoSlice ¶
func InsertIntoSlice(parentSlice interface{}, value interface{}) error
InsertIntoSlice inserts value into parent which must be a slice ptr.
func InsertIntoSliceStructField ¶
func InsertIntoSliceStructField(parentStruct interface{}, fieldName string, fieldValue interface{}) error
InsertIntoSliceStructField inserts fieldValue into a field of type slice in parentStruct called fieldName (which must exist, but may be nil).
func InsertIntoStruct ¶
InsertIntoStruct updates a field called fieldName (which must exist, but may be nil) in parentStruct, with value fieldValue. If the struct field type is a ptr and the value is non-ptr, the field is populated with the corresponding ptr type.
func IsChoiceOrCase ¶
IsChoiceOrCase returns true if the entry is either a 'case' or a 'choice' node within the schema. These are schema nodes only, and the code generation operates on data tree paths.
func IsCompressedSchema ¶ added in v0.6.0
IsCompressedSchema determines whether the yang.Entry s provided is part of a generated set of structs that have schema compression enabled. It traverses to the schema root, and determines the presence of an annotation with the name CompressedSchemaAnnotation which is added by ygen.
func IsFakeRoot ¶
IsFakeRoot reports whether the supplied yang.Entry represents the synthesised root entity in the generated code.
func IsNilOrInvalidValue ¶
IsNilOrInvalidValue reports whether v is nil or reflect.Zero.
func IsStructValueWithNFields ¶
IsStructValueWithNFields returns true if the reflect.Value representing a struct v has n fields.
func IsTypeInterface ¶
IsTypeInterface reports whether v is an interface.
func IsTypeSlice ¶
IsTypeSlice reports whether v is a slice type.
func IsTypeSliceOfInterface ¶
IsTypeSliceOfInterface reports whether v is a slice of interface.
func IsTypeSlicePtr ¶
IsTypeSlicePtr reports whether v is a slice ptr type.
func IsTypeStruct ¶
IsTypeStruct reports whether t is a struct type.
func IsTypeStructPtr ¶
IsTypeStructPtr reports whether v is a struct ptr type.
func IsUnkeyedList ¶
IsUnkeyedList reports whether e is an unkeyed list.
func IsValueInterface ¶
IsValueInterface reports whether v is an interface type.
func IsValueInterfaceToStructPtr ¶
IsValueInterfaceToStructPtr reports whether v is an interface that contains a pointer to a struct.
func IsValueNil ¶
func IsValueNil(value interface{}) bool
IsValueNil returns true if either value is nil, or has dynamic type {ptr, map, slice} with value nil.
func IsValueNilOrDefault ¶
func IsValueNilOrDefault(value interface{}) bool
IsValueNilOrDefault returns true if either IsValueNil(value) or the default value for the type.
func IsValueScalar ¶
IsValueScalar reports whether v is a scalar type.
func IsValueSlice ¶
IsValueSlice reports whether v is a slice type.
func IsValueStruct ¶
IsValueStruct reports whether v is a struct type.
func IsValueStructPtr ¶
IsValueStructPtr reports whether v is a struct ptr type.
func IsValueTypeCompatible ¶ added in v0.6.0
IsValueTypeCompatible reports whether f.Set(v) can be called successfully on a struct field f with type t. It is assumed that f is exported and addressable.
func IsYgotAnnotation ¶ added in v0.6.0
func IsYgotAnnotation(s reflect.StructField) bool
IsYgotAnnotation reports whether struct field s is an annotation field.
func MatchingNonChoiceCaseSchema ¶
MatchingNonChoiceCaseSchema returns the child schema at the given path from schema if one is found, or nil otherwise.
func PathMatchesPathElemPrefix ¶ added in v0.6.0
PathMatchesPathElemPrefix checks whether prefix is a prefix of path. Both paths must use the gNMI >=0.4.0 PathElem path format.
func PathMatchesPrefix ¶ added in v0.6.0
PathMatchesPrefix reports whether prefix is a prefix of path.
func PopGNMIPath ¶ added in v0.6.0
PopGNMIPath returns the supplied GNMI path with the first path element removed. If the path is empty, it returns an empty path.
func ResolveIfLeafRef ¶
ResolveIfLeafRef returns a ptr to the schema pointed to by the leaf-ref path in schema if it's a leafref, or schema itself if it's not.
func SchemaPaths ¶
func SchemaPaths(f reflect.StructField) ([][]string, error)
SchemaPaths returns all the paths in the path tag.
func SchemaTreeString ¶
SchemaTreeString returns the schema hierarchy tree as a string with node names and types only e.g. clock (container)
timezone (choice) timezone-name (case) timezone-name (leaf) timezone-utc-offset (case) timezone-utc-offset (leaf)
func SchemaTypeStr ¶
SchemaTypeStr returns a string representation of the type of element schema represents e.g. "container", "choice" etc.
func StripModulePrefix ¶
StripModulePrefix returns s with any prefix up to and including the last ':' character removed.
func StripModulePrefixes ¶
StripModulePrefixes returns "in" with each element with the format "A:B" changed to "B".
func StripModulePrefixesStr ¶
StripModulePrefixesStr returns "in" with each element with the format "A:B" changed to "B".
func ToString ¶
ToString returns a string representation of errors. Any nil errors in the slice are skipped.
func TrimGNMIPathElemPrefix ¶ added in v0.6.0
TrimGNMIPathElemPrefix returns the path with the prefix trimmed. It returns the original path if the prefix does not match.
func TrimGNMIPathPrefix ¶ added in v0.6.0
TrimGNMIPathPrefix returns path with the prefix trimmed. It returns the original path if the prefix does not fully match.
func UpdateField ¶
UpdateField updates a field called fieldName (which must exist, but may be nil) in parentStruct, with value fieldValue. If the field is a slice, fieldValue is appended.
func ValueStr ¶
func ValueStr(value interface{}) string
ValueStr returns a string representation of value which may be a value, ptr, or struct type.
func ValueStrDebug ¶ added in v0.6.0
func ValueStrDebug(value interface{}) string
ValueStrDebug returns "<not calculated>" if the package global variable debugLibrary is not set. Otherwise, it is the same as ValueStr. Use this function instead of ValueStr for debugging purpose, e.g. when the output is passed to DbgPrint, because ValueStr calls can be the bottleneck for large input.
func ValuesAreSameType ¶ added in v0.6.0
ValuesAreSameType returns true if v1 and v2 has the same reflect.Type, otherwise it returns false.
func YangTypeToDebugString ¶
YangTypeToDebugString returns a debug string representation of a YangType.
Types ¶
type Errors ¶
type Errors []error
Errors is a slice of error.
func AppendErr ¶
AppendErr appends err to errors if it is not nil and returns the result. If err is nil, it is not appended.
func AppendErrs ¶
AppendErrs appends newErrs to errors and returns the result. If newErrs is empty, nothing is appended.
func ForEachDataField ¶ added in v0.6.0
func ForEachDataField(value, in, out interface{}, iterFunction FieldIteratorFunc) Errors
ForEachDataField iterates the value supplied and calls the iterFunction for each data tree node found in the supplied value. No schema information is required to perform the iteration. The in and out arguments are passed to the iterFunction without inspection by this function, and can be used by the caller to store input and output during the iteration through the data tree.
func ForEachField ¶
func ForEachField(schema *yang.Entry, value interface{}, in, out interface{}, iterFunction FieldIteratorFunc) Errors
ForEachField recursively iterates through the fields of value (which may be any Go type) and executes iterFunction on each field. Any nil fields (including value) are traversed in the schema tree only. This is done to support iterations that need to detect the absence of some data item e.g. leafref. Fields that are present in value that are explicitly noted not to have a corresponding schema (e.g., annotation/metadata fields added by ygen) are skipped during traversal.
schema is the schema corresponding to value. in, out are passed to the iterator function and can be used to carry state and return results from the iterator. iterFunction is executed on each scalar field.
It returns a slice of errors encountered while processing the struct.
func NewErrs ¶
NewErrs returns a slice of error with a single element err. If err is nil, returns nil.
func PrefixErrors ¶ added in v0.6.0
PrefixErrors prefixes each error within the supplied Errors slice with the string pfx.
func UniqueErrors ¶ added in v0.6.0
UniqueErrors returns the unique errors from the supplied Errors slice. Errors are considered equal if they have equal stringified values.
type FieldIteratorFunc ¶
FieldIteratorFunc is an iteration function for arbitrary field traversals. in, out are passed through from the caller to the iteration vistior function and can be used to pass state in and out. They are not otherwise touched. It returns a slice of errors encountered while processing the field.
type NodeInfo ¶
type NodeInfo struct { // Schema is the schema for the node. Schema *yang.Entry // Path is the relative path from the parent to the current schema node. PathFromParent []string // Parent is a ptr to the containing node. Parent *NodeInfo // StructField is the StructField for the field being traversed. StructField reflect.StructField // FieldValue is the Value for the field being traversed. FieldValue reflect.Value // FieldKeys is the slice of keys in the map being traversed. nil if type // being traversed is not a map. FieldKeys []reflect.Value // FieldKey is the key of the map element being traversed. ValueOf(nil) if // type being traversed is not a map. FieldKey reflect.Value // Annotation is a field that can be populated by an iterFunction such that // context can be carried with a node throughout the iteration. Annotation []interface{} }
NodeInfo describes a node in a tree being traversed. It is passed to the iterator function supplied to a traversal driver function like ForEachField.