Skip to content

Commit

Permalink
Flag changes (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanG100 authored Jun 21, 2022
1 parent d330fcb commit 916ff90
Show file tree
Hide file tree
Showing 28 changed files with 182 additions and 187 deletions.
52 changes: 23 additions & 29 deletions app/ygnmi/cmd/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
"github.com/openconfig/ygot/ygen"
"github.com/spf13/cobra"
"github.com/spf13/viper"

log "github.com/golang/glog"
)

// New returns a new generator command.
Expand All @@ -45,41 +43,40 @@ func New() *cobra.Command {
Args: cobra.MinimumNArgs(1),
}

generator.Flags().String("schema_struct_path", "", "The Go import path for the schema structs package. If struct generation is enabled, this defaults to base_import_path.")
generator.Flags().String("schema_struct_path", "", "The Go import path for the schema structs package. This is not needed for most use cases.")
generator.Flags().String("ygot_path", "github.com/openconfig/ygot/ygot", "The import path to use for ygot.")
generator.Flags().String("ygnmi_path", "github.com/openconfig/ygnmi/ygnmi", "The import path to use for ygnmi.")
generator.Flags().String("ytypes_path", "github.com/openconfig/ygot/ytypes", "The import path to use for ytypes.")
generator.Flags().String("goyang_path", "github.com/openconfig/goyang/pkg/yang", "The import path to use for goyang.")
generator.Flags().String("base_import_path", "", "This needs to be set to the import path of the output_dir.")
generator.Flags().String("base_package_path", "", "This needs to be set to the package path (module name + directories relative to go.mod file) of the output_dir.")
generator.Flags().String("trim_module_prefix", "", "A prefix (if any) to trim from generated package names and enums")
generator.Flags().StringSlice("paths", nil, "Comma-separated list of paths to be recursively searched for included modules or submodules within the defined YANG modules.")
generator.Flags().StringSlice("exclude_modules", nil, "Comma-separated YANG modules to exclude from code generation.")
generator.Flags().String("output_dir", "", "The directory that the generated Go code should be written to. This directory is the base of the generated module packages. default (working dir)")
generator.Flags().Bool("generate_structs", true, "Generate structs and schema for YANG modules.")
generator.Flags().Int("structs_split_files_count", 1, "The number of files to split the generated schema structs into.")
generator.Flags().Int("pathstructs_split_files_count", 1, "The number of files to split the generated path structs into.")

generator.Flags().MarkHidden("schema_struct_path")

return generator
}

const (
rootPackageName = "root"
)

// generate runs the ygnmi PathStruct and optionally the ygot GoStruct generation.
func generate(cmd *cobra.Command, args []string) error {
if viper.Get("base_import_path") == "" {
return fmt.Errorf("base_import_path must be set")
if viper.Get("base_package_path") == "" {
return fmt.Errorf("base_package_path must be set")
}

schemaStructPath := viper.GetString("schema_struct_path")
if viper.GetBool("generate_structs") {
if schemaStructPath == "" {
log.Info("schema_struct_path is unset, defaulting to base import path")
schemaStructPath = viper.GetString("base_import_path")
}
if schemaStructPath == "" {
schemaStructPath = viper.GetString("base_package_path")
}

version := "ygnmi version: " + cmd.Root().Version

importPath := strings.Split(viper.GetString("base_package_path"), "/")
rootPackageName := fmt.Sprintf("%spath", importPath[len(importPath)-1])

pcg := pathgen.GenConfig{
PackageName: rootPackageName,
GoImports: pathgen.GoImports{
Expand All @@ -92,7 +89,7 @@ func generate(cmd *cobra.Command, args []string) error {
ExcludeState: false,
SkipEnumDeduplication: false,
ShortenEnumLeafNames: true,
EnumOrgPrefixesToTrim: []string{"openconfig"},
EnumOrgPrefixesToTrim: []string{viper.GetString("trim_module_prefix")},
UseDefiningModuleForTypedefEnumNames: false,
AppendEnumSuffixForSimpleUnionEnums: true,
FakeRootName: "root",
Expand All @@ -101,14 +98,14 @@ func generate(cmd *cobra.Command, args []string) error {
YANGParseOptions: yang.Options{
IgnoreSubmoduleCircularDependencies: false,
},
GeneratingBinary: version,
GenerateWildcardPaths: true,
TrimOCPackage: true,
SplitByModule: true,
BaseImportPath: viper.GetString("base_import_path"),
PackageSuffix: "",
UnifyPathStructs: true,
ExtraGenerators: []pathgen.Generator{pathgen.GNMIGenerator},
GeneratingBinary: version,
GenerateWildcardPaths: true,
TrimPackageModulePrefix: viper.GetString("trim_module_prefix"),
SplitByModule: true,
BasePackagePath: viper.GetString("base_package_path"),
PackageSuffix: "",
UnifyPathStructs: true,
ExtraGenerators: []pathgen.Generator{pathgen.GNMIGenerator},
}

pathCode, _, errs := pcg.GeneratePathCode(args, viper.GetStringSlice("paths"))
Expand Down Expand Up @@ -139,9 +136,6 @@ func generate(cmd *cobra.Command, args []string) error {
return err
}
}
if !viper.GetBool("generate_structs") {
return nil
}

return generateStructs(args, schemaStructPath, version)
}
Expand All @@ -168,7 +162,7 @@ func generateStructs(modules []string, schemaPath, version string) error {
GenerateFakeRoot: true,
FakeRootName: "root",
ShortenEnumLeafNames: true,
EnumOrgPrefixesToTrim: []string{"openconfig"},
EnumOrgPrefixesToTrim: []string{viper.GetString("trim_module_prefix")},
UseDefiningModuleForTypedefEnumNames: false,
EnumerationsUseUnderscores: true,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

/*
Package root is a generated package which contains definitions
Package exampleocpath is a generated package which contains definitions
of structs which generate gNMI paths for a YANG schema. The generated paths are
based on a compressed form of the schema.
Expand All @@ -24,7 +24,7 @@ using the following YANG input files:
- ../../pathgen/testdata/yang/openconfig-nested.yang
Imported modules were sourced from:
*/
package root
package exampleocpath

import (
oc "github.com/openconfig/ygnmi/internal/exampleoc"
Expand All @@ -40,8 +40,8 @@ type RootPath struct {
*ygnmi.DeviceRootBase
}

// New returns a new path object from which YANG paths can be constructed.
func New() *RootPath {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *RootPath {
return &RootPath{ygnmi.NewDeviceRootBase()}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/exampleoc/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ set -e
cd "$(dirname "$0")"

go run ../../app/ygnmi generator \
--base_import_path=github.com/openconfig/ygnmi/internal/exampleoc \
--trim_module_prefix=openconfig \
--base_package_path=github.com/openconfig/ygnmi/internal/exampleoc \
../../pathgen/testdata/yang/openconfig-simple.yang \
../../pathgen/testdata/yang/openconfig-withlistval.yang \
../../pathgen/testdata/yang/openconfig-nested.yang
Expand Down
40 changes: 20 additions & 20 deletions pathgen/pathgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ type GenConfig struct {
GenerateWildcardPaths bool
// SplitByModule controls whether to generate a go package for each yang module.
SplitByModule bool
// TrimOCPackage controls whether to trim openconfig- from generated go package names.
TrimOCPackage bool
// BaseImportPath is used to create to full import path of the generated go packages.
BaseImportPath string
// TrimPackageModulePrefix is the prefix to trim from generated go package names.
TrimPackageModulePrefix string
// BasePackagePath is used to create to full import path of the generated go packages.
BasePackagePath string
// PackageString is the string to apppend to the generated Go package names.
PackageSuffix string
// UnifyPathStructs controls whether to generate both config and states in the same package.
Expand Down Expand Up @@ -276,7 +276,7 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (map[str
}

// Get NodeDataMap for the schema.
nodeDataMap, es := getNodeDataMap(ir, cg.FakeRootName, schemaStructPkgAccessor, cg.PathStructSuffix, cg.PackageName, cg.PackageSuffix, cg.SplitByModule, cg.TrimOCPackage)
nodeDataMap, es := getNodeDataMap(ir, cg.FakeRootName, schemaStructPkgAccessor, cg.PathStructSuffix, cg.PackageName, cg.PackageSuffix, cg.SplitByModule, cg.TrimPackageModulePrefix)
if es != nil {
errs = util.AppendErrs(errs, es)
}
Expand Down Expand Up @@ -304,7 +304,7 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (map[str
for _, directoryPath := range ir.OrderedDirectoryPathsByName() {
directory := ir.Directories[directoryPath]

structSnippet, es := generateDirectorySnippet(directory, ir.Directories, schemaStructPkgAccessor, cg.PathStructSuffix, cg.GenerateWildcardPaths, cg.SplitByModule, cg.TrimOCPackage, cg.PackageName, cg.PackageSuffix, cg.UnifyPathStructs)
structSnippet, es := generateDirectorySnippet(directory, ir.Directories, schemaStructPkgAccessor, cg.PathStructSuffix, cg.GenerateWildcardPaths, cg.SplitByModule, cg.TrimPackageModulePrefix, cg.PackageName, cg.PackageSuffix, cg.UnifyPathStructs)
if es != nil {
errs = util.AppendErrs(errs, es)
}
Expand Down Expand Up @@ -351,13 +351,13 @@ var packageNameReplacePattern = regexp.MustCompile("[._-]")
// a transformed version of the module that the directory belongs to is returned.
// If trimOCPkg is true, "openconfig-" is remove from the package name.
// fakeRootPkgName is the name of the package that contains just the fake root path struct.
func goPackageName(rootModuleName string, splitByModule, trimOCPkg, isFakeRoot bool, pkgName, pkgSuffix string) string {
func goPackageName(rootModuleName string, splitByModule, isFakeRoot bool, pkgName, trimPrefix, pkgSuffix string) string {
if !splitByModule || isFakeRoot {
return pkgName
}
name := rootModuleName
if trimOCPkg {
name = strings.TrimPrefix(name, "openconfig-")
if trimPrefix != "" {
name = strings.TrimPrefix(name, trimPrefix)
}

name = packageNameReplacePattern.ReplaceAllString(name, "")
Expand Down Expand Up @@ -570,8 +570,8 @@ type {{ .TypeName }} struct {
*ygnmi.{{ .FakeRootBaseTypeName }}
}
// New returns a new path object from which YANG paths can be constructed.
func New() *{{ .TypeName }} {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *{{ .TypeName }} {
return &{{ .TypeName }}{ygnmi.New{{- .FakeRootBaseTypeName }}()}
}
`)
Expand Down Expand Up @@ -688,7 +688,7 @@ func mustTemplate(name, src string) *template.Template {
// parsed information.
// packageName, splitByModule, and trimOCPackage are used to determine
// the generated Go package name for the generated PathStructs.
func getNodeDataMap(ir *ygen.IR, fakeRootName, schemaStructPkgAccessor, pathStructSuffix, packageName, packageSuffix string, splitByModule, trimOCPackage bool) (NodeDataMap, util.Errors) {
func getNodeDataMap(ir *ygen.IR, fakeRootName, schemaStructPkgAccessor, pathStructSuffix, packageName, packageSuffix string, splitByModule bool, trimPrefix string) (NodeDataMap, util.Errors) {
nodeDataMap := NodeDataMap{}
var errs util.Errors
for _, dir := range ir.Directories {
Expand All @@ -705,7 +705,7 @@ func getNodeDataMap(ir *ygen.IR, fakeRootName, schemaStructPkgAccessor, pathStru
HasDefault: false,
YANGTypeName: "",
YANGPath: "/",
GoPathPackageName: goPackageName(dir.RootElementModule, splitByModule, trimOCPackage, true, packageName, packageSuffix),
GoPathPackageName: goPackageName(dir.RootElementModule, splitByModule, true, packageName, trimPrefix, packageSuffix),
DirectoryName: dir.Path,
}
}
Expand Down Expand Up @@ -760,7 +760,7 @@ func getNodeDataMap(ir *ygen.IR, fakeRootName, schemaStructPkgAccessor, pathStru
HasDefault: isLeaf && (len(field.YANGDetails.Defaults) > 0 || mType.DefaultValue != nil),
YANGTypeName: yangTypeName,
YANGPath: field.YANGDetails.Path,
GoPathPackageName: goPackageName(field.YANGDetails.RootElementModule, splitByModule, trimOCPackage, false, packageName, packageSuffix),
GoPathPackageName: goPackageName(field.YANGDetails.RootElementModule, splitByModule, false, packageName, trimPrefix, packageSuffix),
DirectoryName: field.YANGDetails.Path,
}
if isLeaf {
Expand Down Expand Up @@ -805,7 +805,7 @@ func writeHeader(yangFiles, includePaths []string, packageName string, cg *GenCo
}
// Create an ordered list of imports to include in the header.
for dep := range genCode.Deps {
s.ExtraImports = append(s.ExtraImports, fmt.Sprintf("%s/%s", cg.BaseImportPath, dep))
s.ExtraImports = append(s.ExtraImports, fmt.Sprintf("%s/%s", cg.BasePackagePath, dep))
}
sort.Slice(s.ExtraImports, func(i, j int) bool { return s.ExtraImports[i] < s.ExtraImports[j] })

Expand Down Expand Up @@ -887,7 +887,7 @@ type goPathFieldData struct {
// node, and directories is a map from path to a parsed schema node for all
// directory nodes in the schema.
func generateDirectorySnippet(directory *ygen.ParsedDirectory, directories map[string]*ygen.ParsedDirectory, schemaStructPkgAccessor, pathStructSuffix string,
generateWildcardPaths, splitByModule, trimOCPkg bool, pkgName, pkgSuffix string, unified bool) ([]GoPathStructCodeSnippet, util.Errors) {
generateWildcardPaths, splitByModule bool, trimPrefix, pkgName, pkgSuffix string, unified bool) ([]GoPathStructCodeSnippet, util.Errors) {

var errs util.Errors
// structBuf is used to store the code associated with the struct defined for
Expand Down Expand Up @@ -929,8 +929,8 @@ func generateDirectorySnippet(directory *ygen.ParsedDirectory, directories map[s
// Only the fake root could be importing a child path struct from another package.
// If it is, add that package as a dependency and set the accessor.
if directory.IsFakeRoot && (field.Type == ygen.ContainerNode || field.Type == ygen.ListNode) {
parentPackge := goPackageName(directory.RootElementModule, splitByModule, trimOCPkg, directory.IsFakeRoot, pkgName, pkgSuffix)
childPackage := goPackageName(field.YANGDetails.RootElementModule, splitByModule, trimOCPkg, false, pkgName, pkgSuffix)
parentPackge := goPackageName(directory.RootElementModule, splitByModule, directory.IsFakeRoot, pkgName, trimPrefix, pkgSuffix)
childPackage := goPackageName(field.YANGDetails.RootElementModule, splitByModule, false, pkgName, trimPrefix, pkgSuffix)
if parentPackge != childPackage {
deps[childPackage] = true
childPkgAccessor = childPackage + "."
Expand Down Expand Up @@ -977,7 +977,7 @@ func generateDirectorySnippet(directory *ygen.ParsedDirectory, directories map[s
leafSnippet := GoPathStructCodeSnippet{
PathStructName: leafTypeName,
StructBase: buf.String(),
Package: goPackageName(directory.RootElementModule, splitByModule, trimOCPkg, directory.IsFakeRoot, pkgName, pkgSuffix),
Package: goPackageName(directory.RootElementModule, splitByModule, directory.IsFakeRoot, pkgName, trimPrefix, pkgSuffix),
}
snippets = append(snippets, leafSnippet)
}
Expand All @@ -990,7 +990,7 @@ func generateDirectorySnippet(directory *ygen.ParsedDirectory, directories map[s
PathStructName: structData.TypeName,
StructBase: structBuf.String(),
ChildConstructors: methodBuf.String(),
Package: goPackageName(directory.RootElementModule, splitByModule, trimOCPkg, directory.IsFakeRoot, pkgName, pkgSuffix),
Package: goPackageName(directory.RootElementModule, splitByModule, directory.IsFakeRoot, pkgName, trimPrefix, pkgSuffix),
}
for dep := range deps {
nonLeafSnippet.Deps = append(nonLeafSnippet.Deps, dep)
Expand Down
22 changes: 11 additions & 11 deletions pathgen/pathgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ func TestGeneratePathCodeSplitModules(t *testing.T) {
inFiles []string
// inIncludePaths is the set of paths that should be searched for imports.
inIncludePaths []string
inTrimOCPath bool
inTrimPrefix string
// wantStructsCodeFileDir map from package name to want source file.
wantStructsCodeFiles map[string]string
}{{
Expand All @@ -1280,7 +1280,7 @@ func TestGeneratePathCodeSplitModules(t *testing.T) {
}, {
name: "oc simple and trim",
inFiles: []string{filepath.Join(datapath, "openconfig-simple.yang")},
inTrimOCPath: true,
inTrimPrefix: "openconfig",
wantStructsCodeFiles: map[string]string{
"simplepath": "testdata/modules/oc-simple-trim/simple.txt",
"device": "testdata/modules/oc-simple-trim/device.txt",
Expand Down Expand Up @@ -1314,8 +1314,8 @@ func TestGeneratePathCodeSplitModules(t *testing.T) {
cg.PreferOperationalState = true
cg.GenerateWildcardPaths = true
cg.SplitByModule = true
cg.BaseImportPath = "example.com"
cg.TrimOCPackage = tt.inTrimOCPath
cg.BasePackagePath = "example.com"
cg.TrimPackageModulePrefix = tt.inTrimPrefix

gotCode, _, err := cg.GeneratePathCode(tt.inFiles, tt.inIncludePaths)
if err != nil {
Expand Down Expand Up @@ -2101,7 +2101,7 @@ func TestGetNodeDataMap(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, gotErrs := getNodeDataMap(tt.inIR, tt.inFakeRootName, tt.inSchemaStructPkgAccessor, tt.inPathStructSuffix, tt.inPackageName, tt.inPackageSuffix, tt.inSplitByModule, false)
got, gotErrs := getNodeDataMap(tt.inIR, tt.inFakeRootName, tt.inSchemaStructPkgAccessor, tt.inPathStructSuffix, tt.inPackageName, tt.inPackageSuffix, tt.inSplitByModule, "")
// TODO(wenbli): Enhance gNMI's errdiff with checking a slice of substrings and use here.
var gotErrStrs []string
for _, err := range gotErrs {
Expand Down Expand Up @@ -2327,8 +2327,8 @@ type RootPath struct {
*ygnmi.DeviceRootBase
}
// New returns a new path object from which YANG paths can be constructed.
func New() *RootPath {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *RootPath {
return &RootPath{ygnmi.NewDeviceRootBase()}
}
`
Expand All @@ -2341,8 +2341,8 @@ type RootPath struct {
*ygnmi.DeviceRootBase
}
// New returns a new path object from which YANG paths can be constructed.
func New() *RootPath {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *RootPath {
return &RootPath{ygnmi.NewDeviceRootBase()}
}
`
Expand Down Expand Up @@ -3028,7 +3028,7 @@ func (n *ListPathAny) WithUnionKey(UnionKey oc.RootElementModule_List_UnionKey_U
for _, tt := range tests {
if tt.want != nil {
t.Run(tt.name, func(t *testing.T) {
got, gotErr := generateDirectorySnippet(tt.inDirectory, directories, "oc.", tt.inPathStructSuffix, true, tt.inSplitByModule, false, tt.inPackageName, tt.inPackageSuffix, tt.inUnifiedPath)
got, gotErr := generateDirectorySnippet(tt.inDirectory, directories, "oc.", tt.inPathStructSuffix, true, tt.inSplitByModule, "", tt.inPackageName, tt.inPackageSuffix, tt.inUnifiedPath)
if gotErr != nil {
t.Fatalf("func generateDirectorySnippet, unexpected error: %v", gotErr)
}
Expand All @@ -3044,7 +3044,7 @@ func (n *ListPathAny) WithUnionKey(UnionKey oc.RootElementModule_List_UnionKey_U

if tt.wantNoWildcard != nil {
t.Run(tt.name+" no wildcard", func(t *testing.T) {
got, gotErr := generateDirectorySnippet(tt.inDirectory, directories, "oc.", tt.inPathStructSuffix, false, tt.inSplitByModule, false, tt.inPackageName, tt.inPackageSuffix, tt.inUnifiedPath)
got, gotErr := generateDirectorySnippet(tt.inDirectory, directories, "oc.", tt.inPathStructSuffix, false, tt.inSplitByModule, "", tt.inPackageName, tt.inPackageSuffix, tt.inUnifiedPath)
if gotErr != nil {
t.Fatalf("func generateDirectorySnippet, unexpected error: %v", gotErr)
}
Expand Down
4 changes: 2 additions & 2 deletions pathgen/testdata/modules/oc-import/device.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type DevicePath struct {
*ygnmi.DeviceRootBase
}

// New returns a new path object from which YANG paths can be constructed.
func New() *DevicePath {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *DevicePath {
return &DevicePath{ygnmi.NewDeviceRootBase()}
}

Expand Down
4 changes: 2 additions & 2 deletions pathgen/testdata/modules/oc-list/device.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type DevicePath struct {
*ygnmi.DeviceRootBase
}

// New returns a new path object from which YANG paths can be constructed.
func New() *DevicePath {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *DevicePath {
return &DevicePath{ygnmi.NewDeviceRootBase()}
}

Expand Down
4 changes: 2 additions & 2 deletions pathgen/testdata/modules/oc-simple-trim/device.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type DevicePath struct {
*ygnmi.DeviceRootBase
}

// New returns a new path object from which YANG paths can be constructed.
func New() *DevicePath {
// Root returns a root path object from which YANG paths can be constructed.
func Root() *DevicePath {
return &DevicePath{ygnmi.NewDeviceRootBase()}
}

Expand Down
Loading

0 comments on commit 916ff90

Please sign in to comment.