Skip to content

Commit 3361cdb

Browse files
authored
Add DiffOpt to exclude new paths. (#552)
1 parent 2e7c3ed commit 3361cdb

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

ygot/diff.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,25 @@ type DiffOpt interface {
353353
IsDiffOpt()
354354
}
355355

356+
// IgnoreAdditions is a DiffOpt that indicates newly-added fields should be
357+
// ignored. The returned Notification will only contain the updates and
358+
// deletions from original to modified.
359+
type IgnoreAdditions struct{}
360+
361+
func (*IgnoreAdditions) IsDiffOpt() {}
362+
363+
// hasIgnoreAdditions returns the first IgnoreAdditions from an opts slice, or
364+
// nil if there isn't one.
365+
func hasIgnoreAdditions(opts []DiffOpt) *IgnoreAdditions {
366+
for _, o := range opts {
367+
switch v := o.(type) {
368+
case *IgnoreAdditions:
369+
return v
370+
}
371+
}
372+
return nil
373+
}
374+
356375
// DiffPathOpt is a DiffOpt that allows control of the path behaviour of the
357376
// Diff function.
358377
type DiffPathOpt struct {
@@ -432,7 +451,9 @@ func Diff(original, modified GoStruct, opts ...DiffOpt) (*gnmipb.Notification, e
432451
n.Delete = append(n.Delete, origPath.gNMIPaths...)
433452
}
434453
}
435-
454+
if hasIgnoreAdditions(opts) != nil {
455+
return n, nil
456+
}
436457
// Check that all paths that are in the modified struct have been examined, if
437458
// not they are updates.
438459
for modPath, modVal := range modLeaves {

ygot/diff_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,34 @@ func TestDiff(t *testing.T) {
838838
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_StringVal{"cabernet-sauvignon"}},
839839
}},
840840
},
841+
}, {
842+
desc: "one path each modified, deleted, and added with IgnoreNewPaths set",
843+
inOrig: &renderExample{
844+
IntVal: Int32(5),
845+
FloatVal: Float32(1.5),
846+
Int64Val: Int64(100),
847+
},
848+
inMod: &renderExample{
849+
IntVal: Int32(10),
850+
Str: String("cabernet-sauvignon"),
851+
Int64Val: Int64(100),
852+
},
853+
inOpts: []DiffOpt{&IgnoreAdditions{}},
854+
want: &gnmipb.Notification{
855+
Delete: []*gnmipb.Path{{
856+
Elem: []*gnmipb.PathElem{{
857+
Name: "floatval",
858+
}},
859+
}},
860+
Update: []*gnmipb.Update{{
861+
Path: &gnmipb.Path{
862+
Elem: []*gnmipb.PathElem{{
863+
Name: "int-val",
864+
}},
865+
},
866+
Val: &gnmipb.TypedValue{Value: &gnmipb.TypedValue_IntVal{10}},
867+
}},
868+
},
841869
}, {
842870
desc: "extra empty child struct in modified -- no difference",
843871
inOrig: &renderExample{},

0 commit comments

Comments
 (0)