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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
// Package repr attempts to represent Go values in a form that can be copy-and-pasted into source
// code directly.
//
// Some values (such as pointers to basic types) can not be represented directly in
// Go. These values will be output as `&<value>`. eg. `&23`
package repr
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"sort"
"strings"
"time"
"unsafe"
)
var (
// "Real" names of basic kinds, used to differentiate type aliases.
realKindName = map[reflect.Kind]string{
reflect.Bool: "bool",
reflect.Int: "int",
reflect.Int8: "int8",
reflect.Int16: "int16",
reflect.Int32: "int32",
reflect.Int64: "int64",
reflect.Uint: "uint",
reflect.Uint8: "uint8",
reflect.Uint16: "uint16",
reflect.Uint32: "uint32",
reflect.Uint64: "uint64",
reflect.Uintptr: "uintptr",
reflect.Float32: "float32",
reflect.Float64: "float64",
reflect.Complex64: "complex64",
reflect.Complex128: "complex128",
reflect.Array: "array",
reflect.Chan: "chan",
reflect.Func: "func",
reflect.Map: "map",
reflect.Slice: "slice",
reflect.String: "string",
}
goStringerType = reflect.TypeOf((*fmt.GoStringer)(nil)).Elem()
anyType = reflect.TypeOf((*any)(nil)).Elem()
byteSliceType = reflect.TypeOf([]byte{})
)
// Default prints to os.Stdout with two space indentation.
var Default = New(os.Stdout, Indent(" "))
// An Option modifies the default behaviour of a Printer.
type Option func(o *Printer)
// Indent output by this much.
func Indent(indent string) Option { return func(o *Printer) { o.indent = indent } }
// NoIndent disables indenting.
func NoIndent() Option { return Indent("") }
// OmitEmpty sets whether empty field members should be omitted from output.
func OmitEmpty(omitEmpty bool) Option { return func(o *Printer) { o.omitEmpty = omitEmpty } }
// ExplicitTypes adds explicit typing to slice and map struct values that would normally be inferred by Go.
func ExplicitTypes(ok bool) Option { return func(o *Printer) { o.explicitTypes = true } }
// IgnoreGoStringer disables use of the .GoString() method.
func IgnoreGoStringer() Option { return func(o *Printer) { o.ignoreGoStringer = true } }
// IgnorePrivate disables private field members from output.
func IgnorePrivate() Option { return func(o *Printer) { o.ignorePrivate = true } }
// ScalarLiterals forces the use of literals for scalars, rather than a string representation if available.
//
// For example, `time.Hour` will be printed as `time.Duration(3600000000000)` rather than `time.Duration(1h0m0s)`.
func ScalarLiterals() Option { return func(o *Printer) { o.useLiterals = true } }
// Hide excludes fields of the given type from representation.
func Hide[T any]() Option {
return func(o *Printer) {
t := (*T)(nil) // A bit of skulduggery so we can Hide() interfaces.
rt := reflect.TypeOf(t).Elem()
o.exclude[rt] = true
}
}
// AlwaysIncludeType always includes explicit type information for each item.
func AlwaysIncludeType() Option { return func(o *Printer) { o.alwaysIncludeType = true } }
// Printer represents structs in a printable manner.
type Printer struct {
indent string
omitEmpty bool
ignoreGoStringer bool
ignorePrivate bool
alwaysIncludeType bool
explicitTypes bool
exclude map[reflect.Type]bool
w io.Writer
useLiterals bool
}
// New creates a new Printer on w with the given Options.
func New(w io.Writer, options ...Option) *Printer {
p := &Printer{
w: w,
indent: " ",
omitEmpty: true,
exclude: map[reflect.Type]bool{},
}
for _, option := range options {
option(p)
}
return p
}
func (p *Printer) nextIndent(indent string) string {
if p.indent != "" {
return indent + p.indent
}
return ""
}
func (p *Printer) thisIndent(indent string) string {
if p.indent != "" {
return indent
}
return ""
}
// Print the values.
func (p *Printer) Print(vs ...any) {
for i, v := range vs {
if i > 0 {
fmt.Fprint(p.w, " ")
}
p.reprValue(map[reflect.Value]bool{}, reflect.ValueOf(v), "", true, false)
}
}
// Println prints each value on a new line.
func (p *Printer) Println(vs ...any) {
for i, v := range vs {
if i > 0 {
fmt.Fprint(p.w, " ")
}
p.reprValue(map[reflect.Value]bool{}, reflect.ValueOf(v), "", true, false)
}
fmt.Fprintln(p.w)
}
// showType is true if struct types should be shown. isAnyValue is true if the containing value is an "any" type.
func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent string, showStructType bool, isAnyValue bool) { // nolint: gocyclo
if seen[v] {
fmt.Fprint(p.w, "...")
return
}
seen[v] = true
defer delete(seen, v)
if v.Kind() == reflect.Invalid || (v.Kind() == reflect.Ptr || v.Kind() == reflect.Map || v.Kind() == reflect.Chan || v.Kind() == reflect.Slice || v.Kind() == reflect.Func || v.Kind() == reflect.Interface) && v.IsNil() {
fmt.Fprint(p.w, "nil")
return
}
t := v.Type()
if t == byteSliceType {
fmt.Fprintf(p.w, "[]byte(%q)", v.Bytes())
return
}
// If we can't access a private field directly with reflection, try and do so via unsafe.
if !v.CanInterface() && v.CanAddr() {
uv := reflect.NewAt(t, unsafe.Pointer(v.UnsafeAddr())).Elem()
if uv.CanInterface() {
v = uv
}
}
// Attempt to use fmt.GoStringer interface.
if !p.ignoreGoStringer && t.Implements(goStringerType) && v.CanInterface() {
fmt.Fprint(p.w, v.Interface().(fmt.GoStringer).GoString())
return
}
in := p.thisIndent(indent)
ni := p.nextIndent(indent)
switch v.Kind() {
case reflect.Slice, reflect.Array:
fmt.Fprintf(p.w, "%s{", substAny(v.Type()))
if v.Len() == 0 {
fmt.Fprint(p.w, "}")
} else {
if p.indent != "" {
fmt.Fprintf(p.w, "\n")
}
for i := 0; i < v.Len(); i++ {
e := v.Index(i)
fmt.Fprintf(p.w, "%s", ni)
p.reprValue(seen, e, ni, p.alwaysIncludeType || p.explicitTypes, v.Type().Elem() == anyType)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.Len()-1 {
fmt.Fprintf(p.w, ", ")
}
}
fmt.Fprintf(p.w, "%s}", in)
}
case reflect.Chan:
fmt.Fprintf(p.w, "make(")
fmt.Fprintf(p.w, "%s", substAny(v.Type()))
fmt.Fprintf(p.w, ", %d)", v.Cap())
case reflect.Map:
fmt.Fprintf(p.w, "%s{", substAny(v.Type()))
if p.indent != "" && v.Len() != 0 {
fmt.Fprintf(p.w, "\n")
}
keys := v.MapKeys()
sort.Slice(keys, func(i, j int) bool {
return fmt.Sprint(keys[i]) < fmt.Sprint(keys[j])
})
for i, k := range keys {
kv := v.MapIndex(k)
fmt.Fprintf(p.w, "%s", ni)
p.reprValue(seen, k, ni, p.alwaysIncludeType || p.explicitTypes, v.Type().Key() == anyType)
fmt.Fprintf(p.w, ": ")
p.reprValue(seen, kv, ni, true, v.Type().Elem() == anyType)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.Len()-1 {
fmt.Fprintf(p.w, ", ")
}
}
fmt.Fprintf(p.w, "%s}", in)
case reflect.Struct:
if td, ok := asTime(v); ok {
timeToGo(p.w, td)
} else {
if showStructType {
fmt.Fprintf(p.w, "%s{", substAny(v.Type()))
} else {
fmt.Fprint(p.w, "{")
}
if p.indent != "" && v.NumField() != 0 {
fmt.Fprintf(p.w, "\n")
}
previous := false
for i := 0; i < v.NumField(); i++ {
t := v.Type().Field(i)
if p.exclude[t.Type] {
continue
}
f := v.Field(i)
ft := f.Type()
// skip private fields
if p.ignorePrivate && !f.CanInterface() {
continue
}
if p.omitEmpty && (f.IsZero() ||
ft.Kind() == reflect.Slice && f.Len() == 0 ||
ft.Kind() == reflect.Map && f.Len() == 0) {
continue
}
if previous && p.indent == "" {
fmt.Fprintf(p.w, ", ")
}
previous = true
fmt.Fprintf(p.w, "%s%s: ", ni, t.Name)
p.reprValue(seen, f, ni, true, t.Type == anyType)
// if private fields should be ignored, look up if a public
// field need to be displayed and breaks at the first public
// field found preventing from looping over all remaining
// fields.
//
// If no other field need to be displayed, continue and do
// not print a comma.
//
// This prevents from having a trailing comma if a private
// field ends a structure.
if p.ignorePrivate {
nc := false
for j := i + 1; j < v.NumField(); j++ {
if v.Field(j).CanInterface() {
nc = true
// exit for j loop
break
}
}
// Skip comma display if no remaining public field found.
if !nc {
continue
}
}
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
}
}
fmt.Fprintf(p.w, "%s}", indent)
}
case reflect.Ptr:
if v.IsNil() {
fmt.Fprintf(p.w, "nil")
return
}
if showStructType {
fmt.Fprintf(p.w, "&")
}
p.reprValue(seen, v.Elem(), indent, showStructType, false)
case reflect.String:
if t.Name() != "string" || p.alwaysIncludeType {
fmt.Fprintf(p.w, "%s(%q)", t, v.String())
} else {
fmt.Fprintf(p.w, "%q", v.String())
}
case reflect.Interface:
if v.IsNil() {
fmt.Fprintf(p.w, "%s(nil)", substAny(v.Type()))
} else {
p.reprValue(seen, v.Elem(), indent, true, true)
}
case reflect.Func:
fmt.Fprint(p.w, substAny(v.Type()))
default:
value := fmt.Sprintf("%v", v)
if p.useLiterals {
value = fmt.Sprintf("%#v", v)
}
if t.Name() != realKindName[t.Kind()] || p.alwaysIncludeType || isAnyValue {
fmt.Fprintf(p.w, "%s(%s)", t, value)
} else {
fmt.Fprintf(p.w, "%s", value)
}
}
}
func asTime(v reflect.Value) (time.Time, bool) {
if !v.CanInterface() {
return time.Time{}, false
}
t, ok := v.Interface().(time.Time)
return t, ok
}
// String returns a string representing v.
func String(v any, options ...Option) string {
w := bytes.NewBuffer(nil)
options = append([]Option{NoIndent()}, options...)
p := New(w, options...)
p.Print(v)
return w.String()
}
func extractOptions(vs ...any) (args []any, options []Option) {
for _, v := range vs {
if o, ok := v.(Option); ok {
options = append(options, o)
} else {
args = append(args, v)
}
}
return
}
// Println prints v to os.Stdout, one per line.
func Println(vs ...any) {
args, options := extractOptions(vs...)
New(os.Stdout, options...).Println(args...)
}
// Print writes a representation of v to os.Stdout, separated by spaces.
func Print(vs ...any) {
args, options := extractOptions(vs...)
New(os.Stdout, options...).Print(args...)
}
func timeToGo(w io.Writer, t time.Time) {
if t.IsZero() {
fmt.Fprint(w, "time.Time{}")
return
}
var zone string
switch loc := t.Location(); loc {
case nil:
zone = "nil"
case time.UTC:
zone = "time.UTC"
case time.Local:
zone = "time.Local"
default:
n, off := t.Zone()
zone = fmt.Sprintf("time.FixedZone(%q, %d)", n, off)
}
y, m, d := t.Date()
fmt.Fprintf(w, `time.Date(%d, %d, %d, %d, %d, %d, %d, %s)`, y, m, d, t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), zone)
}
// Replace "interface {}" with "any"
func substAny(t reflect.Type) string {
switch t.Kind() {
case reflect.Array:
return fmt.Sprintf("[%d]%s", t.Len(), substAny(t.Elem()))
case reflect.Slice:
return "[]" + substAny(t.Elem())
case reflect.Map:
return "map[" + substAny(t.Key()) + "]" + substAny(t.Elem())
case reflect.Chan:
return fmt.Sprintf("%s %s", t.ChanDir(), substAny(t.Elem()))
case reflect.Func:
in := []string{}
out := []string{}
for i := 0; i < t.NumIn(); i++ {
in = append(in, substAny(t.In(i)))
}
for i := 0; i < t.NumOut(); i++ {
out = append(out, substAny(t.Out(i)))
}
if len(out) == 0 {
return "func" + t.Name() + "(" + strings.Join(in, ", ") + ")"
}
return "func" + t.Name() + "(" + strings.Join(in, ", ") + ") (" + strings.Join(out, ", ") + ")"
}
if t == anyType {
return "any"
}
return t.String()
}
|