Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ygot.FormatDiff #929

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions ygot/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/openconfig/gnmi/errlist"
"github.com/openconfig/ygot/internal/yreflect"
"github.com/openconfig/ygot/util"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"

gnmipb "github.com/openconfig/gnmi/proto/gnmi"
Expand Down Expand Up @@ -488,6 +489,41 @@ func Diff(original, modified GoStruct, opts ...DiffOpt) (*gnmipb.Notification, e
}
}

// FormatDiff formats the output of ygot.Diff as a multiline string. This
// function is only intended for human consumption and ignores errors. Do not
// depend on the output being stable. It may change over time across different
// versions of the program.
func FormatDiff(n *gnmipb.Notification) string {
wenovus marked this conversation as resolved.
Show resolved Hide resolved
if n == nil {
return "<nil> notification"
}
var build strings.Builder
for _, d := range n.Delete {
path, err := PathToString(d)
if err != nil {
path = prototext.Format(d)
}
if build.Len() != 0 {
build.WriteRune('\n')
}
build.WriteString(fmt.Sprintf("deleted: %s", path))
}
for _, u := range n.Update {
path, err := PathToString(u.GetPath())
if err != nil {
path = prototext.Format(u.GetPath())
}
if build.Len() != 0 {
build.WriteRune('\n')
}
build.WriteString(fmt.Sprintf("new/updated %s: %v", path, u.GetVal()))
}
if build.Len() == 0 {
return "no diff"
}
return build.String()
}

// DiffWithAtomic takes an original and modified GoStruct, which must be of the same
// type and returns a slice of gNMI Notifications that represents the diff
// between them in a way that can be used to update a local gNMI.Subscribe
Expand Down
6 changes: 6 additions & 0 deletions ygot/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,9 @@ func TestDiff(t *testing.T) {
t.Run(tt.desc+"Diff", func(t *testing.T) {
got, err := Diff(tt.inOrig, tt.inMod, tt.inOpts...)
testDiffSingleNotif(t, "Diff", got, err)
if diffout := FormatDiff(got); diffout == "" {
wenovus marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("FormatDiff returned empty")
}
})
t.Run(tt.desc+"DiffWithAtomic", func(t *testing.T) {
var got *gnmipb.Notification
Expand All @@ -1589,6 +1592,9 @@ func TestDiff(t *testing.T) {
t.Fatalf("Got multiple Notifications for DiffWithAtomic: %d, this is not expected.", len(gots))
}
testDiffSingleNotif(t, "DiffWithAtomic", got, err)
if diffout := FormatDiff(got); diffout == "" {
t.Errorf("FormatDiff returned empty")
}
})
}
}
Expand Down
Loading