ytypes

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2021 License: Apache-2.0 Imports: 22 Imported by: 265

Documentation

Overview

Package ytypes implements YANG type validation logic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteNode added in v0.7.0

func DeleteNode(schema *yang.Entry, root interface{}, path *gpb.Path, opts ...DelNodeOpt) error

DeleteNode zeroes the value of the node specified by the supplied path from the specified root, whose schema must also be supplied. If the node specified by that path is already its zero value, or an intermediate node in the path is nil (implying the node is already deleted), then the call is a no-op.

func GetOrCreateNode added in v0.6.0

func GetOrCreateNode(schema *yang.Entry, root interface{}, path *gpb.Path, opts ...GetOrCreateNodeOpt) (interface{}, *yang.Entry, error)

GetOrCreateNode function retrieves the node specified by the supplied path from the root which must have the schema supplied. It strictly matches keys in the path, in other words doesn't treat partial match as match. However, if there is no match, a new entry in the map is created. GetOrCreateNode also initializes the nodes along the path if they are nil. Function returns the value and schema of the node as well as error. Note that this function may modify the supplied root even if the function fails. Note that this function may create containers or list entries even if the input path is a shadow path. TODO(wenbli): a traversal should remember what containers or list entries

were created so that a failed call or a call to a shadow path can later undo
this. This applies to SetNode as well.

func IsCaseSelected

func IsCaseSelected(schema *yang.Entry, value interface{}) (selected []string, errors []error)

IsCaseSelected reports whether a case with the given schema has been selected in the given value struct. The top level of the struct is checked, and any choices present in the schema are recursively followed to determine whether any case is selected for that choice schema subtree. It returns a slice with the names of all fields in the case that were selected.

func SetNode added in v0.6.0

func SetNode(schema *yang.Entry, root interface{}, path *gpb.Path, val interface{}, opts ...SetNodeOpt) error

SetNode sets the value of the node specified by the supplied path from the specified root, whose schema must also be supplied. It takes a set of options which can be used to specify set behaviours, such as whether or not to ensure that the node's ancestors are initialized. Note that SetNode does not do a full validation -- e.g., it does not do the string regex restriction validation done by ytypes.Validate().

func StringToType added in v0.6.0

func StringToType(t reflect.Type, s string) (reflect.Value, error)

StringToType converts given string to given type which can be one of the following; - int, int8, int16, int32, int64 - uint, uint8, uint16, uint32, uint64 - string - GoEnum type Function can be extended to support other types as well. If the given string carries an incompatible or overflowing value for the given type, function returns error. Note that castToEnumValue returns (nil, nil) if the given string is carrying an invalid enum string. Function checks not only error, but also value in this case.

func Unmarshal

func Unmarshal(schema *yang.Entry, parent interface{}, value interface{}, opts ...UnmarshalOpt) error

Unmarshal recursively unmarshals JSON data tree in value into the given parent, using the given schema. Any values already in the parent that are not present in value are preserved. If provided schema is a leaf or leaf list, parent must be referencing the parent GoStruct.

func Validate

func Validate(schema *yang.Entry, value interface{}, opts ...ygot.ValidationOption) util.Errors

Validate recursively validates the value of the given data tree struct against the given schema.

func ValidateBinaryRestrictions added in v0.10.2

func ValidateBinaryRestrictions(schemaType *yang.YangType, binaryVal []byte) error

ValidateBinaryRestrictions checks that the given binary string matches the schema's length restrictions (if any). It returns an error if the validation fails.

func ValidateDecimalRestrictions added in v0.10.2

func ValidateDecimalRestrictions(schemaType *yang.YangType, floatVal float64) error

ValidateDecimalRestrictions checks that the given decimal matches the schema's range restrictions (if any). It returns an error if the validation fails.

func ValidateIntRestrictions added in v0.10.2

func ValidateIntRestrictions(schemaType *yang.YangType, intVal int64) error

ValidateIntRestrictions checks that the given signed int matches the schema's range restrictions (if any). It returns an error if the validation fails.

func ValidateLeafRefData

func ValidateLeafRefData(schema *yang.Entry, value interface{}, opt *LeafrefOptions) util.Errors

ValidateLeafRefData traverses the entire tree with root value and the given corresponding schema. For the referring node A, the leafref will point to a value set B which may be empty. For each element in B:

  • if the element is a leaf, it checks whether A == B
  • if the element is a leaf list C, it check whether A is equal to any of the elements of C.

It returns nil if at least one equality check passes or an error otherwise. It also returns an error if any leafref points to a value outside of the tree rooted at value; therefore it should only be called on the root node of the entire data tree. The supplied LeafrefOptions specify particular behaviours of the leafref validation such as ignoring missing pointed to elements.

func ValidateStringRestrictions added in v0.10.2

func ValidateStringRestrictions(schemaType *yang.YangType, stringVal string) error

ValidateStringRestrictions checks that the given string matches the string schema's length and pattern restrictions (if any). It returns an error if the validation fails.

func ValidateUintRestrictions added in v0.10.2

func ValidateUintRestrictions(schemaType *yang.YangType, uintVal uint64) error

ValidateUintRestrictions checks that the given unsigned int matches the schema's range restrictions (if any). It returns an error if the validation fails.

Types

type Binary added in v0.7.0

type Binary []byte

Binary is a derived type which is used to represent the YANG binary type.

type CustomValidationOptions added in v0.8.7

type CustomValidationOptions struct {
	// FakeRootCustomValidate specifies the user implemented method
	FakeRootCustomValidate func(ygot.GoStruct) error
}

CustomValidationOptions controls the custom validate function to be invoked on the root

func (*CustomValidationOptions) IsValidationOption added in v0.8.7

func (*CustomValidationOptions) IsValidationOption()

IsValidationOption ensures that CustomValidationOptions implements the ValidationOption interface.

type DelNodeOpt added in v0.10.11

type DelNodeOpt interface {
	// IsDelNodeOpt is a marker method that is used to identify an instance of DelNodeOpt.
	IsDelNodeOpt()
}

DelNodeOpt defines an interface that can be used to supply arguments to functions using DeleteNode.

type Encoding added in v0.6.0

type Encoding int

Encoding specifies how the value provided to UnmarshalGeneric function is encoded.

const (
	// JSONEncoding indicates that provided value is JSON encoded.
	JSONEncoding Encoding = iota

	// GNMIEncoding indicates that provided value is gNMI TypedValue.
	GNMIEncoding
)

type GetHandleWildcards added in v0.6.0

type GetHandleWildcards struct{}

GetHandleWildcards specifies that a match within GetNode should be allowed to use wildekarts.

func (*GetHandleWildcards) IsGetNodeOpt added in v0.6.0

func (*GetHandleWildcards) IsGetNodeOpt()

IsGetNodeOpt implements the GetNodeOpt interface.

type GetNodeOpt added in v0.6.0

type GetNodeOpt interface {
	// IsGetNodeOpt is a marker method that is used to identify an instance of GetNodeOpt.
	IsGetNodeOpt()
}

GetNodeOpt defines an interface that can be used to supply arguments to functions using GetNode.

type GetOrCreateNodeOpt added in v0.10.11

type GetOrCreateNodeOpt interface {
	// IsGetOrCreateNodeOpt is a marker method that is used to identify an instance of GetOrCreateNodeOpt.
	IsGetOrCreateNodeOpt()
}

GetOrCreateNodeOpt defines an interface that can be used to supply arguments to functions using GetOrCreateNode.

type GetPartialKeyMatch added in v0.6.0

type GetPartialKeyMatch struct{}

GetPartialKeyMatch specifies that a match within GetNode should be allowed to partially match keys for list entries.

func (*GetPartialKeyMatch) IsGetNodeOpt added in v0.6.0

func (*GetPartialKeyMatch) IsGetNodeOpt()

IsGetNodeOpt implements the GetNodeOpt interface.

type IgnoreExtraFields added in v0.6.0

type IgnoreExtraFields struct{}

IgnoreExtraFields is an unmarshal option that controls the behaviour of the Unmarshal function when additional fields are found in the input JSON. By default, an error will be returned, by specifying the IgnoreExtraFields option to Unmarshal, extra fields will be discarded.

func (*IgnoreExtraFields) IsUnmarshalOpt added in v0.6.0

func (*IgnoreExtraFields) IsUnmarshalOpt()

IsUnmarshalOpt marks IgnoreExtraFields as a valid UnmarshalOpt.

type InitMissingElements added in v0.6.0

type InitMissingElements struct{}

InitMissingElements signals SetNode to initialize the node's ancestors and to ensure that keys are added into keyed lists(maps) if they are missing, before updating the node.

func (*InitMissingElements) IsSetNodeOpt added in v0.6.0

func (*InitMissingElements) IsSetNodeOpt()

IsSetNodeOpt implements the SetNodeOpt interface.

type LeafrefOptions

type LeafrefOptions struct {
	// IgnoreMissingData determines whether leafrefs that target a node
	// that does not exist should return an error to the calling application. When
	// set to true, no error is returned.
	//
	// This functionality is typically used where a partial set of schema information
	// is populated, but validation is required - for example, configuration for
	// a protocol within OpenConfig references an interface, but the schema being
	// validated does not contain the interface definitions.
	IgnoreMissingData bool
	// Log specifies whether log entries should be created where a leafref
	// cannot be successfully resolved.
	Log bool
}

LeafrefOptions controls the behaviour of validation functions for leaf-ref data types.

func (*LeafrefOptions) IsValidationOption

func (*LeafrefOptions) IsValidationOption()

IsValidationOption ensures that LeafrefOptions implements the ValidationOption interface.

type PreferShadowPath added in v0.11.0

type PreferShadowPath struct{}

PreferShadowPath signals to prefer using the "shadow-path" tags instead of the "path" tags when both are present while processing a GoStruct. This means paths matching "shadow-path" will be unmarshalled, while paths matching "path" will be silently ignored.

func (*PreferShadowPath) IsDelNodeOpt added in v0.11.0

func (*PreferShadowPath) IsDelNodeOpt()

IsDelNodeOpt implements the DelNodeOpt interface.

func (*PreferShadowPath) IsGetNodeOpt added in v0.11.0

func (*PreferShadowPath) IsGetNodeOpt()

IsGetNodeOpt implements the GetNodeOpt interface.

func (*PreferShadowPath) IsGetOrCreateNodeOpt added in v0.11.0

func (*PreferShadowPath) IsGetOrCreateNodeOpt()

IsGetOrCreateNodeOpt implements the GetOrCreateNodeOpt interface.

func (*PreferShadowPath) IsSetNodeOpt added in v0.11.0

func (*PreferShadowPath) IsSetNodeOpt()

IsSetNodeOpt implements the SetNodeOpt interface.

type Schema added in v0.6.0

type Schema struct {
	Root       ygot.ValidatedGoStruct // Root is the ValidatedGoStruct that acts as the root for a schema, it is nil if there is no generated fakeroot.
	SchemaTree map[string]*yang.Entry // SchemaTree is the extracted schematree for the generated schema.
	Unmarshal  UnmarshalFunc          // Unmarshal is a function that can unmarshal RFC7951 JSON into the specified Root type.
}

Schema specifies the common types that are part of a generated ygot schema, such that it can be referenced and handled in calling application code.

func (*Schema) IsValid added in v0.6.0

func (s *Schema) IsValid() bool

IsValid determines whether all required fields of the UnmarshalIETFJSON struct have been populated.

func (*Schema) RootSchema added in v0.6.0

func (s *Schema) RootSchema() *yang.Entry

RootSchema returns the YANG entry schema corresponding to the type of the root within the schema.

type SetNodeOpt added in v0.6.0

type SetNodeOpt interface {
	// IsSetNodeOpt is a marker method that is used to identify an instance of SetNodeOpt.
	IsSetNodeOpt()
}

SetNodeOpt defines an interface that can be used to supply arguments to functions using SetNode.

type TolerateJSONInconsistencies added in v0.7.0

type TolerateJSONInconsistencies struct{}

TolerateJSONInconsistencies signals SetNode to tolerate inconsistencies for val as if it were converted from JSON. As of right now, this is specifically to deal with uint values being streamed as positive int values.

func (*TolerateJSONInconsistencies) IsSetNodeOpt added in v0.7.0

func (*TolerateJSONInconsistencies) IsSetNodeOpt()

IsSetNodeOpt implements the SetNodeOpt interface.

type TreeNode added in v0.6.0

type TreeNode struct {
	// Schema is the schema entry for the data tree node, specified as a goyang Entry struct.
	Schema *yang.Entry
	// Data is the data node found at the path.
	Data interface{}
	// Path is the path of the data node that is being returned.
	Path *gpb.Path
}

TreeNode wraps an individual entry within a YANG data tree to return to a caller.

func GetNode added in v0.6.0

func GetNode(schema *yang.Entry, root interface{}, path *gpb.Path, opts ...GetNodeOpt) ([]*TreeNode, error)

GetNode retrieves the node specified by the supplied path from the specified root, whose schema must also be supplied. It takes a set of options which can be used to specify get behaviours, such as allowing partial match. If there are no matches for the path, an error is returned.

type UnmarshalFunc added in v0.6.0

type UnmarshalFunc func([]byte, ygot.GoStruct, ...UnmarshalOpt) error

UnmarshalFunc defines a common signature for an RFC7951 to GoStruct unmarshalling function

type UnmarshalOpt added in v0.6.0

type UnmarshalOpt interface {
	IsUnmarshalOpt()
}

UnmarshalOpt is an interface used for any option to be supplied to the Unmarshal function. Types implementing it can be used to control the behaviour of JSON unmarshalling.

type YANGEmpty added in v0.6.0

type YANGEmpty bool

YANGEmpty is a derived type which is used to represent the YANG empty type.

Directories

Path Synopsis
Package validate is used for testing with the default OpenConfig generated structs.
Package validate is used for testing with the default OpenConfig generated structs.

Jump to

Keyboard shortcuts

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