Skip to content

Commit

Permalink
Add Fakeroot to NodeDataMap return value for ypathgen's GeneratePathC…
Browse files Browse the repository at this point in the history
…ode. (#550)

This helps the consuming code process the fake root as well.
  • Loading branch information
wenovus authored Jun 28, 2021
1 parent 926dd30 commit 5ba0876
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
27 changes: 21 additions & 6 deletions ypathgen/pathgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (*Genera
schemaStructPkgAccessor = schemaStructPkgAlias + "."
}

// Get NodeDataMap for the schema.
nodeDataMap, es := getNodeDataMap(directories, leafTypeMap, schemaStructPkgAccessor, cg.PathStructSuffix)
if es != nil {
errs = util.AppendErrs(errs, es)
}

// Generate struct code.
var structSnippets []GoPathStructCodeSnippet
for _, directoryName := range orderedDirNames {
Expand All @@ -265,6 +271,21 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (*Genera
util.NewErrs(fmt.Errorf("GeneratePathCode: Implementation bug -- node %s not found in dirNameMap", directoryName)))
}

if ygen.IsFakeRoot(directory.Entry) {
// Since we always generate the fake root, we add the
// fake root GoStruct to the data map as well.
nodeDataMap[directory.Name+cg.PathStructSuffix] = &NodeData{
GoTypeName: "*" + schemaStructPkgAccessor + yang.CamelCase(cg.FakeRootName),
LocalGoTypeName: "*" + yang.CamelCase(cg.FakeRootName),
GoFieldName: "",
SubsumingGoStructName: yang.CamelCase(cg.FakeRootName),
IsLeaf: false,
IsScalarField: false,
YANGTypeName: "",
YANGPath: "/",
}
}

var listBuilderKeyThreshold uint
if cg.GenerateWildcardPaths {
listBuilderKeyThreshold = cg.ListBuilderKeyThreshold
Expand All @@ -277,12 +298,6 @@ func (cg *GenConfig) GeneratePathCode(yangFiles, includePaths []string) (*Genera
}
genCode.Structs = structSnippets

// Get NodeDataMap for the schema.
nodeDataMap, es := getNodeDataMap(directories, leafTypeMap, schemaStructPkgAccessor, cg.PathStructSuffix)
if es != nil {
errs = util.AppendErrs(errs, es)
}

if len(errs) == 0 {
errs = nil
}
Expand Down
42 changes: 42 additions & 0 deletions ypathgen/pathgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func TestGeneratePathCode(t *testing.T) {
inPathStructSuffix: "Path",
checkYANGPath: true,
wantNodeDataMap: NodeDataMap{
"DevicePath": {
GoTypeName: "*Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"ParentPath": {
GoTypeName: "*Parent",
LocalGoTypeName: "*Parent",
Expand Down Expand Up @@ -168,6 +174,12 @@ func TestGeneratePathCode(t *testing.T) {
inSchemaStructPkgPath: "",
inPathStructSuffix: "Path",
wantNodeDataMap: NodeDataMap{
"DevicePath": {
GoTypeName: "*Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"ParentPath": {
GoTypeName: "*Parent",
LocalGoTypeName: "*Parent",
Expand Down Expand Up @@ -248,6 +260,12 @@ func TestGeneratePathCode(t *testing.T) {
inSchemaStructPkgPath: "",
inPathStructSuffix: "Path",
wantNodeDataMap: NodeDataMap{
"DevicePath": {
GoTypeName: "*Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"ParentPath": {
GoTypeName: "*Parent",
LocalGoTypeName: "*Parent",
Expand Down Expand Up @@ -371,6 +389,12 @@ func TestGeneratePathCode(t *testing.T) {
inPathStructSuffix: "Path",
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-unione.path-txt"),
wantNodeDataMap: NodeDataMap{
"DevicePath": {
GoTypeName: "*Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"DupEnumPath": {
GoTypeName: "*DupEnum",
LocalGoTypeName: "*DupEnum",
Expand Down Expand Up @@ -467,6 +491,12 @@ func TestGeneratePathCode(t *testing.T) {
inPathStructSuffix: "Path",
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-unione.path-txt"),
wantNodeDataMap: NodeDataMap{
"DevicePath": {
GoTypeName: "*Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"DupEnumPath": {
GoTypeName: "*DupEnum",
LocalGoTypeName: "*DupEnum",
Expand Down Expand Up @@ -565,6 +595,12 @@ func TestGeneratePathCode(t *testing.T) {
inPathStructSuffix: "Path",
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/enum-module.path-txt"),
wantNodeDataMap: NodeDataMap{
"DevicePath": {
GoTypeName: "*Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"AListPath": {
GoTypeName: "*AList",
LocalGoTypeName: "*AList",
Expand Down Expand Up @@ -692,6 +728,12 @@ func TestGeneratePathCode(t *testing.T) {
inPathStructSuffix: "",
wantStructsCodeFile: filepath.Join(TestRoot, "testdata/structs/openconfig-augmented.path-txt"),
wantNodeDataMap: NodeDataMap{
"Device": {
GoTypeName: "*oc.Device",
LocalGoTypeName: "*Device",
SubsumingGoStructName: "Device",
YANGPath: "/",
},
"Native": {
GoTypeName: "*oc.Native",
LocalGoTypeName: "*Native",
Expand Down

0 comments on commit 5ba0876

Please sign in to comment.