Skip to content

Commit 26a85b4

Browse files
authored
Use correct schema entry for determining package name, and minor refactor of getNodeDataMap. (#594)
Previously it was using the parent's directory. Now changed to use the child's schema to generate the package name. Also moved fakeroot's `NodeData` generation within `getNodeDataMap` itself.
1 parent a776802 commit 26a85b4

File tree

2 files changed

+61
-36
lines changed

2 files changed

+61
-36
lines changed

ypathgen/pathgen.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (map[str
261261
}
262262

263263
// Get NodeDataMap for the schema.
264-
nodeDataMap, es := getNodeDataMap(directories, leafTypeMap, schemaStructPkgAccessor, cg.PathStructSuffix, cg.PackageName, cg.SplitByModule, cg.TrimOCPackage)
264+
nodeDataMap, es := getNodeDataMap(directories, leafTypeMap, cg.FakeRootName, schemaStructPkgAccessor, cg.PathStructSuffix, cg.PackageName, cg.SplitByModule, cg.TrimOCPackage)
265265
if es != nil {
266266
errs = util.AppendErrs(errs, es)
267267
}
@@ -275,23 +275,6 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (map[str
275275
util.NewErrs(fmt.Errorf("GeneratePathCode: Implementation bug -- node %s not found in dirNameMap", directoryName)))
276276
}
277277

278-
if ygen.IsFakeRoot(directory.Entry) {
279-
// Since we always generate the fake root, we add the
280-
// fake root GoStruct to the data map as well.
281-
nodeDataMap[directory.Name+cg.PathStructSuffix] = &NodeData{
282-
GoTypeName: "*" + schemaStructPkgAccessor + yang.CamelCase(cg.FakeRootName),
283-
LocalGoTypeName: "*" + yang.CamelCase(cg.FakeRootName),
284-
GoFieldName: "",
285-
SubsumingGoStructName: yang.CamelCase(cg.FakeRootName),
286-
IsLeaf: false,
287-
IsScalarField: false,
288-
HasDefault: false,
289-
YANGTypeName: "",
290-
YANGPath: "/",
291-
GoPathPackageName: goPackageName(directory, cg.SplitByModule, cg.TrimOCPackage, cg.PackageName),
292-
}
293-
}
294-
295278
var listBuilderKeyThreshold uint
296279
if cg.GenerateWildcardPaths {
297280
listBuilderKeyThreshold = cg.ListBuilderKeyThreshold
@@ -332,16 +315,16 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (map[str
332315
// packageNameReplacePattern matches all characters allowed in yang modules, but not go packages.
333316
var packageNameReplacePattern = regexp.MustCompile("[._-]")
334317

335-
// goPackageName returns the go package to use when generating code for the input Directory.
318+
// goPackageName returns the go package to use when generating code for the input schema Entry.
336319
// If splitByModule is false, the pkgName is always returned. Otherwise,
337320
// a transformed version of the module that the directory belongs to is returned.
338321
// If trimOCPkg is true, "openconfig-" is remove from the package name.
339322
// fakeRootPkgName is the name of the package that contains just the fake root path struct.
340-
func goPackageName(dir *ygen.Directory, splitByModule, trimOCPkg bool, pkgName string) string {
341-
if !splitByModule || ygen.IsFakeRoot(dir.Entry) {
323+
func goPackageName(entry *yang.Entry, splitByModule, trimOCPkg bool, pkgName string) string {
324+
if !splitByModule || ygen.IsFakeRoot(entry) {
342325
return pkgName
343326
}
344-
name := util.SchemaTreeRoot(dir.Entry).Name
327+
name := util.SchemaTreeRoot(entry).Name
345328
if trimOCPkg {
346329
name = strings.TrimPrefix(name, "openconfig-")
347330
}
@@ -622,11 +605,27 @@ func mustTemplate(name, src string) *template.Template {
622605
// packageName, splitByModule, and trimOCPackage are used to determine
623606
// the generated Go package name for the generated PathStructs.
624607
// If a directory or field doesn't exist in the leafTypeMap, then an error is returned.
625-
// Note: Top-level nodes, but *not* the fake root, are part of the output.
626-
func getNodeDataMap(directories map[string]*ygen.Directory, leafTypeMap map[string]map[string]*ygen.MappedType, schemaStructPkgAccessor, pathStructSuffix, packageName string, splitByModule, trimOCPackage bool) (NodeDataMap, util.Errors) {
608+
func getNodeDataMap(directories map[string]*ygen.Directory, leafTypeMap map[string]map[string]*ygen.MappedType, fakeRootName, schemaStructPkgAccessor, pathStructSuffix, packageName string, splitByModule, trimOCPackage bool) (NodeDataMap, util.Errors) {
627609
nodeDataMap := NodeDataMap{}
628610
var errs util.Errors
629611
for path, dir := range directories {
612+
if ygen.IsFakeRoot(dir.Entry) {
613+
// Since we always generate the fake root, we add the
614+
// fake root GoStruct to the data map as well.
615+
nodeDataMap[dir.Name+pathStructSuffix] = &NodeData{
616+
GoTypeName: "*" + schemaStructPkgAccessor + yang.CamelCase(fakeRootName),
617+
LocalGoTypeName: "*" + yang.CamelCase(fakeRootName),
618+
GoFieldName: "",
619+
SubsumingGoStructName: yang.CamelCase(fakeRootName),
620+
IsLeaf: false,
621+
IsScalarField: false,
622+
HasDefault: false,
623+
YANGTypeName: "",
624+
YANGPath: "/",
625+
GoPathPackageName: goPackageName(dir.Entry, splitByModule, trimOCPackage, packageName),
626+
}
627+
}
628+
630629
goFieldNameMap := ygen.GoFieldNameMap(dir)
631630
fieldTypeMap, ok := leafTypeMap[path]
632631
if !ok {
@@ -686,7 +685,7 @@ func getNodeDataMap(directories map[string]*ygen.Directory, leafTypeMap map[stri
686685
HasDefault: isLeaf && (field.Default != "" || mType.DefaultValue != nil),
687686
YANGTypeName: yangTypeName,
688687
YANGPath: field.Path(),
689-
GoPathPackageName: goPackageName(dir, splitByModule, trimOCPackage, packageName),
688+
GoPathPackageName: goPackageName(field, splitByModule, trimOCPackage, packageName),
690689
}
691690
}
692691
}
@@ -843,8 +842,8 @@ func generateDirectorySnippet(directory *ygen.Directory, directories map[string]
843842
// If it is, add that package as a dependency and set the accessor.
844843
if ygen.IsFakeRoot(directory.Entry) {
845844
if fieldDirectory := directories[field.Path()]; fieldDirectory != nil {
846-
parentPackge := goPackageName(directory, splitByModule, trimOCPkg, pkgName)
847-
childPackage := goPackageName(fieldDirectory, splitByModule, trimOCPkg, pkgName)
845+
parentPackge := goPackageName(directory.Entry, splitByModule, trimOCPkg, pkgName)
846+
childPackage := goPackageName(field, splitByModule, trimOCPkg, pkgName)
848847
if parentPackge != childPackage {
849848
deps[childPackage] = true
850849
childPkgAccessor = childPackage + "."
@@ -892,7 +891,7 @@ func generateDirectorySnippet(directory *ygen.Directory, directories map[string]
892891
PathStructName: structData.TypeName,
893892
StructBase: structBuf.String(),
894893
ChildConstructors: methodBuf.String(),
895-
Package: goPackageName(directory, splitByModule, trimOCPkg, pkgName),
894+
Package: goPackageName(directory.Entry, splitByModule, trimOCPkg, pkgName),
896895
}
897896
for dep := range deps {
898897
snippet.Deps = append(snippet.Deps, dep)

ypathgen/pathgen_test.go

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,7 @@ func TestGetNodeDataMap(t *testing.T) {
16641664
name string
16651665
inDirectories map[string]*ygen.Directory
16661666
inLeafTypeMap map[string]map[string]*ygen.MappedType
1667+
inFakeRootName string
16671668
inSchemaStructPkgAccessor string
16681669
inPathStructSuffix string
16691670
inPackageName string
@@ -1679,6 +1680,7 @@ func TestGetNodeDataMap(t *testing.T) {
16791680
"leaf": leafTypeMap["/root-module/container"]["leaf"],
16801681
},
16811682
},
1683+
inFakeRootName: "device",
16821684
inSchemaStructPkgAccessor: "struct.",
16831685
inPathStructSuffix: "Path",
16841686
wantNodeDataMap: NodeDataMap{
@@ -1698,6 +1700,7 @@ func TestGetNodeDataMap(t *testing.T) {
16981700
name: "non-leaf and non-scalar leaf",
16991701
inDirectories: directoryWithBinaryLeaf,
17001702
inLeafTypeMap: leafTypeMap2,
1703+
inFakeRootName: "device",
17011704
inSchemaStructPkgAccessor: "struct.",
17021705
inPathStructSuffix: "_Path",
17031706
wantNodeDataMap: NodeDataMap{
@@ -1719,8 +1722,17 @@ func TestGetNodeDataMap(t *testing.T) {
17191722
IsScalarField: false,
17201723
HasDefault: false,
17211724
},
1725+
"Root_Path": {
1726+
GoTypeName: "*struct.Device",
1727+
LocalGoTypeName: "*Device",
1728+
GoFieldName: "",
1729+
SubsumingGoStructName: "Device",
1730+
IsLeaf: false,
1731+
IsScalarField: false,
1732+
HasDefault: false,
1733+
},
17221734
},
1723-
wantSorted: []string{"Container_Leaf_Path", "Container_Path"},
1735+
wantSorted: []string{"Container_Leaf_Path", "Container_Path", "Root_Path"},
17241736
}, {
17251737
name: "non-existent path",
17261738
inDirectories: map[string]*ygen.Directory{"/root-module/container": directories["/root-module/container"]},
@@ -1732,6 +1744,7 @@ func TestGetNodeDataMap(t *testing.T) {
17321744
"leaf": {NativeType: "Binary"},
17331745
},
17341746
},
1747+
inFakeRootName: "device",
17351748
inSchemaStructPkgAccessor: "oc.",
17361749
inPathStructSuffix: "Path",
17371750
wantErrSubstrings: []string{`path "/root-module/container" does not exist`},
@@ -1746,13 +1759,15 @@ func TestGetNodeDataMap(t *testing.T) {
17461759
"laugh": leafTypeMap["/root-module/container"]["leaf"],
17471760
},
17481761
},
1762+
inFakeRootName: "device",
17491763
inSchemaStructPkgAccessor: "oc.",
17501764
inPathStructSuffix: "Path",
17511765
wantErrSubstrings: []string{`field name "leaf" does not exist`},
17521766
}, {
17531767
name: "big test with everything",
17541768
inDirectories: directories,
17551769
inLeafTypeMap: leafTypeMap,
1770+
inFakeRootName: "root",
17561771
inPathStructSuffix: "Path",
17571772
inSplitByModule: true,
17581773
inPackageName: "device",
@@ -1765,7 +1780,7 @@ func TestGetNodeDataMap(t *testing.T) {
17651780
IsLeaf: false,
17661781
IsScalarField: false,
17671782
HasDefault: false,
1768-
GoPathPackageName: "device",
1783+
GoPathPackageName: "rootmodule_path",
17691784
},
17701785
"ContainerWithConfigPath": {
17711786
GoTypeName: "*ContainerWithConfig",
@@ -1775,7 +1790,7 @@ func TestGetNodeDataMap(t *testing.T) {
17751790
IsLeaf: false,
17761791
IsScalarField: false,
17771792
HasDefault: false,
1778-
GoPathPackageName: "device",
1793+
GoPathPackageName: "rootmodule_path",
17791794
},
17801795
"ContainerWithConfig_LeafPath": {
17811796
GoTypeName: "Binary",
@@ -1827,7 +1842,7 @@ func TestGetNodeDataMap(t *testing.T) {
18271842
IsScalarField: false,
18281843
HasDefault: false,
18291844
YANGTypeName: "ieeefloat32",
1830-
GoPathPackageName: "device",
1845+
GoPathPackageName: "rootmodule_path",
18311846
},
18321847
"LeafWithDefaultPath": {
18331848
GoTypeName: "string",
@@ -1838,7 +1853,7 @@ func TestGetNodeDataMap(t *testing.T) {
18381853
IsScalarField: true,
18391854
HasDefault: true,
18401855
YANGTypeName: "string",
1841-
GoPathPackageName: "device",
1856+
GoPathPackageName: "rootmodule_path",
18421857
},
18431858
"ListPath": {
18441859
GoTypeName: "*List",
@@ -1848,7 +1863,7 @@ func TestGetNodeDataMap(t *testing.T) {
18481863
IsLeaf: false,
18491864
IsScalarField: false,
18501865
HasDefault: false,
1851-
GoPathPackageName: "device",
1866+
GoPathPackageName: "rootmodule_path",
18521867
},
18531868
"ListWithStatePath": {
18541869
GoTypeName: "*ListWithState",
@@ -1858,7 +1873,7 @@ func TestGetNodeDataMap(t *testing.T) {
18581873
IsLeaf: false,
18591874
IsScalarField: false,
18601875
HasDefault: false,
1861-
GoPathPackageName: "device",
1876+
GoPathPackageName: "rootmodule_path",
18621877
},
18631878
"ListWithState_KeyPath": {
18641879
GoTypeName: "float64",
@@ -1899,6 +1914,16 @@ func TestGetNodeDataMap(t *testing.T) {
18991914
IsScalarField: false,
19001915
HasDefault: false,
19011916
GoPathPackageName: "rootmodule_path",
1917+
},
1918+
"RootPath": {
1919+
GoTypeName: "*Root",
1920+
LocalGoTypeName: "*Root",
1921+
GoFieldName: "",
1922+
SubsumingGoStructName: "Root",
1923+
IsLeaf: false,
1924+
IsScalarField: false,
1925+
HasDefault: false,
1926+
GoPathPackageName: "device",
19021927
}},
19031928
wantSorted: []string{
19041929
"ContainerPath",
@@ -1915,12 +1940,13 @@ func TestGetNodeDataMap(t *testing.T) {
19151940
"List_Key1Path",
19161941
"List_Key2Path",
19171942
"List_UnionKeyPath",
1943+
"RootPath",
19181944
},
19191945
}}
19201946

19211947
for _, tt := range tests {
19221948
t.Run(tt.name, func(t *testing.T) {
1923-
got, gotErrs := getNodeDataMap(tt.inDirectories, tt.inLeafTypeMap, tt.inSchemaStructPkgAccessor, tt.inPathStructSuffix, tt.inPackageName, tt.inSplitByModule, false)
1949+
got, gotErrs := getNodeDataMap(tt.inDirectories, tt.inLeafTypeMap, tt.inFakeRootName, tt.inSchemaStructPkgAccessor, tt.inPathStructSuffix, tt.inPackageName, tt.inSplitByModule, false)
19241950
// TODO(wenbli): Enhance gNMI's errdiff with checking a slice of substrings and use here.
19251951
var gotErrStrs []string
19261952
for _, err := range gotErrs {

0 commit comments

Comments
 (0)