From 4efbaded5dd394d8f17ca92f75d09fb140310de4 Mon Sep 17 00:00:00 2001 From: Alex Viscreanu Date: Wed, 8 Mar 2023 20:08:23 +0100 Subject: [PATCH] fix: report create/delete differences on empty structs --- diff_struct.go | 14 ++++++++++++++ diff_test.go | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/diff_struct.go b/diff_struct.go index fb14c57..cd51c6d 100644 --- a/diff_struct.go +++ b/diff_struct.go @@ -85,6 +85,20 @@ func (d *Differ) structValues(t string, path []string, a reflect.Value) error { return ErrTypeMismatch } + // empty struct + // add to changelog and return + if a.NumField() == 0 { + var from, to interface{} + + from, to = nil, exportInterface(a) + if t == DELETE { + from, to = to, nil + } + + d.cl.Add(t, path, from, to) + return nil + } + x := reflect.New(a.Type()).Elem() for i := 0; i < a.NumField(); i++ { diff --git a/diff_test.go b/diff_test.go index 4008623..86257d1 100644 --- a/diff_test.go +++ b/diff_test.go @@ -618,6 +618,20 @@ func TestDiff(t *testing.T) { diff.Changelog{}, nil, }, + { + "map-empty-struct-value", + map[string]struct{}{ + "one": {}, + }, + map[string]struct{}{ + "two": {}, + }, + diff.Changelog{ + diff.Change{Type: diff.DELETE, Path: []string{"one"}, From: struct{}{}, To: nil}, + diff.Change{Type: diff.CREATE, Path: []string{"two"}, From: nil, To: struct{}{}}, + }, + nil, + }, } for _, tc := range cases {