Skip to content

Commit

Permalink
Add DiffOpt to exclude new paths. (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-lepage authored Jul 28, 2021
1 parent 2e7c3ed commit 3361cdb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
23 changes: 22 additions & 1 deletion ygot/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,25 @@ type DiffOpt interface {
IsDiffOpt()
}

// IgnoreAdditions is a DiffOpt that indicates newly-added fields should be
// ignored. The returned Notification will only contain the updates and
// deletions from original to modified.
type IgnoreAdditions struct{}

func (*IgnoreAdditions) IsDiffOpt() {}

// hasIgnoreAdditions returns the first IgnoreAdditions from an opts slice, or
// nil if there isn't one.
func hasIgnoreAdditions(opts []DiffOpt) *IgnoreAdditions {
for _, o := range opts {
switch v := o.(type) {
case *IgnoreAdditions:
return v
}
}
return nil
}

// DiffPathOpt is a DiffOpt that allows control of the path behaviour of the
// Diff function.
type DiffPathOpt struct {
Expand Down Expand Up @@ -432,7 +451,9 @@ func Diff(original, modified GoStruct, opts ...DiffOpt) (*gnmipb.Notification, e
n.Delete = append(n.Delete, origPath.gNMIPaths...)
}
}

if hasIgnoreAdditions(opts) != nil {
return n, nil
}
// Check that all paths that are in the modified struct have been examined, if
// not they are updates.
for modPath, modVal := range modLeaves {
Expand Down
28 changes: 28 additions & 0 deletions ygot/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,34 @@ func TestDiff(t *testing.T) {
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"cabernet-sauvignon"}},
}},
},
}, {
desc: "one path each modified, deleted, and added with IgnoreNewPaths set",
inOrig: &renderExample{
IntVal: Int32(5),
FloatVal: Float32(1.5),
Int64Val: Int64(100),
},
inMod: &renderExample{
IntVal: Int32(10),
Str: String("cabernet-sauvignon"),
Int64Val: Int64(100),
},
inOpts: []DiffOpt{&IgnoreAdditions{}},
want: &gnmipb.Notification{
Delete: []*gnmipb.Path{{
Elem: []*gnmipb.PathElem{{
Name: "floatval",
}},
}},
Update: []*gnmipb.Update{{
Path: &gnmipb.Path{
Elem: []*gnmipb.PathElem{{
Name: "int-val",
}},
},
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_IntVal{10}},
}},
},
}, {
desc: "extra empty child struct in modified -- no difference",
inOrig: &renderExample{},
Expand Down

0 comments on commit 3361cdb

Please sign in to comment.