Skip to content

Commit

Permalink
fix calling interface on struct values that are private
Browse files Browse the repository at this point in the history
  • Loading branch information
purehyperbole committed May 24, 2021
1 parent 4e4ce4b commit 1352d25
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions diff_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package diff

import (
"reflect"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{},
Expand Down

0 comments on commit 1352d25

Please sign in to comment.