@@ -487,3 +487,88 @@ func timePtrsEqual(a, b *time.Time) bool {
487487 }
488488 return a .Equal (* b )
489489}
490+
491+ func TestPanicOnVisibleFieldsIndexMismatch (t * testing.T ) {
492+ type Embedded struct {
493+ Secret string
494+ }
495+ type Outer struct {
496+ Embedded // Promoted field
497+ Age int
498+ }
499+
500+ // This will panic with:
501+ // panic: reflect: Field index out of bounds
502+ _ = DumpStr (Outer {
503+ Embedded : Embedded {Secret : "classified" },
504+ Age : 42 ,
505+ })
506+ }
507+
508+ type FriendlyDuration time.Duration
509+
510+ func (fd FriendlyDuration ) String () string {
511+ td := time .Duration (fd )
512+ return fmt .Sprintf ("%02d:%02d:%02d" , int (td .Hours ()), int (td .Minutes ())% 60 , int (td .Seconds ())% 60 )
513+ }
514+
515+ func TestTheKitchenSink (t * testing.T ) {
516+
517+ type Inner struct {
518+ ID int
519+ Notes []string
520+ }
521+
522+ type Ref struct {
523+ Self * Ref
524+ }
525+
526+ type Everything struct {
527+ String string
528+ Bool bool
529+ Int int
530+ Float float64
531+ Time time.Time
532+ Duration time.Duration
533+ Friendly FriendlyDuration
534+ PtrString * string
535+ PtrDuration * time.Duration
536+ SliceInts []int
537+ ArrayStrings [2 ]string
538+ MapValues map [string ]int
539+ Nested Inner
540+ NestedPtr * Inner
541+ Interface any
542+ Recursive * Ref
543+ privateField string
544+ privateStruct Inner
545+ }
546+
547+ now := time .Now ()
548+ ptrStr := "Hello"
549+ dur := time .Minute * 20
550+
551+ val := Everything {
552+ String : "test" ,
553+ Bool : true ,
554+ Int : 42 ,
555+ Float : 3.1415 ,
556+ Time : now ,
557+ Duration : dur ,
558+ Friendly : FriendlyDuration (dur ),
559+ PtrString : & ptrStr ,
560+ PtrDuration : & dur ,
561+ SliceInts : []int {1 , 2 , 3 },
562+ ArrayStrings : [2 ]string {"foo" , "bar" },
563+ MapValues : map [string ]int {"a" : 1 , "b" : 2 },
564+ Nested : Inner {ID : 10 , Notes : []string {"alpha" , "beta" }},
565+ NestedPtr : & Inner {ID : 99 , Notes : []string {"x" , "y" }},
566+ Interface : map [string ]bool {"ok" : true },
567+ Recursive : & Ref {},
568+ privateField : "should show" ,
569+ privateStruct : Inner {ID : 5 , Notes : []string {"private" }},
570+ }
571+ val .Recursive .Self = val .Recursive // cycle
572+
573+ Dump (val )
574+ }
0 commit comments