ygen

package
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: Apache-2.0 Imports: 23 Imported by: 24

Documentation

Overview

Package ygen contains a library to generate Go structs from a YANG model. The Goyang parsing library is used to parse YANG. The output can consider OpenConfig-specific conventions such that the schema is compressed.

Index

Constants

View Source
const (
	// DefaultBasePackageName defines the default base package that is
	// generated when generating proto3 code.
	DefaultBasePackageName = "openconfig"
	// DefaultEnumPackageName defines the default package name that is
	// used for the package that defines enumerated types that are
	// used throughout the schema.
	DefaultEnumPackageName = "enums"
	// DefaultYwrapperPath defines the default import path for the ywrapper.proto file,
	// excluding the filename.
	DefaultYwrapperPath = "github.com/openconfig/ygot/proto/ywrapper"
	// DefaultYextPath defines the default import path for the yext.proto file, excluding
	// the filename.
	DefaultYextPath = "github.com/openconfig/ygot/proto/yext"
)

Constants defining the defaults for Protobuf package generation. These constants can be referred to by calling applications as defaults that are presented to a user.

View Source
const (

	// DefaultAnnotationPrefix is the default string that is used to prefix the name
	// of metadata fields in the output Go structs.
	DefaultAnnotationPrefix string = "Λ"
)

Variables

This section is empty.

Functions

func BytesToGoByteSlice

func BytesToGoByteSlice(b []byte) []string

BytesToGoByteSlice takes an input slice of bytes and outputs it as a slice of strings corresponding to lines of Go source code that define the byte slice. Each string within the slice contains up to 16 bytes of the output byte array, with each byte represented as a two digit hex character.

func FindSchemaPath added in v0.7.0

func FindSchemaPath(parent *Directory, fieldName string, absolutePaths bool) ([]string, error)

FindSchemaPath finds the relative or absolute schema path of a given field of a Directory. The Field is specified as a name in order to guarantee its existence before processing.

func GetOrderedDirectories added in v0.7.0

func GetOrderedDirectories(directory map[string]*Directory) ([]string, map[string]*Directory, error)

GetOrderedDirectories returns an alphabetically-ordered slice of Directory names and a map of Directories keyed by their names instead of their paths, so that each directory can be processed in alphabetical order. This helps produce deterministic generated code, and minimize diffs when compared with expected output (i.e., diffs don't appear simply due to reordering of the Directory maps). If the names of the directories are not unique, which is unexpected, an error is returned.

func GetOrderedFieldNames added in v0.7.0

func GetOrderedFieldNames(directory *Directory) []string

GetOrderedFieldNames returns the field names of a Directory in alphabetical order.

func GoFieldNameMap added in v0.7.0

func GoFieldNameMap(directory *Directory) map[string]string

GoFieldNameMap returns a map containing the Go name for a field (key is the field schema name). Camelcase and uniquification is done to ensure compilation. Naming uniquification is done deterministically.

func IsFakeRoot added in v0.7.0

func IsFakeRoot(e *yang.Entry) bool

IsFakeRoot checks whether a given entry is the generated fake root.

func IsScalarField added in v0.7.0

func IsScalarField(field *yang.Entry, t *MappedType) bool

IsScalarField determines which fields should be converted to pointers when outputting structs; this is done to allow checks against nil.

func IsYgenDefinedGoType added in v0.7.0

func IsYgenDefinedGoType(t *MappedType) bool

IsYgenDefinedGoType returns true if the native type of a MappedType is a Go type that's defined by ygen's generated code.

func MakeFakeRoot added in v0.7.0

func MakeFakeRoot(rootName string) *yang.Entry

MakeFakeRoot creates and returns a fakeroot *yang.Entry with rootName as its name. It has an empty, but initialized Dir.

func WriteGzippedByteSlice

func WriteGzippedByteSlice(b []byte) ([]byte, error)

WriteGzippedByteSlice takes an input slice of bytes, gzips it and returns the resulting compressed output as a byte slice.

Types

type DirType added in v0.8.4

type DirType int64

DirType describes the different types of Directory that can be output within the IR such that 'list' directories can have special handling applied.

const (

	// Container represents a YANG 'container'.
	Container DirType
	// List represents a YANG 'list'.
	List
)

type Directory added in v0.7.0

type Directory struct {
	Name           string                 // Name is the name of the struct to be generated.
	Entry          *yang.Entry            // Entry is the yang.Entry that corresponds to the schema element being converted to a struct.
	Fields         map[string]*yang.Entry // Fields is a map, keyed by the YANG node identifier, of the entries that are the struct fields.
	ShadowedFields map[string]*yang.Entry // ShadowedFields is a map, keyed by the YANG node identifier, of the field entries duplicated via compression.
	Path           []string               // Path is a slice of strings indicating the element's path.
	ListAttr       *YangListAttr          // ListAttr is used to store characteristics of structs that represent YANG lists.
	IsFakeRoot     bool                   // IsFakeRoot indicates that the struct is a fake root struct, so specific mapping rules should be implemented.
}

Directory stores information needed for outputting a data node of the generated code. When viewed as a collection of entries that is generated from an entire YANG schema, they serve the purpose of mapping the YANG schema tree to a directory tree (connected through implicit yang.Entry edges) where each directory corresponds to a data node of the Go version of the schema, and where digested data is stored that is friendly to the code generation algorithm.

type DirectoryGenConfig added in v0.7.0

type DirectoryGenConfig struct {
	// ParseOptions contains parsing options for a given set of schema files.
	ParseOptions ParseOpts
	// TransformationOptions contains options for how the generated code
	// may be transformed from a simple 1:1 mapping with respect to the
	// given YANG schema.
	TransformationOptions TransformationOpts
	// GoOptions stores a struct which stores Go code generation specific
	// options for the code generaton.
	GoOptions GoOpts
}

DirectoryGenConfig contains the configuration necessary to generate a set of Directory objects for a given schema. The set of Directory objects is the intermediate representation generated by ygen, which can be useful for external code generation libraries which can make use of it.

func (*DirectoryGenConfig) GetDirectoriesAndLeafTypes added in v0.7.0

func (dcg *DirectoryGenConfig) GetDirectoriesAndLeafTypes(yangFiles, includePaths []string) (map[string]*Directory, map[string]map[string]*MappedType, util.Errors)

GetDirectoriesAndLeafTypes parses YANG files and returns two path-keyed maps. The first contains Directory entries that is the intermediate representation used by ygen for subsequent code generation, and the second contains the *MappedType for each field of the same corresponding Directory entries, with non-leafs having a nil value. This representation may be useful to external code generation libraries that make use of such information, e.g. if their output code depends on a ygen-generated type. yangFiles is a slice of strings containing the path to a set of YANG files which contain YANG modules, includePaths is 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). Any errors encountered during code generation are returned.

type EnumeratedValueType added in v0.8.4

type EnumeratedValueType int64

EnumeratedValueType is used to indicate the source YANG type that an enumeration was generated based on.

const (

	// SimpleEnumerationType represents 'enumeration' leaves within
	// the YANG schema.
	SimpleEnumerationType EnumeratedValueType
	// DerivedEnumerationType represents enumerations that are within
	// a YANG 'typedef'
	DerivedEnumerationType
	// UnionEnumerationType represents a 'type enumeration' within
	// a union.
	UnionEnumerationType
	// DerivedUnionEnumerationType represents a 'enumeration' within
	// a union that is itself within a typedef.
	DerivedUnionEnumerationType
	// IdentityType represents an enumeration that is an 'identity'
	// within the YANG schema.
	IdentityType
)

type EnumeratedYANGType added in v0.8.4

type EnumeratedYANGType struct {
	// Name is the name of the generated enumeration to be
	// used in the generated code.
	Name string
	// Kind indicates the type of enumerated value that the
	// EnumeratedYANGType represents - allowing for a code
	// generation mechanism to select how different enumerated
	// value types are output.
	Kind EnumeratedValueType
	// ValuePrefix stores any prefix that has been annotated by the IR generation
	// that specifies what prefix should be appended to value names within the type.
	ValuePrefix []string
	// TypeName stores the original YANG type name for the enumeration.
	TypeName string

	// ValToCodeName stores the mapping between the int64
	// value for the enumeration, and its language-specific
	// name.
	ValToCodeName map[int64]string
	// ValToYANGDetails stores the mapping between the
	// int64 identifier for the enumeration value and its
	// YANG-specific details (as defined by the ygot.EnumDefinition).
	ValToYANGDetails map[int64]*ygot.EnumDefinition
}

EnumeratedYANGType is an abstract representation of an enumerated type to be produced in the output code.

type GeneratedGoCode

type GeneratedGoCode struct {
	Structs      []GoStructCodeSnippet // Structs is the generated set of structs representing containers or lists in the input YANG models.
	Enums        []string              // Enums is the generated set of enum definitions corresponding to identities and enumerations in the input YANG models.
	CommonHeader string                // CommonHeader is the header that should be used for all output Go files.
	OneOffHeader string                // OneOffHeader defines the header that should be included in only one output Go file - such as package init statements.
	EnumMap      string                // EnumMap is a Go map that allows the YANG string values of enumerated types to be resolved.
	// JSONSchemaCode contains code defining a variable storing a serialised JSON schema for the
	// generated Go structs. When deserialised it consists of a map[string]*yang.Entry. The
	// entries are the root level yang.Entry definitions along with their corresponding
	// hierarchy (i.e., the yang.Entry for /foo contains /foo/... - all of foo's descendents).
	// Each yang.Entry which corresponds to a generated Go struct has two extra fields defined:
	//  - schemapath - the path to this entry within the schema. This is provided since the Path() method of
	//                 the deserialised yang.Entry does not return the path since the Parent pointer is not
	//                 populated.
	//  - structname - the name of the struct that was generated for the schema element.
	JSONSchemaCode string
	// RawJSONSchema stores the JSON document which is serialised and stored in JSONSchemaCode.
	// It is populated only if the StoreRawSchema YANGCodeGenerator boolean is set to true.
	RawJSONSchema []byte
	// EnumTypeMap is a Go map that allows YANG schemapaths to be mapped to reflect.Type values.
	EnumTypeMap string
}

GeneratedGoCode 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.

type GeneratedProto3

type GeneratedProto3 struct {
	// Packages stores a map, keyed by the Protobuf package name, and containing the contents of the protobuf3
	// messages defined within the package. The calling application can write out the defined packages to the
	// files expected by the protoc tool.
	Packages map[string]Proto3Package
}

GeneratedProto3 stores a set of generated Protobuf packages.

type GeneratorConfig

type GeneratorConfig struct {
	// PackageName is the name that should be used for the generating package.
	PackageName string
	// Caller 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.
	Caller string
	// GenerateJSONSchema stores a boolean which defines whether to generate
	// the JSON corresponding to the YANG schema parsed to generate the
	// output code.
	GenerateJSONSchema bool
	// StoreRawSchema the raw JSON schema should be returned by the code
	// generation function, such that it can be handled by an external
	// library.
	StoreRawSchema bool
	// ParseOptions contains parsing options for a given set of schema files.
	ParseOptions ParseOpts
	// TransformationOptions contains options for how the generated code
	// may be transformed from a simple 1:1 mapping with respect to the
	// given YANG schema.
	TransformationOptions TransformationOpts
	// GoOptions stores a struct which stores Go code generation specific
	// options for the code generaton.
	GoOptions GoOpts
	// ProtoOptions stores a struct which contains Protobuf specific options.
	ProtoOptions ProtoOpts
	// IncludeDescriptions specifies that YANG entry descriptions are added
	// to the JSON schema. Is false by default, to reduce the size of generated schema
	IncludeDescriptions bool
}

GeneratorConfig stores the configuration options used for code generation.

type GoOpts

type GoOpts struct {
	// SchemaVarName is the name for the variable which stores the compressed
	// JSON schema in the generated Go code. JSON schema output is only
	// produced if the GenerateJSONSchema YANGCodeGenerator field is set to
	// true.
	SchemaVarName string
	// GoyangImportPath specifies the path that should be used in the generated
	// code for importing the goyang/pkg/yang package.
	GoyangImportPath string
	// YgotImportPath specifies the path to the ygot library that should be used
	// in the generated code.
	YgotImportPath string
	// YtypesImportPath specifies the path to ytypes library that should be used
	// in the generated code.
	YtypesImportPath string
	// GenerateRenameMethod specifies whether methods for renaming list entries
	// should be generated in the output Go code.
	GenerateRenameMethod bool
	// AddAnnotationFields specifies whether annotation fields should be added to
	// the generated structs. When set to true, a metadata field is added for each
	// struct, and for each field of each struct. Metadata field's names are
	// prefixed by the string specified in the AnnotationPrefix argument.
	AddAnnotationFields bool
	// AnnotationPrefix specifies the string which is prefixed to the name of
	// annotation fields. It defaults to Λ.
	AnnotationPrefix string
	// GenerateGetters specifies whether GetOrCreate* methods should be created
	// for struct pointer (YANG container) and map (YANG list) fields of generated
	// structs.
	GenerateGetters bool
	// GenerateDeleteMethod specifies whether Delete* methods should be created for
	// map (YANG list) fields of generated structs.
	GenerateDeleteMethod bool
	// GenerateAppendList specifies whether Append* methods should be created for
	// list fields of a struct. These methods take an input list member type, extract
	// the key and append the supplied value to the list.
	GenerateAppendMethod bool
	// GenerateSimpleUnions specifies whether simple typedefs are used to
	// represent union subtypes in the generated code instead of using
	// wrapper types.
	GenerateSimpleUnions bool
	// GenerateLeafGetters specifies whether Get* methods should be created for
	// leaf fields of a struct. Care should be taken with this option since a Get
	// method returns the *Go* zero value for a particular entity if the field is
	// unset. This means that it is not possible for a caller of method to know
	// whether a field has been explicitly set to the zero value (i.e., an integer
	// field is set to 0), or whether the field was actually unset.
	GenerateLeafGetters bool
	// GeneratePopulateDefault specifies whether a PopulateDefaults method
	// should be generated for every GoStruct that recursively populates
	// default values within the subtree.
	GeneratePopulateDefault bool
	// GNMIProtoPath specifies the path to the generated gNMI protobuf, which
	// is used to store the catalogue entries for generated modules.
	GNMIProtoPath string
	// IncludeModelData specifies whether gNMI ModelData messages should be generated
	// in the output code.
	IncludeModelData bool
	// AppendEnumSuffixForSimpleUnionEnums appends an "Enum" suffix to the
	// enumeration name for simple (i.e. non-typedef) leaves which are
	// unions with an enumeration inside. This makes all inlined
	// enumerations within unions, whether typedef or not, have this
	// suffix, achieving consistency. Since this flag is planned to be a
	// v1 compatibility flag along with
	// UseDefiningModuleForTypedefEnumNames, and will be removed in v1, it
	// only applies when useDefiningModuleForTypedefEnumNames is also set
	// to true.
	AppendEnumSuffixForSimpleUnionEnums bool
}

GoOpts stores Go specific options for the code generation library.

type GoStructCodeSnippet added in v0.6.0

type GoStructCodeSnippet struct {
	// StructName 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.
	StructName string
	// StructDef stores the code snippet that represents the struct that is
	// the input when code generation is performed.
	StructDef string
	// ListKeys stores code snippets that are associated with structs that are
	// generated to represent the keys of multi-key lists. In the case that the
	// Go struct for which the code is being generated does not contain a list
	// with multiple keys, this string is empty.
	ListKeys string
	// Methods contains code snippsets that represent functions that have the
	// input struct as a receiver, that help the user create new entries within
	// lists, without needing to populate the keys of the list.
	Methods string
	// Interfaces contains code snippets that represent interfaces that are
	// used within the generated struct. Used when there are interfaces that
	// represent multi-type unions generated.
	Interfaces string
	// contains filtered or unexported fields
}

GoStructCodeSnippet is used to store the generated code snippets associated with a particular Go struct entity (generated from a container or list).

func (GoStructCodeSnippet) String added in v0.6.0

func (g GoStructCodeSnippet) String() string

String returns the contents of the receiver GoStructCodeSnippet as a string.

type IR added in v0.8.4

type IR struct {
	// Directories is the set of 'directory' entries that are to be produced
	// in the generated code.
	Directories map[string]*ParsedDirectory

	// Enums is the set of enumerated entries that are to be output in the
	// generated language code.
	Enums map[string]*EnumeratedYANGType
}

IR represents the returned intermediate representation produced by ygen.

func GenerateIR added in v0.8.4

func GenerateIR(yangFiles, includePaths []string, newLangMapper NewLangMapperFn, opts IROptions) (*IR, error)

GenerateIR creates the ygen intermediate representation for a set of YANG modules. The YANG files to be parsed are read from the yangFiles argument, with any includes that they use searched for in the string slice of paths specified by includePaths. The supplier LangMapper interface is used to perform mapping of language-specific naming whilst creating the IR -- the full details of the implementation of LangMapper can be found in ygen/ir.go and docs/code-generation-design.md.

The supplied IROptions controls the parsing and transformations that are applied to the schema whilst generating the IR.

GenerateIR returns the complete ygen intermediate representation.

type IROptions added in v0.8.4

type IROptions struct {
	// ParseOptions specifies the options for how the YANG schema is
	// produced.
	ParseOptions ParseOpts

	// Transformation options specifies any transformations that should
	// be applied to the input YANG schema when producing the IR.
	TransformationOptions TransformationOpts
}

type LangMapper added in v0.8.4

type LangMapper interface {
	// FieldName maps an input yang.Entry to the name that should be used
	// in the intermediate representation. It is called for each field of
	// a defined directory.
	FieldName(e *yang.Entry) (string, error)

	// DirectoryName maps an input yang.Entry to the name that should be used in the
	// intermediate representation (IR). It is called for any directory entity that
	// is to be output in the generated code.
	DirectoryName(*yang.Entry, genutil.CompressBehaviour) (string, error)

	// KeyLeafType maps an input yang.Entry which must represent a leaf to the
	// type that should be used when the leaf is used in the context of a
	// list key within the output IR.
	KeyLeafType(*yang.Entry, genutil.CompressBehaviour) (*MappedType, error)

	// LeafType maps an input yang.Entry which must represent a leaf to the
	// type that should be used when the leaf is used in the context of a
	// field within a directory within the output IR.
	LeafType(*yang.Entry, genutil.CompressBehaviour) (*MappedType, error)

	// EnumeratedValueName maps an input string representing an enumerated
	// value to a language-safe name for the enumerated value. This function
	// should ensure that the returned string is sanitised to ensure that
	// it can be directly output in the generated code.
	EnumeratedValueName(string) (string, error)

	// EnumeratedTypePrefix specifies a prefix that should be used as a
	// prefix to types that are mapped from the YANG schema. The prefix
	// is applied only to the type name - and not to the values within
	// the enumeration.
	EnumeratedTypePrefix() string

	// EnumerationsUseUnderscores specifies whether enumeration names
	// should use underscores between path segments.
	EnumerationsUseUnderscores() bool

	// SetEnumSet is used to supply a set of enumerated values to the
	// mapper such that leaves that have enumerated types can be looked up.
	// An enumSet provides lookup methods that allow:
	//  - simple enumerated types
	//  - identityrefs
	//  - enumerations within typedefs
	//  - identityrefs within typedefs
	// to be resolved to the corresponding type that is to be used in
	// the IR.
	SetEnumSet(*enumSet)

	// SetSchemaTree is used to supply a copy of the YANG schema tree to
	// the mapped such that leaves of type leafref can be resolved to
	// their target leaves.
	SetSchemaTree(*schemaTree)
}

LangMapper is the interface to be implemented by a language-specific library and provided as an input to the IR production phase of ygen.

type MappedType added in v0.7.0

type MappedType struct {
	// NativeType is the type which is to be used for the mapped entity.
	NativeType string
	// UnionTypes is a map, keyed by the generated type name, of the types
	// specified as valid for a union. The value of the map indicates the
	// order of the type, since order is important for unions in YANG.
	// Where two types are mapped to the same generated language type
	// (e.g., string) then only the order of the first is maintained. Since
	// the generated code from the structs maintains only type validation,
	// this is not currently a limitation.
	UnionTypes map[string]int
	// IsEnumeratedValue specifies whether the NativeType that is returned
	// is a generated enumerated value. Such entities are reflected as
	// derived types with constant values, and are hence not represented
	// as pointers in the output code.
	IsEnumeratedValue bool
	// ZeroValue stores the value that should be used for the type if
	// it is unset. This is used only in contexts where the nil pointer
	// cannot be used, such as leaf getters.
	ZeroValue string
	// DefaultValue stores the default value for the type if is specified.
	// It is represented as a string pointer to ensure that default values
	// of the empty string can be distinguished from unset defaults.
	DefaultValue *string
}

MappedType is used to store the generated language type that a leaf entity in YANG is mapped to. The NativeType is always populated for any leaf. UnionTypes is populated when the type may have subtypes (i.e., is a union). enumValues is populated when the type is an enumerated type.

The code generation explicitly maps YANG types to corresponding generated language types. In the case that an explicit mapping is not specified, a type will be mapped to an empty interface (interface{}). For an explicit list of types that are supported, see the yangTypeTo*Type functions in this package.

func (*MappedType) OrderedUnionTypes added in v0.8.11

func (t *MappedType) OrderedUnionTypes() []string

OrderedUnionTypes returns a slice of union type names of the given MappedType in YANG order. If the type is not a union (i.e. UnionTypes is empty), then a nil slice is returned.

type NewLangMapperFn added in v0.8.4

type NewLangMapperFn func() LangMapper

type NodeDetails added in v0.8.4

type NodeDetails struct {
	// Name is the language-specific name that should be used for
	// the node.
	Name string
	// YANGDetails stores details of the node from the original
	// YANG schema, such that some characteristics can be accessed
	// by the code generation process. Only details that are
	// directly required are provided.
	YANGDetails YANGNodeDetails
	// Type describes the type of node that the leaf represents,
	// allowing for container, list, leaf and leaf-list entries
	// to be distinguished.
	// In the future it can be used to store other node types that
	// form a direct child of a subtree node.
	Type NodeType
	// LangType describes the type that the node should be given in
	// the output code, using the output of the language-specific
	// type mapping provided by calling the LangMapper interface.
	LangType *MappedType
	// MapPaths describes the paths that the output node should
	// be mapped to in the output code - these annotations can be
	// used to annotation the output code with the field(s) that it
	// corresponds to in the YANG schema.
	MapPaths [][]string
}

NodeDetails describes an individual field of the generated code tree. The Node may correspond to another Directory entry in the output code, or a individual leaf node.

type NodeType added in v0.8.4

type NodeType int64

NodeType describes the different types of node that can be output within the IR.

const (
	// InvalidNode represents a node that has not been correctly
	// set up.
	InvalidNode NodeType = iota
	// DirectoryNode indicates a YANG 'container'.
	DirectoryNode
	// ListNode indicates a YANG 'list'.
	ListNode
	// LeafNode represents a YANG 'leaf'.
	LeafNode
	// LeafListNode represents a YANG 'leaf-list'.
	LeafListNode
)

type ParseOpts added in v0.7.0

type ParseOpts struct {
	// 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
	// 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 behaviour is useful in scenarios where there is no difference between
	// two types, and the leaf is mirrored in a logical way (e.g., the OpenConfig
	// config/state split). For example:
	//
	// grouping foo-config {
	//	leaf enabled {
	//		type enumeration {
	//			enum A;
	//			enum B;
	//			enum C;
	//		}
	//	 }
	// }
	//
	//  container config { uses foo-config; }
	//  container state { uses foo-config; }
	//
	// will result in a single enumeration type (ModuleName_Config_Enabled) being
	// output when de-duplication is enabled.
	//
	// When it is disabled, two different enumerations (ModuleName_(State|Config)_Enabled)
	// will be output in the generated code.
	SkipEnumDeduplication bool
}

ParseOpts contains parsing configuration for a given schema.

type ParsedDirectory added in v0.8.4

type ParsedDirectory struct {
	// Name is the language-specific name of the directory to be
	// output.
	Name string
	// Type describes the type of directory that is being produced -
	// such that YANG 'list' entries can have special handling.
	Type DirType
	// Fields is the set of direct children of the node that are
	// to be output. It is keyed by the name of the child field
	// using a language-specific format.
	Fields map[string]*NodeDetails
	// ListAttr describes the attributes of a YANG list that
	// are required in the output code (e.g., the characteristics
	// of the list's keys).
	ListAttr *YangListAttr
	// IsFakeRoot indicates whether the directory being described
	// is the root entity and has been synthetically generated by
	// ygen.
	IsFakeRoot bool
}

ParsedDirectory describes an internal node within the generated code. Such a 'directory' may represent a struct, or a message, in the generated code. It represents a YANG 'container' or 'list'.

type Proto3Package

type Proto3Package struct {
	FilePath           []string // FilePath is the path to the file that this package should be written to.
	Header             string   // Header is the header text to be used in the package.
	Messages           []string // Messages is a slice of strings containing the set of messages that are within the generated package.
	Enums              []string // Enums is a slice of string containing the generated set of enumerations within the package.
	UsesYwrapperImport bool     // UsesYwrapperImport indicates whether the ywrapper proto package is used within the generated package.
	UsesYextImport     bool     // UsesYextImport indicates whether the yext proto package is used within the generated package.
}

Proto3Package stores the code for a generated protobuf3 package.

type ProtoOpts

type ProtoOpts struct {
	// BaseImportPath stores the root URL or path for imports that are
	// relative within the imported protobufs.
	BaseImportPath string
	// EnumPackageName stores the package name that should be used
	// for the package that defines enumerated types that are used
	// in multiple parts of the schema (identityrefs, and enumerations)
	// that fall within type definitions.
	EnumPackageName string
	// YwrapperPath is the path to the ywrapper.proto file that stores
	// the definition of the wrapper messages used to ensure that unset
	// fields can be distinguished from those that are set to their
	// default value. The path excluds the filename.
	YwrapperPath string
	// YextPath is the path to the yext.proto file that stores the
	// definition of the extension messages that are used to annotat the
	// generated protobuf messages.
	YextPath string
	// AnnotateSchemaPaths specifies whether the extensions defined in
	// yext.proto should be used to annotate schema paths into the output
	// protobuf file. See
	// https://github.com/openconfig/ygot/blob/master/docs/yang-to-protobuf-transformations-spec.md#annotation-of-schema-paths
	AnnotateSchemaPaths bool
	// AnnotateEnumNames specifies whether the extensions defined in
	// yext.proto should be used to annotate enum values with their
	// original YANG names in the output protobuf file.
	// See https://github.com/openconfig/ygot/blob/master/docs/yang-to-protobuf-transformations-spec.md#annotation-of-enums
	AnnotateEnumNames bool
	// NestedMessages indicates whether nested messages should be
	// output for the protobuf schema. If false, a separate package
	// is generated per package.
	NestedMessages bool
	// UseConsistentNamesForProtoUnionEnums, when set, avoids using the schema
	// path as the name of enumerations under unions in generated proto
	// code, and also appends a suffix to non-typedef union enums.
	UseConsistentNamesForProtoUnionEnums bool
	// GoPackageBase specifies the base of the names that are used in
	// the go_package file option for generated protobufs. Additional
	// package identifiers are appended to the go_package - such that
	// the format <base>/<path>/<to>/<package> is used.
	GoPackageBase string
}

ProtoOpts stores Protobuf specific options for the code generation library.

type TransformationOpts added in v0.7.0

type TransformationOpts struct {
	// CompressBehaviour specifies how the set of direct children of any
	// entry should determined. Specifically, whether compression is
	// enabled, and whether state fields in the schema should be excluded.
	CompressBehaviour genutil.CompressBehaviour
	// IgnoreShadowSchemaPaths indicates whether when OpenConfig path
	// compression is enabled, that the shadowed paths are to be ignored
	// while while unmarshalling.
	IgnoreShadowSchemaPaths bool
	// GenerateFakeRoot specifies whether an entity that represents the
	// root of the YANG schema tree should be generated in the generated
	// code.
	GenerateFakeRoot bool
	// FakeRootName specifies the name of the struct that should be generated
	// representing the root.
	FakeRootName string
	// ExcludeState specifies whether config false values should be
	// included in the generated code output. When set, all values that are
	// not writeable (i.e., config false) within the YANG schema and their
	// children are excluded from the generated code.
	ExcludeState bool
	// ShortenEnumLeafNames removes the module name from the name of
	// enumeration leaves.
	ShortenEnumLeafNames bool
	// EnumOrgPrefixesToTrim trims the organization name from the module
	// part of the name of enumeration leaves if there is a match.
	EnumOrgPrefixesToTrim []string
	// UseDefiningModuleForTypedefEnumNames uses the defining module name
	// to prefix typedef enumerated types instead of the module where the
	// typedef enumerated value is used.
	UseDefiningModuleForTypedefEnumNames bool
}

TransformationOpts specifies transformations to the generated code with respect to the input schema.

type YANGCodeGenerator

type YANGCodeGenerator struct {
	// Config stores the configuration parameters used for code generation.
	Config GeneratorConfig
}

YANGCodeGenerator is a structure that is used to pass arguments as to how the output Go code should be generated.

func NewYANGCodeGenerator

func NewYANGCodeGenerator(c *GeneratorConfig) *YANGCodeGenerator

NewYANGCodeGenerator returns a new instance of the YANGCodeGenerator struct to the calling function.

func (*YANGCodeGenerator) GenerateGoCode

func (cg *YANGCodeGenerator) GenerateGoCode(yangFiles, includePaths []string) (*GeneratedGoCode, util.Errors)

GenerateGoCode 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 GeneratedGoCode struct which contains:

  1. A struct definition for each container or list that is within the specified set of models.
  2. Enumerated values which correspond to the set of enumerated entities (leaves of type enumeration, identities, typedefs that reference an enumeration) within the specified models.

If errors are encountered during code generation, an error is returned.

func (*YANGCodeGenerator) GenerateProto3

func (cg *YANGCodeGenerator) GenerateProto3(yangFiles, includePaths []string) (*GeneratedProto3, util.Errors)

GenerateProto3 generates Protobuf 3 code for the input set of YANG files. The YANG schemas for which protobufs are to be created is supplied as the yangFiles argument, with included modules being searched for in includePaths. It returns a GeneratedProto3 struct containing the messages that are to be output, along with any associated values (e.g., enumerations).

type YANGNodeDetails added in v0.8.4

type YANGNodeDetails struct {
	// Name is the name of the node from the YANG schema.
	Name string
	// Default represents the 'default' value directly
	// specified in the YANG schema.
	Default string
	// Module stores the name of the module that instantiates
	// the node.
	Module string
	// Path specifies the complete YANG schema node path.
	Path []string
}

YANGNodeDetails stores the YANG-specific details of a node within the schema.

type YangListAttr added in v0.7.0

type YangListAttr struct {
	// keys is a map, keyed by the name of the key leaf, with values of the type
	// of the key of a YANG list.
	Keys map[string]*MappedType
	// keyElems is a slice containing the pointers to yang.Entry structs that
	// make up the list key.
	KeyElems []*yang.Entry
}

YangListAttr is used to store the additional elements for a Go struct that are required if the struct represents a YANG list. It stores the name of the key elements, and their associated types, along with pointers to those elements.

Directories

Path Synopsis
Package schematest is used for testing with the default OpenConfig generated structs.
Package schematest 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