Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugutil: pretty print added support nil cases #32

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions debugutil/prettysprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package debugutil

import (
"bytes"
"fmt"
"reflect"
"strings"
Expand All @@ -14,42 +15,53 @@ const (
bracketOpen string = "{\n"
bracketClose string = "}"
pointerSign string = "&"
nilSign string = "nil"
nilSign string = "<nil>"
)

// PrettyPrint generates a human readable representation of the value v.
// PrettySprint generates a human readable representation of the value v.
func PrettySprint(v interface{}) string {

value := reflect.ValueOf(v)

// nil
switch value.Kind() {
case reflect.Interface, reflect.Map, reflect.Slice, reflect.Ptr:
if value.IsNil() {
return nilSign
}
}

buff := bytes.Buffer{}
switch value.Kind() {
case reflect.Struct:
str := fullName(value.Type()) + bracketOpen
buff.WriteString(fullName(value.Type()) + bracketOpen)
for i := 0; i < value.NumField(); i++ {
l := string(value.Type().Field(i).Name[0])
if strings.ToUpper(l) == l {
str += fmt.Sprintf("%s: %s,\n", value.Type().Field(i).Name, PrettySprint(value.Field(i).Interface()))
buff.WriteString(fmt.Sprintf("%s: %s,\n", value.Type().Field(i).Name, PrettySprint(value.Field(i).Interface())))
}
}
str += bracketClose
return str
buff.WriteString(bracketClose)
return buff.String()
case reflect.Map:
str := "map[" + fullName(value.Type().Key()) + "]" + fullName(value.Type().Elem()) + bracketOpen
buff.WriteString("map[" + fullName(value.Type().Key()) + "]" + fullName(value.Type().Elem()) + bracketOpen)
for _, k := range value.MapKeys() {
str += fmt.Sprintf(`"%s":%s,\n`, k.String(), PrettySprint(value.MapIndex(k).Interface()))
buff.WriteString(fmt.Sprintf(`"%s":%s,\n`, k.String(), PrettySprint(value.MapIndex(k).Interface())))
}
str += bracketClose
return str
buff.WriteString(bracketClose)
return buff.String()
case reflect.Ptr:
if e := value.Elem(); e.IsValid() {
return fmt.Sprintf("%s%s", pointerSign, PrettySprint(e.Interface()))
}
return nilSign
case reflect.Slice:
str := "[]" + fullName(value.Type().Elem()) + bracketOpen
buff.WriteString("[]" + fullName(value.Type().Elem()) + bracketOpen)
for i := 0; i < value.Len(); i++ {
str += fmt.Sprintf("%s,\n", PrettySprint(value.Index(i).Interface()))
buff.WriteString(fmt.Sprintf("%s,\n", PrettySprint(value.Index(i).Interface())))
}
str += bracketClose
return str
buff.WriteString(bracketClose)
return buff.String()
default:
return fmt.Sprintf("%#v", v)
}
Expand Down
12 changes: 12 additions & 0 deletions debugutil/prettysprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ func Test(t *testing.T) {

strings := "dummy"

var nilSlice []string = nil

tests := []struct {
name string
input interface{}
output string
}{
{
name: "pretty print nil slice",
input: nilSlice,
output: "<nil>",
},
{
name: "pretty print nil",
input: nil,
output: "<nil>",
},
{
name: "pretty print slice",
input: make([]string, 0),
Expand Down