Skip to content

Commit

Permalink
Add Functionality to Trim Organization Name from Module Name (#453)
Browse files Browse the repository at this point in the history
* Add Functionality to Trim Organization from Module Name
  • Loading branch information
wenovus authored Oct 2, 2020
1 parent 9fef924 commit 14d9a77
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 8 deletions.
21 changes: 19 additions & 2 deletions genutil/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,30 @@ func ParentModuleName(node yang.Node) string {
// supplied as the node argument. If the discovered root node of the node is found
// to be a submodule, the name of the parent module is returned. If the root has
// a camel case extension, this is returned rather than the actual module name.
func ParentModulePrettyName(node yang.Node) string {
// If organization prefixes (e.g. "openconfig", "ietf") are given, they are
// trimmed from the module name if a match is found.
func ParentModulePrettyName(node yang.Node, orgPrefixesToTrim ...string) string {
definingMod := definingModule(node)
if name, ok := CamelCaseNameExt(definingMod.Exts()); ok {
return name
}

return yang.CamelCase(definingMod.NName())
return yang.CamelCase(TrimOrgPrefix(definingMod.NName(), orgPrefixesToTrim...))
}

// TrimOrgPrefix checks each input organization prefix (e.g. "openconfig", "ietf")
// (https://tools.ietf.org/html/rfc8407#section-4.1), and if matching the input
// module name, trims it and returns it. If none is matching, the original
// module name is returned.
// E.g. If "openconfig" is provided as a prefix to trim, then
// "openconfig-interfaces" becomes simply "interfaces".
func TrimOrgPrefix(modName string, orgPrefixesToTrim ...string) string {
for _, pfx := range orgPrefixesToTrim {
if trimmedModName := strings.TrimPrefix(modName, pfx+"-"); trimmedModName != modName {
return trimmedModName
}
}
return modName
}

// MakeNameUnique makes the name specified as an argument unique based on the names
Expand Down
97 changes: 91 additions & 6 deletions genutil/names_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ func TestCamelCase(t *testing.T) {

func TestDefiningModule(t *testing.T) {
tests := []struct {
name string
inNode yang.Node
wantNode yang.Node
wantName string
wantPrettyName string
name string
inNode yang.Node
inOrgPrefixesToTrim []string
wantNode yang.Node
wantName string
wantPrettyName string
}{{
name: "direct child of module",
inNode: &yang.Container{
Expand Down Expand Up @@ -165,6 +166,48 @@ func TestDefiningModule(t *testing.T) {
},
wantName: "root",
wantPrettyName: "FooBar",
}, {
name: "direct child of module, with trimming",
inNode: &yang.Container{
Name: "child",
Parent: &yang.Module{
Name: "apple-parent",
},
},
inOrgPrefixesToTrim: []string{"apple", "banana"},
wantNode: &yang.Module{
Name: "apple-parent",
},
wantName: "apple-parent",
wantPrettyName: "Parent",
}, {
name: "direct child of module, with trimming using a different name",
inNode: &yang.Container{
Name: "child",
Parent: &yang.Module{
Name: "banana-parent",
},
},
inOrgPrefixesToTrim: []string{"apple", "banana"},
wantNode: &yang.Module{
Name: "banana-parent",
},
wantName: "banana-parent",
wantPrettyName: "Parent",
}, {
name: "direct child of module, with trimming but without match",
inNode: &yang.Container{
Name: "child",
Parent: &yang.Module{
Name: "cherry-parent",
},
},
inOrgPrefixesToTrim: []string{"apple", "banana"},
wantNode: &yang.Module{
Name: "cherry-parent",
},
wantName: "cherry-parent",
wantPrettyName: "CherryParent",
}}

for _, tt := range tests {
Expand All @@ -180,9 +223,51 @@ func TestDefiningModule(t *testing.T) {
if got := ParentModuleName(tt.inNode); !cmp.Equal(got, tt.wantName) {
t.Errorf("did not get expected parent name, got: %s, want: %s", got, tt.wantName)
}
if got := ParentModulePrettyName(tt.inNode); !cmp.Equal(got, tt.wantPrettyName) {
if got := ParentModulePrettyName(tt.inNode, tt.inOrgPrefixesToTrim...); !cmp.Equal(got, tt.wantPrettyName) {
t.Errorf("did not get expected parent pretty name, got: %s, want: %s", got, tt.wantPrettyName)
}
})
}
}

func TestTrimOrgPrefix(t *testing.T) {
tests := []struct {
desc string
inModName string
inOrgPrefixesToTrim []string
want string
}{{
desc: "basic",
inModName: "openconfig-interfaces",
inOrgPrefixesToTrim: []string{"openconfig"},
want: "interfaces",
}, {
desc: "no prefixes",
inModName: "openconfig-interfaces",
inOrgPrefixesToTrim: nil,
want: "openconfig-interfaces",
}, {
desc: "second prefix",
inModName: "openconfig2-interfaces",
inOrgPrefixesToTrim: []string{"openconfig", "openconfig2"},
want: "interfaces",
}, {
desc: "no match",
inModName: "openconfig-interfaces",
inOrgPrefixesToTrim: []string{"openconfig1", "openconfig2"},
want: "openconfig-interfaces",
}, {
desc: "two matches, but only the first one should apply",
inModName: "openconfig-openconfig2-interfaces",
inOrgPrefixesToTrim: []string{"openconfig", "openconfig2"},
want: "openconfig2-interfaces",
}}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
if diff := cmp.Diff(TrimOrgPrefix(tt.inModName, tt.inOrgPrefixesToTrim...), tt.want); diff != "" {
t.Errorf("(-got, +want):\n%s", diff)
}
})
}
}

0 comments on commit 14d9a77

Please sign in to comment.