File size: 1,572 Bytes
e36aeda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
	"reflect"
	"strconv"
)

func pprints(v any) string {
	var pp pprinter
	pp.val(reflect.ValueOf(v), 0)
	return string(pp.buf)
}

type pprinter struct {
	buf []byte
}

func (p *pprinter) indent(by int) {
	for range by {
		p.buf = append(p.buf, '\t')
	}
}

func (p *pprinter) val(v reflect.Value, indent int) {
	switch v.Kind() {
	default:
		p.buf = fmt.Appendf(p.buf, "unsupported kind %v", v.Kind())

	case reflect.Bool:
		p.buf = strconv.AppendBool(p.buf, v.Bool())

	case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
		p.buf = strconv.AppendInt(p.buf, v.Int(), 10)

	case reflect.String:
		p.buf = strconv.AppendQuote(p.buf, v.String())

	case reflect.Pointer:
		if v.IsNil() {
			p.buf = append(p.buf, "nil"...)
		} else {
			p.buf = append(p.buf, "&"...)
			p.val(v.Elem(), indent)
		}

	case reflect.Slice, reflect.Array:
		p.buf = append(p.buf, "[\n"...)
		for i := range v.Len() {
			p.indent(indent + 1)
			p.val(v.Index(i), indent+1)
			p.buf = append(p.buf, ",\n"...)
		}
		p.indent(indent)
		p.buf = append(p.buf, ']')

	case reflect.Struct:
		vt := v.Type()
		p.buf = append(append(p.buf, vt.String()...), "{\n"...)
		for f := range v.NumField() {
			p.indent(indent + 1)
			p.buf = append(append(p.buf, vt.Field(f).Name...), ": "...)
			p.val(v.Field(f), indent+1)
			p.buf = append(p.buf, ",\n"...)
		}
		p.indent(indent)
		p.buf = append(p.buf, '}')
	}
}