Skip to content

Commit 4fde30c

Browse files
committed
fix: fixes panic on unexported field access.
1 parent 61ca8aa commit 4fde30c

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

dump.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ func (s *dumpState) dumpSlice(v reflect.Value) {
134134
}
135135

136136
func (s *dumpState) dumpStruct(v reflect.Value) {
137-
val := v.Interface()
138-
if t, ok := val.(time.Time); ok && s.timeFormatter != nil {
139-
s.writeString(s.timeFormatter(t))
140-
return
137+
if v.CanInterface() {
138+
val := v.Interface()
139+
if t, ok := val.(time.Time); ok && s.timeFormatter != nil {
140+
s.writeString(s.timeFormatter(t))
141+
return
142+
}
141143
}
142144

143145
dumpPreamble := func() {

dump_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,19 @@ func TestSdump_RecursiveMaps(t *testing.T) {
263263
runTests(t, "recursive_maps", mp)
264264
}
265265

266+
type unexportedStruct struct {
267+
x int
268+
}
269+
type StructWithUnexportedType struct {
270+
unexported unexportedStruct
271+
}
272+
273+
func TestSdump_unexported(t *testing.T) {
274+
runTests(t, "unexported", StructWithUnexportedType{
275+
unexported: unexportedStruct{},
276+
})
277+
}
278+
266279
var standardCfg = litter.Options{}
267280

268281
func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...interface{}) {

testdata/unexported.dump

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
litter_test.StructWithUnexportedType{
2+
unexported: litter_test.unexportedStruct{
3+
x: 0,
4+
},
5+
}

0 commit comments

Comments
 (0)