From 41a4a2e0fcbf192e6836e0c426627145c48f7d07 Mon Sep 17 00:00:00 2001 From: Wen Bo Li <50884368+wenovus@users.noreply.github.com> Date: Mon, 31 Jul 2023 13:21:26 -0700 Subject: [PATCH] Add SchemaPath field to ParsedDirectory (#902) --- gogen/genir_test.go | 155 +++++++++++++++++++++++------------------ protogen/genir_test.go | 92 +++++++++++++----------- ygen/directory.go | 1 + ygen/ir.go | 3 + 4 files changed, 146 insertions(+), 105 deletions(-) diff --git a/gogen/genir_test.go b/gogen/genir_test.go index 71dfc2bc..0776da3c 100644 --- a/gogen/genir_test.go +++ b/gogen/genir_test.go @@ -69,9 +69,10 @@ func TestGenerateIR(t *testing.T) { wantIR: &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "parent": { Name: "Parent", @@ -116,9 +117,10 @@ func TestGenerateIR(t *testing.T) { IsFakeRoot: true, }, "/openconfig-simple/parent": { - Name: "Parent", - Type: ygen.Container, - Path: "/openconfig-simple/parent", + Name: "Parent", + Type: ygen.Container, + Path: "/openconfig-simple/parent", + SchemaPath: "/parent", Fields: map[string]*ygen.NodeDetails{ "child": { Name: "Child", @@ -146,9 +148,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child": { - Name: "Parent_Child", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child", + Name: "Parent_Child", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child", + SchemaPath: "/parent/child", Fields: map[string]*ygen.NodeDetails{ "zero": { Name: "Zero", @@ -287,9 +290,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/remote-container": { - Name: "RemoteContainer", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container", + Name: "RemoteContainer", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container", + SchemaPath: "/remote-container", Fields: map[string]*ygen.NodeDetails{ "a-leaf": { Name: "ALeaf", @@ -362,9 +366,10 @@ func TestGenerateIR(t *testing.T) { wantIR: &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "parent": { Name: "Parent", @@ -408,9 +413,10 @@ func TestGenerateIR(t *testing.T) { IsFakeRoot: true, }, "/openconfig-simple/parent": { - Name: "Parent", - Type: ygen.Container, - Path: "/openconfig-simple/parent", + Name: "Parent", + Type: ygen.Container, + Path: "/openconfig-simple/parent", + SchemaPath: "/parent", Fields: map[string]*ygen.NodeDetails{ "child": { Name: "Child", @@ -438,9 +444,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child": { - Name: "Parent_Child", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child", + Name: "Parent_Child", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child", + SchemaPath: "/parent/child", Fields: map[string]*ygen.NodeDetails{ "zero": { Name: "Zero", @@ -579,9 +586,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/remote-container": { - Name: "RemoteContainer", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container", + Name: "RemoteContainer", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container", + SchemaPath: "/remote-container", Fields: map[string]*ygen.NodeDetails{ "a-leaf": { Name: "ALeaf", @@ -654,9 +662,10 @@ func TestGenerateIR(t *testing.T) { wantIR: &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "parent": { Name: "Parent", @@ -700,9 +709,10 @@ func TestGenerateIR(t *testing.T) { IsFakeRoot: true, }, "/openconfig-simple/parent": { - Name: "OpenconfigSimple_Parent", - Type: ygen.Container, - Path: "/openconfig-simple/parent", + Name: "OpenconfigSimple_Parent", + Type: ygen.Container, + Path: "/openconfig-simple/parent", + SchemaPath: "/parent", Fields: map[string]*ygen.NodeDetails{ "child": { Name: "Child", @@ -730,9 +740,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child": { - Name: "OpenconfigSimple_Parent_Child", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child", + Name: "OpenconfigSimple_Parent_Child", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child", + SchemaPath: "/parent/child", Fields: map[string]*ygen.NodeDetails{ "config": { Name: "Config", @@ -775,9 +786,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child/config": { - Name: "OpenconfigSimple_Parent_Child_Config", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child/config", + Name: "OpenconfigSimple_Parent_Child_Config", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child/config", + SchemaPath: "/parent/child/config", Fields: map[string]*ygen.NodeDetails{ "four": { Name: "Four", @@ -865,9 +877,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child/state": { - Name: "OpenconfigSimple_Parent_Child_State", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child/state", + Name: "OpenconfigSimple_Parent_Child_State", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child/state", + SchemaPath: "/parent/child/state", Fields: map[string]*ygen.NodeDetails{ "four": { Name: "Four", @@ -1013,9 +1026,10 @@ func TestGenerateIR(t *testing.T) { ConfigFalse: true, }, "/openconfig-simple/remote-container": { - Name: "OpenconfigSimple_RemoteContainer", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container", + Name: "OpenconfigSimple_RemoteContainer", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container", + SchemaPath: "/remote-container", Fields: map[string]*ygen.NodeDetails{ "config": { Name: "Config", @@ -1058,9 +1072,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-remote", }, "/openconfig-simple/remote-container/config": { - Name: "OpenconfigSimple_RemoteContainer_Config", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container/config", + Name: "OpenconfigSimple_RemoteContainer_Config", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container/config", + SchemaPath: "/remote-container/config", Fields: map[string]*ygen.NodeDetails{ "a-leaf": { Name: "ALeaf", @@ -1094,9 +1109,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-remote", }, "/openconfig-simple/remote-container/state": { - Name: "OpenconfigSimple_RemoteContainer_State", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container/state", + Name: "OpenconfigSimple_RemoteContainer_State", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container/state", + SchemaPath: "/remote-container/state", Fields: map[string]*ygen.NodeDetails{ "a-leaf": { Name: "ALeaf", @@ -1166,9 +1182,10 @@ func TestGenerateIR(t *testing.T) { wantIR: &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "e1": { Name: "E1", @@ -1221,9 +1238,10 @@ func TestGenerateIR(t *testing.T) { wantIR: &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "model": { Name: "Model", @@ -1273,6 +1291,7 @@ func TestGenerateIR(t *testing.T) { Name: "ExamplePresence", Type: ygen.Container, Path: "/openconfig-complex/example-presence", + SchemaPath: "/example-presence", Fields: map[string]*ygen.NodeDetails{}, PackageName: "", ListKeys: nil, @@ -1284,9 +1303,10 @@ func TestGenerateIR(t *testing.T) { CompressedTelemetryAtomic: false, }, "/openconfig-complex/model": { - Name: "Model", - Type: ygen.Container, - Path: "/openconfig-complex/model", + Name: "Model", + Type: ygen.Container, + Path: "/openconfig-complex/model", + SchemaPath: "/model", Fields: map[string]*ygen.NodeDetails{ "anydata-leaf": { Name: "AnydataLeaf", @@ -1395,9 +1415,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-complex", }, "/openconfig-complex/model/a/single-key": { - Name: "Model_SingleKey", - Type: ygen.List, - Path: "/openconfig-complex/model/a/single-key", + Name: "Model_SingleKey", + Type: ygen.List, + Path: "/openconfig-complex/model/a/single-key", + SchemaPath: "/model/a/single-key", Fields: map[string]*ygen.NodeDetails{ "dates": { Name: "Dates", @@ -1704,9 +1725,10 @@ func TestGenerateIR(t *testing.T) { CompressedTelemetryAtomic: true, }, "/openconfig-complex/model/b/multi-key": { - Name: "Model_MultiKey", - Type: ygen.List, - Path: "/openconfig-complex/model/b/multi-key", + Name: "Model_MultiKey", + Type: ygen.List, + Path: "/openconfig-complex/model/b/multi-key", + SchemaPath: "/model/b/multi-key", Fields: map[string]*ygen.NodeDetails{ "key1": { Name: "Key1", @@ -1787,9 +1809,10 @@ func TestGenerateIR(t *testing.T) { CompressedTelemetryAtomic: false, }, "/openconfig-complex/model/c/unkeyed-list": { - Name: "Model_UnkeyedList", - Type: ygen.List, - Path: "/openconfig-complex/model/c/unkeyed-list", + Name: "Model_UnkeyedList", + Type: ygen.List, + Path: "/openconfig-complex/model/c/unkeyed-list", + SchemaPath: "/model/c/unkeyed-list", Fields: map[string]*ygen.NodeDetails{ "field": { Name: "Field", diff --git a/protogen/genir_test.go b/protogen/genir_test.go index 47f17f2d..8dec266a 100644 --- a/protogen/genir_test.go +++ b/protogen/genir_test.go @@ -40,9 +40,10 @@ func protoIR(nestedDirectories bool) *ygen.IR { return &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "model": { Name: "model", @@ -93,6 +94,7 @@ func protoIR(nestedDirectories bool) *ygen.IR { Name: "ExamplePresence", Type: ygen.Container, Path: "/openconfig-complex/example-presence", + SchemaPath: "/example-presence", Fields: map[string]*ygen.NodeDetails{}, PackageName: "", ListKeys: nil, @@ -104,9 +106,10 @@ func protoIR(nestedDirectories bool) *ygen.IR { CompressedTelemetryAtomic: false, }, "/openconfig-complex/model": { - Name: "Model", - Type: ygen.Container, - Path: "/openconfig-complex/model", + Name: "Model", + Type: ygen.Container, + Path: "/openconfig-complex/model", + SchemaPath: "/model", Fields: map[string]*ygen.NodeDetails{ "anydata-leaf": { Name: "anydata_leaf", @@ -215,9 +218,10 @@ func protoIR(nestedDirectories bool) *ygen.IR { DefiningModule: "openconfig-complex", }, "/openconfig-complex/model/a/single-key": { - Name: "SingleKey", - Type: ygen.List, - Path: "/openconfig-complex/model/a/single-key", + Name: "SingleKey", + Type: ygen.List, + Path: "/openconfig-complex/model/a/single-key", + SchemaPath: "/model/a/single-key", Fields: map[string]*ygen.NodeDetails{ "dates": { Name: "dates", @@ -688,9 +692,10 @@ func protoIR(nestedDirectories bool) *ygen.IR { CompressedTelemetryAtomic: true, }, "/openconfig-complex/model/b/multi-key": { - Name: "MultiKey", - Type: ygen.List, - Path: "/openconfig-complex/model/b/multi-key", + Name: "MultiKey", + Type: ygen.List, + Path: "/openconfig-complex/model/b/multi-key", + SchemaPath: "/model/b/multi-key", Fields: map[string]*ygen.NodeDetails{ "key1": { Name: "key1", @@ -825,9 +830,10 @@ func protoIR(nestedDirectories bool) *ygen.IR { CompressedTelemetryAtomic: false, }, "/openconfig-complex/model/c/unkeyed-list": { - Name: "UnkeyedList", - Type: ygen.List, - Path: "/openconfig-complex/model/c/unkeyed-list", + Name: "UnkeyedList", + Type: ygen.List, + Path: "/openconfig-complex/model/c/unkeyed-list", + SchemaPath: "/model/c/unkeyed-list", Fields: map[string]*ygen.NodeDetails{ "field": { Name: "field", @@ -1048,9 +1054,10 @@ func TestGenerateIR(t *testing.T) { wantIR: &ygen.IR{ Directories: map[string]*ygen.ParsedDirectory{ "/device": { - Name: "Device", - Type: ygen.Container, - Path: "/device", + Name: "Device", + Type: ygen.Container, + Path: "/device", + SchemaPath: "", Fields: map[string]*ygen.NodeDetails{ "parent": { Name: "parent", @@ -1094,9 +1101,10 @@ func TestGenerateIR(t *testing.T) { IsFakeRoot: true, }, "/openconfig-simple/parent": { - Name: "Parent", - Type: ygen.Container, - Path: "/openconfig-simple/parent", + Name: "Parent", + Type: ygen.Container, + Path: "/openconfig-simple/parent", + SchemaPath: "/parent", Fields: map[string]*ygen.NodeDetails{ "child": { Name: "child", @@ -1125,9 +1133,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child": { - Name: "Child", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child", + Name: "Child", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child", + SchemaPath: "/parent/child", Fields: map[string]*ygen.NodeDetails{ "config": { Name: "config", @@ -1170,9 +1179,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child/config": { - Name: "Config", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child/config", + Name: "Config", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child/config", + SchemaPath: "/parent/child/config", Fields: map[string]*ygen.NodeDetails{ "four": { Name: "four", @@ -1260,9 +1270,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-simple", }, "/openconfig-simple/parent/child/state": { - Name: "State", - Type: ygen.Container, - Path: "/openconfig-simple/parent/child/state", + Name: "State", + Type: ygen.Container, + Path: "/openconfig-simple/parent/child/state", + SchemaPath: "/parent/child/state", Fields: map[string]*ygen.NodeDetails{ "four": { Name: "four", @@ -1409,9 +1420,10 @@ func TestGenerateIR(t *testing.T) { ConfigFalse: true, }, "/openconfig-simple/remote-container": { - Name: "RemoteContainer", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container", + Name: "RemoteContainer", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container", + SchemaPath: "/remote-container", Fields: map[string]*ygen.NodeDetails{ "config": { Name: "config", @@ -1454,9 +1466,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-remote", }, "/openconfig-simple/remote-container/config": { - Name: "Config", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container/config", + Name: "Config", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container/config", + SchemaPath: "/remote-container/config", Fields: map[string]*ygen.NodeDetails{ "a-leaf": { Name: "a_leaf", @@ -1491,9 +1504,10 @@ func TestGenerateIR(t *testing.T) { DefiningModule: "openconfig-remote", }, "/openconfig-simple/remote-container/state": { - Name: "State", - Type: ygen.Container, - Path: "/openconfig-simple/remote-container/state", + Name: "State", + Type: ygen.Container, + Path: "/openconfig-simple/remote-container/state", + SchemaPath: "/remote-container/state", Fields: map[string]*ygen.NodeDetails{ "a-leaf": { Name: "a_leaf", diff --git a/ygen/directory.go b/ygen/directory.go index 4e3c1094..2a205745 100644 --- a/ygen/directory.go +++ b/ygen/directory.go @@ -172,6 +172,7 @@ func getOrderedDirDetails(langMapper LangMapper, directory map[string]*Directory pd := &ParsedDirectory{ Name: dir.Name, Path: util.SlicePathToString(dir.Path), + SchemaPath: util.SchemaTreePathNoModule(dir.Entry), PackageName: packageName, IsFakeRoot: dir.IsFakeRoot, BelongingModule: belongingModule, diff --git a/ygen/ir.go b/ygen/ir.go index 2d9de2e4..8de84fa6 100644 --- a/ygen/ir.go +++ b/ygen/ir.go @@ -384,6 +384,9 @@ type ParsedDirectory struct { Type DirType // Path specifies the absolute YANG schema path of the node. Path string + // SchemaPath specifies the absolute YANG schema node path. It does not + // include the module name nor choice/case elements in the YANG file. + SchemaPath string // Fields is the set of direct children of the node that are to be // output. It is keyed by the YANG node identifier of the child field // since there could be name conflicts at this processing stage.