Skip to content

Commit c667114

Browse files
committed
funcr: Handle and test recursive values
1 parent f7ffadb commit c667114

2 files changed

Lines changed: 354 additions & 152 deletions

File tree

‎funcr/funcr.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ func (f Formatter) prettyWithFlags(value any, flags uint32, depth int) string {
637637
keystr = prettyString(keystr)
638638
} else {
639639
// prettyWithFlags will produce already-escaped values
640-
keystr = f.prettyWithFlags(it.Key().Interface(), 0, depth+1)
640+
keystr = f.prettyWithFlags(it.Key().Interface(), 0, 0) // key depth is unrelated to overall depth
641641
if t.Key().Kind() != reflect.String {
642642
// JSON only does string keys. Unlike Go's standard JSON, we'll
643643
// convert just about anything to a string.
@@ -655,6 +655,12 @@ func (f Formatter) prettyWithFlags(value any, flags uint32, depth int) string {
655655
if v.IsNil() {
656656
return "null"
657657
}
658+
// Special case: if the pointer points to itself, we have a cycle.
659+
// This should not happen accidentally (e.g. json decoding should never
660+
// do this) but we should handle it gracefully.
661+
if reflect.DeepEqual(v.Elem().Interface(), v.Interface()) {
662+
depth = f.opts.MaxLogDepth + 1 // force a depth error
663+
}
658664
return f.prettyWithFlags(v.Elem().Interface(), 0, depth)
659665
}
660666
return fmt.Sprintf(`"<unhandled-%s>"`, t.Kind().String())

0 commit comments

Comments
 (0)