Skip to content

Commit

Permalink
Forcibly export fields for use by the reporter
Browse files Browse the repository at this point in the history
The choice to avoid traversing unexported fields by default makes
sense for the semantics of normal comparisons. However, the goal
of the reporter is to prioritize humanly readable output.
As such, it seems appropriate to forcibly export fields.
This allows the String method to be called on such values.
  • Loading branch information
dsnet committed Jun 8, 2020
1 parent 4a83f56 commit 1776240
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion cmp/report_reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ func (opts formatOptions) FormatValue(v reflect.Value, withinSlice bool, m visit
return textLine(formatPointer(v))
case reflect.Struct:
var list textList
v := makeAddressable(v) // needed for retrieveUnexportedField
for i := 0; i < v.NumField(); i++ {
vv := v.Field(i)
if value.IsZero(vv) {
continue // Elide fields with zero values
}
sf := t.Field(i)
if supportExporters && !isExported(sf.Name) {
vv = retrieveUnexportedField(v, sf)
}
s := opts.WithTypeMode(autoType).FormatValue(vv, false, m)
list = append(list, textRecord{Key: t.Field(i).Name, Value: s})
list = append(list, textRecord{Key: sf.Name, Value: s})
}
return textWrap{"{", list, "}"}
case reflect.Slice:
Expand Down
6 changes: 3 additions & 3 deletions cmp/testdata/diffs
Original file line number Diff line number Diff line change
Expand Up @@ -1154,10 +1154,10 @@
CleanGerms: nil,
GermMap: map[int32]*testprotos.Germ{13: s"germ13", 21: s"germ21"},
DishMap: map[int32]*teststructs.Dish{
0: &{err: &errors.errorString{s: "EOF"}},
0: &{err: e"EOF"},
- 1: nil,
+ 1: &{err: &errors.errorString{s: "unexpected EOF"}},
2: &{pb: &testprotos.Dish{Stringer: testprotos.Stringer{X: "dish"}}},
+ 1: &{err: e"unexpected EOF"},
2: &{pb: s"dish"},
},
HasPreviousResult: true,
DirtyID: 10,
Expand Down

2 comments on commit 1776240

@dsnet
Copy link
Collaborator Author

@dsnet dsnet commented on 1776240 Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\cc @cybrcodr. Sigh, I accidentally committed a change directly to master instead of branch for code review. Can you review this after the fact?

I miss Gerrit.

@cybrcodr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. LGTM.

Please sign in to comment.