Documentation ¶
Overview ¶
Package ypathgen contains a library to generate gNMI paths from a YANG model. The ygen library is used to parse YANG and obtain intermediate and some final information. The output always assumes the OpenConfig-specific conventions for a compressed schema.
TODO(wenbli): This package is written with only compressed schemas in mind. If needed, can write tests, verify, and enhance it to support uncompressed ygen structs.
Index ¶
Constants ¶
const ( // WildcardSuffix is the suffix given to the wildcard versions of each // node as well as a list's wildcard child constructor methods that // distinguishes each from its non-wildcard counterpart. WildcardSuffix = "Any" // BuilderCtorSuffix is the suffix applied to the list builder // constructor method's name in order to indicate itself to the user. BuilderCtorSuffix = "Any" // BuilderKeyPrefix is the prefix applied to the key-modifying builder // method for a list PathStruct that uses the builder API. // NOTE: This cannot be "", as the builder method name would conflict // with the child constructor method for the keys. BuilderKeyPrefix = "With" )
Static default configuration values that differ from the zero value for their types.
Variables ¶
This section is empty.
Functions ¶
func GetOrderedNodeDataNames ¶
func GetOrderedNodeDataNames(nodeDataMap NodeDataMap) []string
GetOrderedNodeDataNames returns the alphabetically-sorted slice of keys (path struct names) for a given NodeDataMap.
Types ¶
type GenConfig ¶
type GenConfig struct { // PackageName is the name that should be used for the generating package. PackageName string // GoImports contains package import options. GoImports GoImports // PreferOperationalState generates path-build methods for only the // "state" version of a field when it exists under both "config" and // "state" containers of its parent YANG model. If it is false, then // the reverse is true. There are no omissions if a conflict does not // exist, e.g. if a leaf exists only under a "state" container, then // its path-building method will always be generated, and use "state". PreferOperationalState bool // FakeRootName specifies the name of the struct that should be generated // representing the root. FakeRootName string // PathStructSuffix is the suffix to be appended to generated // PathStructs to distinguish them from the generated GoStructs, which // assume a similar name. PathStructSuffix string // SkipEnumDeduplication specifies whether leaves of type 'enumeration' that // are used in multiple places in the schema should share a common type within // the generated code that is output by ygen. By default (false), a common type // is used. // This is the same flag used by ygen: they must match for pathgen's // generated code to be compatible with it. SkipEnumDeduplication bool // ShortenEnumLeafNames removes the module name from the name of // enumeration leaves. // This is the same flag used by ygen: they must match for pathgen's // generated code to be compatible with it. ShortenEnumLeafNames bool // UseDefiningModuleForTypedefEnumNames uses the defining module name // to prefix typedef enumerated types instead of the module where the // typedef enumerated value is used. // This is the same flag used by ygen: they must match for pathgen's // generated code to be compatible with it. UseDefiningModuleForTypedefEnumNames bool // ExcludeModules specifies any modules that are included within the set of // modules that should have code generated for them that should be ignored during // code generation. This is due to the fact that some schemas (e.g., OpenConfig // interfaces) currently result in overlapping entities (e.g., /interfaces). ExcludeModules []string // YANGParseOptions provides the options that should be handed to the // github.com/openconfig/goyang/pkg/yang library. These specify how the // input YANG files should be parsed. YANGParseOptions yang.Options // GeneratingBinary is the name of the binary calling the generator library, it is // included in the header of output files for debugging purposes. If a // string is not specified, the location of the library is utilised. GeneratingBinary string // ListBuilderKeyThreshold means to use the builder API format instead // of the key-combination API format for constructing list keys when // the number of keys is at least the threshold value. // 0 (default) means no threshold, i.e. always use the key-combination // API format. ListBuilderKeyThreshold uint }
GenConfig stores code generation configuration.
func NewDefaultConfig ¶
NewDefaultConfig creates a GenConfig with default configuration. schemaStructPkgPath is a required configuration parameter. It should be set to "" when the generated PathStruct package is to be the same package as the GoStructs package.
func (*GenConfig) GeneratePathCode ¶
func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (*GeneratedPathCode, NodeDataMap, util.Errors)
GeneratePathCode takes a slice of strings containing the path to a set of YANG files which contain YANG modules, and a second slice of strings which specifies the set of paths that are to be searched for associated models (e.g., modules that are included by the specified set of modules, or submodules of those modules). It extracts the set of modules that are to be generated, and returns a pointer to a GeneratedPathCode struct containing all the generated code to support the path-creation API. The important components of the generated code are listed below:
- Struct definitions for each container, list, or leaf schema node, as well as the fakeroot.
- Next-level methods for the fakeroot and each non-leaf schema node, which instantiate and return the next-level structs corresponding to its child schema nodes.
With these components, the generated API is able to support absolute path creation of any node of the input schema. Also returned is the NodeDataMap of the schema, i.e. information about each node in the generated code, which may help callers add customized augmentations to the basic generated path code. If errors are encountered during code generation, they are returned.
type GeneratedPathCode ¶
type GeneratedPathCode struct { Structs []GoPathStructCodeSnippet // Structs is the generated set of structs representing containers or lists in the input YANG models. CommonHeader string // CommonHeader is the header that should be used for all output Go files. }
GeneratedPathCode contains generated code snippets that can be processed by the calling application. The generated code is divided into two types of objects - both represented as a slice of strings: Structs contains a set of Go structures that have been generated, and Enums contains the code for generated enumerated types (corresponding to identities, or enumerated values within the YANG models for which code is being generated). Additionally the header with package comment of the generated code is returned in Header, along with the a slice of strings containing the packages that are required for the generated Go code to be compiled is returned.
For schemas that contain enumerated types (identities, or enumerations), a code snippet is returned as the EnumMap field that allows the string values from the YANG schema to be resolved. The keys of the map are strings corresponding to the name of the generated type, with the map values being maps of the int64 identifier for each value of the enumeration to the name of the element, as used in the YANG schema.
func (GeneratedPathCode) SplitFiles ¶
func (genCode GeneratedPathCode) SplitFiles(fileN int) ([]string, error)
SplitFiles returns a slice of strings, each representing a file that together contains the entire generated code. fileN specifies the number of files to split the code into, and has to be between 1 and the total number of directory entries in the input schema. By splitting, the size of the output files can be roughly controlled.
func (GeneratedPathCode) String ¶
func (genCode GeneratedPathCode) String() string
String method for GeneratedPathCode, which can be used to write all the generated code into a single file.
type GoImports ¶
type GoImports struct { // SchemaStructPkgPath specifies the path to the ygen-generated structs, which // is used to get the enum and union type names used as the list key // for calling a list path accessor. SchemaStructPkgPath string // YgotImportPath specifies the path to the ygot library that should be used // in the generated code. YgotImportPath string }
GoImports contains package import options.
type GoPathStructCodeSnippet ¶
type GoPathStructCodeSnippet struct { // PathStructName is the name of the struct that is contained within the snippet. // It is stored such that callers can identify the struct to control where it // is output. PathStructName string // StructBase stores the basic code snippet that represents the struct that is // the input when code generation is performed, which includes its definition. StructBase string // ChildConstructors contains the method code snippets with the input struct as a // receiver, that is used to get the child path struct. ChildConstructors string }
GoPathStructCodeSnippet is used to store the generated code snippets associated with a particular Go struct entity (corresponding to a container, list, or leaf in the schema).
func (GoPathStructCodeSnippet) String ¶
func (g GoPathStructCodeSnippet) String() string
String returns the contents of a GoPathStructCodeSnippet as a string by simply writing out all of its generated code.
type NodeData ¶
type NodeData struct { // GoTypeName is the type name of the node as part of the schema // structs, which is qualified by the SchemaStructPkgAlias if // necessary. It could be a GoStruct or a leaf type. GoTypeName string // GoFieldName is the field name of the node under its parent struct. GoFieldName string // SubsumingGoStructName is the GoStruct type name corresponding to the node. If // the node is a leaf, then it is the parent GoStruct's name. SubsumingGoStructName string // IsLeaf indicates whether this child is a leaf node. IsLeaf bool // IsScalarField indicates a leaf that is stored as a pointer in its // parent struct. IsScalarField bool // YANGTypeName is the type of the leaf given in the YANG file (without // the module prefix, if any, per goyang behaviour). If the node is not // a leaf this will be empty. Note that the current purpose for this is // to allow callers to handle certain types as special cases, but since // the name of the node is a very basic piece of information which // excludes the defining module, this is somewhat hacky, so it may be // removed or modified in the future. YANGTypeName string // YANGPath is the schema path of the YANG node. YANGPath string }
NodeData contains information about the ygen-generated code of a YANG schema node.
type NodeDataMap ¶
NodeDataMap is a map from the path struct type name of a schema node to its NodeData.
Directories ¶
Path | Synopsis |
---|---|
Package pathtest tests the path generation library (ypathgen) using a real OpenConfig YANG schema.
|
Package pathtest tests the path generation library (ypathgen) using a real OpenConfig YANG schema. |