Skip to content

Commit

Permalink
Improve output description for SetRequest diff. (#796)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenovus authored Mar 24, 2023
1 parent b519912 commit 90e7cdd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 2 additions & 2 deletions gnmidiff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $ go install ./gnmidiff
$ gnmidiff setrequest cmd/demo/setrequest.textproto cmd/demo/setrequest2.textproto

SetRequestIntentDiff(-A, +B):
-------- deletes --------
+ /network-instances/network-instance[name=VrfBlue]: deleted
-------- deletes/replaces --------
+ /network-instances/network-instance[name=VrfBlue]: deleted or replaced only in B
-------- updates --------
m /system/config/hostname:
- "violetsareblue"
Expand Down
24 changes: 22 additions & 2 deletions gnmidiff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ type Format struct {
aName string
// bName is an optional custom name for B in the diff.
bName string
// deleteTitle is an optional custom name when titling the diff section
// with delete paths.
deleteTitle string
// deleteDesc is an optional custom description when describing a
// delete path in the diff.
deleteDesc string
}

func formatJSONValue(value interface{}) interface{} {
Expand All @@ -115,11 +121,17 @@ func (diff StructuredDiff) Format(f Format) string {
if f.bName == "" {
f.bName = "B"
}
if f.deleteTitle == "" {
f.deleteDesc = "deletes"
}
if f.deleteDesc == "" {
f.deleteTitle = "deleted only in %s"
}
b.WriteString(fmt.Sprintf("%s(-%s, +%s):\n", f.title, f.aName, f.bName))

deleteDiff := diff.DeleteDiff.format(f)
if deleteDiff != "" {
b.WriteString("-------- deletes --------\n")
b.WriteString(fmt.Sprintf("-------- %s --------\n", f.deleteTitle))
b.WriteString(deleteDiff)
b.WriteString("-------- updates --------\n")
}
Expand Down Expand Up @@ -177,7 +189,15 @@ func (diff DeleteDiff) format(f Format) string {
}
sort.Strings(paths)
for _, path := range paths {
b.WriteString(fmt.Sprintf("%c %s: deleted\n", symbol, path))
b.WriteString(fmt.Sprintf("%c %s: "+f.deleteDesc, symbol, path))
switch symbol {
case '-':
b.WriteString(fmt.Sprintf(" only in %s\n", f.aName))
case '+':
b.WriteString(fmt.Sprintf(" only in %s\n", f.bName))
default:
b.WriteString("\n")
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions gnmidiff/setrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (diff SetRequestIntentDiff) Format(f Format) string {
f.title = "SetRequestIntentDiff"
f.aName = "A"
f.bName = "B"
f.deleteTitle = "deletes/replaces"
f.deleteDesc = "deleted or replaced"
return StructuredDiff(diff).Format(f)
}

Expand Down
14 changes: 7 additions & 7 deletions gnmidiff/setrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func TestSetRequestDiffFormat(t *testing.T) {
},
inFormat: Format{},
want: `SetRequestIntentDiff(-A, +B):
-------- deletes --------
- /interfaces/interface[name=eth1]: deleted
+ /interfaces/interface[name=eth2]: deleted
-------- deletes/replaces --------
- /interfaces/interface[name=eth1]: deleted or replaced only in A
+ /interfaces/interface[name=eth2]: deleted or replaced only in B
-------- updates --------
- /interfaces/interface[name=eth1]/config/name: "eth1"
- /interfaces/interface[name=eth1]/name: "eth1"
Expand Down Expand Up @@ -146,10 +146,10 @@ m /interfaces/interface[name=eth0]/subinterfaces/subinterface[index=0]/state/nam
Full: true,
},
want: `SetRequestIntentDiff(-A, +B):
-------- deletes --------
/interfaces/interface[name=eth0]: deleted
- /interfaces/interface[name=eth1]: deleted
+ /interfaces/interface[name=eth2]: deleted
-------- deletes/replaces --------
/interfaces/interface[name=eth0]: deleted or replaced
- /interfaces/interface[name=eth1]: deleted or replaced only in A
+ /interfaces/interface[name=eth2]: deleted or replaced only in B
-------- updates --------
/interfaces/interface[name=eth0]/config/description: "I am an eth port"
/interfaces/interface[name=eth0]/config/name: "eth0"
Expand Down

0 comments on commit 90e7cdd

Please sign in to comment.