From 1352d2502ea919e89bd9dd91050c63709d8032df Mon Sep 17 00:00:00 2001 From: Tom Bevan Date: Mon, 24 May 2021 13:45:35 +0100 Subject: [PATCH] fix calling interface on struct values that are private --- diff_struct.go | 5 +++++ diff_test.go | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/diff_struct.go b/diff_struct.go index 4e7045f..e1dcb5c 100644 --- a/diff_struct.go +++ b/diff_struct.go @@ -54,6 +54,11 @@ func (d *Differ) diffStruct(path []string, a, b reflect.Value) error { continue } + // skip private fields + if !a.CanInterface() { + continue + } + err := d.diff(fpath, af, bf, a.Interface()) if err != nil { return err diff --git a/diff_test.go b/diff_test.go index f39c81d..a0427c2 100644 --- a/diff_test.go +++ b/diff_test.go @@ -6,6 +6,7 @@ package diff import ( "reflect" + "sync" "testing" "time" @@ -51,6 +52,11 @@ type customTagStruct struct { Bar int `json:"bar"` } +type privateValueStruct struct { + Public string + Private *sync.RWMutex +} + type CustomStringType string type CustomIntType int type customTypeStruct struct { @@ -352,6 +358,13 @@ func TestDiff(t *testing.T) { }, nil, }, + { + "struct-with-private-value", privateValueStruct{Public: "one", Private: new(sync.RWMutex)}, privateValueStruct{Public: "two", Private: new(sync.RWMutex)}, + Changelog{ + Change{Type: UPDATE, Path: []string{"Public"}, From: "one", To: "two"}, + }, + nil, + }, { "mismatched-values-struct-map", map[string]string{"test": "one"}, &tstruct{Identifiables: []tistruct{{"one", 1}}}, Changelog{},