Commit 76173af4 authored by Anthony Fok's avatar Anthony Fok

New upstream version 0.0~git20180724.ead2165

parent 3ba9d6f3
......@@ -14,33 +14,37 @@ import (
"unsafe"
)
// "Real" names of basic kinds, used to differentiate type aliases.
var 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",
}
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()
var goStringerType = reflect.TypeOf((*fmt.GoStringer)(nil)).Elem()
byteSliceType = reflect.TypeOf([]byte{})
)
// Default prints to os.Stdout with two space indentation.
var Default = New(os.Stdout, Indent(" "))
......@@ -64,7 +68,6 @@ func Hide(ts ...interface{}) Option {
return func(o *Printer) {
for _, t := range ts {
rt := reflect.Indirect(reflect.ValueOf(t)).Type()
fmt.Println(rt)
o.exclude[rt] = true
}
}
......@@ -113,7 +116,7 @@ func (p *Printer) Print(vs ...interface{}) {
if i > 0 {
fmt.Fprint(p.w, " ")
}
p.reprValue(reflect.ValueOf(v), "")
p.reprValue(map[reflect.Value]bool{}, reflect.ValueOf(v), "")
}
}
......@@ -123,21 +126,34 @@ func (p *Printer) Println(vs ...interface{}) {
if i > 0 {
fmt.Fprint(p.w, " ")
}
p.reprValue(reflect.ValueOf(v), "")
p.reprValue(map[reflect.Value]bool{}, reflect.ValueOf(v), "")
}
fmt.Fprintln(p.w)
}
func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent string) { // nolint: gocyclo
if seen[v] {
fmt.Fprint(p.w, "...")
return
}
seen[v] = true
defer delete(seen, v)
if (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
}
if p.exclude[v.Type()] {
fmt.Fprint(p.w, v.Type().Name())
fmt.Fprintf(p.w, "%s...", v.Type().Name())
return
}
t := v.Type()
if t == byteSliceType {
fmt.Fprintf(p.w, "[]byte(%q)", v.Interface())
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()
......@@ -146,7 +162,7 @@ func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
}
}
// Attempt to use fmt.GoStringer interface.
if t.Implements(goStringerType) {
if !p.ignoreGoStringer && t.Implements(goStringerType) {
fmt.Fprint(p.w, v.Interface().(fmt.GoStringer).GoString())
return
}
......@@ -167,7 +183,7 @@ func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
for i := 0; i < v.Len(); i++ {
e := v.Index(i)
fmt.Fprintf(p.w, "%s", ni)
p.reprValue(e, ni)
p.reprValue(seen, e, ni)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.Len()-1 {
......@@ -190,9 +206,9 @@ func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
for i, k := range v.MapKeys() {
kv := v.MapIndex(k)
fmt.Fprintf(p.w, "%s", ni)
p.reprValue(k, ni)
p.reprValue(seen, k, ni)
fmt.Fprintf(p.w, ": ")
p.reprValue(kv, ni)
p.reprValue(seen, kv, ni)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.Len()-1 {
......@@ -213,7 +229,7 @@ func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
continue
}
fmt.Fprintf(p.w, "%s%s: ", ni, t.Name)
p.reprValue(f, ni)
p.reprValue(seen, f, ni)
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
} else if i < v.NumField()-1 {
......@@ -228,7 +244,7 @@ func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
return
}
fmt.Fprintf(p.w, "&")
p.reprValue(v.Elem(), indent)
p.reprValue(seen, v.Elem(), indent)
case reflect.String:
if t.Name() != "string" {
......@@ -241,7 +257,7 @@ func (p *Printer) reprValue(v reflect.Value, indent string) { // nolint: gocyclo
if v.IsNil() {
fmt.Fprintf(p.w, "interface {}(nil)")
} else {
p.reprValue(v.Elem(), indent)
p.reprValue(seen, v.Elem(), indent)
}
default:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment