Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
robshakir committed Oct 18, 2017
1 parent 8a1590b commit cdf0354
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
16 changes: 10 additions & 6 deletions ygen/protogen.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,9 @@ func genProto3Msg(msg *yangDirectory, msgs map[string]*yangDirectory, state *gen
if _, ok := imports[childpath]; !ok {
imports[childpath] = true
}
pfx = fmt.Sprintf("%s.", stripPackagePrefix(parentPkg, childpkg))

p, _ := stripPackagePrefix(parentPkg, childpkg)
pfx = fmt.Sprintf("%s.", p)
}
fieldDef.Type = fmt.Sprintf("%s%s", pfx, childmsg.name)
}
Expand Down Expand Up @@ -917,7 +919,8 @@ func genListKeyProto(listPackage string, listName string, args protoDefinitionAr
ctag++
}

ltype := fmt.Sprintf("%s.%s", stripPackagePrefix(args.parentPackage, listPackage), listName)
p, _ := stripPackagePrefix(args.parentPackage, listPackage)
ltype := fmt.Sprintf("%s.%s", p, listName)
if listPackage == "" {
// Handle the case that the context of the list is already the base package.
ltype = listName
Expand Down Expand Up @@ -1063,17 +1066,18 @@ func protoSchemaPathAnnotation(msg *yangDirectory, field *yang.Entry, compressPa
}

// stripPackagePrefix removes the prefix of pfx from the path supplied. If pfx
// is not a prefix of path the entire path is returned.
func stripPackagePrefix(pfx, path string) string {
// is not a prefix of path the entire path is returned. If the prefix was
// stripped, the returned bool is set.
func stripPackagePrefix(pfx, path string) (string, bool) {
pfxP := strings.Split(pfx, ".")
pathP := strings.Split(path, ".")

var i int
for i = range pfxP {
if pfxP[i] != pathP[i] {
return path
return path, false
}
}

return strings.Join(pathP[i+1:], ".")
return strings.Join(pathP[i+1:], "."), true
}
43 changes: 26 additions & 17 deletions ygen/protogen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1509,31 +1509,40 @@ func TestUnionFieldToOneOf(t *testing.T) {

func TestStripPackagePrefix(t *testing.T) {
tests := []struct {
name string
inPrefix string
inPath string
want string
name string
inPrefix string
inPath string
want string
wantStripped bool
}{{
name: "invalid prefix at element one",
inPrefix: "one.two",
inPath: "two.four",
want: "two.four",
name: "invalid prefix at element one",
inPrefix: "one.two",
inPath: "two.four",
want: "two.four",
wantStripped: false,
}, {
name: "single element prefix",
inPrefix: "one",
inPath: "one.three",
want: "three",
name: "single element prefix",
inPrefix: "one",
inPath: "one.three",
want: "three",
wantStripped: true,
}, {
name: "longer prefix",
inPrefix: "one.two.three",
inPath: "one.two.three.five",
want: "five",
name: "longer prefix",
inPrefix: "one.two.three",
inPath: "one.two.three.five",
want: "five",
wantStripped: true,
}}

for _, tt := range tests {
if got := stripPackagePrefix(tt.inPrefix, tt.inPath); got != tt.want {
got, stripped := stripPackagePrefix(tt.inPrefix, tt.inPath)
if got != tt.want {
t.Errorf("%s: stripPackagePrefix(%s, %s): did not get expected output, got: %s, want: %s", tt.name, tt.inPrefix, tt.inPath, got, tt.want)
}

if stripped != tt.wantStripped {
t.Errorf("%s: stripPackagePrefix(%s, %s): did not get expected stipped status, got: %v, want: %v", tt.name, tt.inPrefix, tt.inPath, stripped, tt.wantStripped)
}
}

}

0 comments on commit cdf0354

Please sign in to comment.