Skip to content

Commit

Permalink
Merge pull request #30 from donutloop/feat/debugutil
Browse files Browse the repository at this point in the history
Debugutil: Added pretty sprint func for any type
  • Loading branch information
donutloop committed Dec 20, 2017
2 parents 9e1642a + 3dd07f7 commit 1b892e2
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 1 deletion.
17 changes: 17 additions & 0 deletions debugutil/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Usage

PrettyPrint generates a human readable representation of the value v.

## Example
```go
package main

import (
"github.com/donutloop/toolkit/debugutil"
"log"
)

func main() {
debugutil.PrettyPrint([]string{})
}
```
15 changes: 15 additions & 0 deletions debugutil/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package debugutil_test

import (
"fmt"

"github.com/donutloop/toolkit/debugutil"
)

func ExamplePrettySprint() {

str := debugutil.PrettySprint([]string{})
fmt.Println(str)
// Output: []string{
//}
}
69 changes: 69 additions & 0 deletions debugutil/prettysprint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2017 The toolkit Authors. All rights reserved.
// Use of this source code is governed by a MIT License
// license that can be found in the LICENSE file.

package debugutil

import (
"fmt"
"reflect"
"strings"
)

const (
bracketOpen string = "{\n"
bracketClose string = "}"
pointerSign string = "&"
nilSign string = "nil"
)

// PrettyPrint generates a human readable representation of the value v.
func PrettySprint(v interface{}) string {
value := reflect.ValueOf(v)
switch value.Kind() {
case reflect.Struct:
str := 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()))
}
}
str += bracketClose
return str
case reflect.Map:
str := "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()))
}
str += bracketClose
return str
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
for i := 0; i < value.Len(); i++ {
str += fmt.Sprintf("%s,\n", PrettySprint(value.Index(i).Interface()))
}
str += bracketClose
return str
default:
return fmt.Sprintf("%#v", v)
}
}

func pkgName(t reflect.Type) string {
pkg := t.PkgPath()
c := strings.Split(pkg, "/")
return c[len(c)-1]
}

func fullName(t reflect.Type) string {
if pkg := pkgName(t); pkg != "" {
return pkg + "." + t.Name()
}
return t.Name()
}
53 changes: 53 additions & 0 deletions debugutil/prettysprint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2017 The toolkit Authors. All rights reserved.
// Use of this source code is governed by a MIT License
// license that can be found in the LICENSE file.

package debugutil_test

import (
"testing"

"github.com/donutloop/toolkit/debugutil"
)

func Test(t *testing.T) {

strings := "dummy"

tests := []struct {
name string
input interface{}
output string
}{
{
name: "pretty print slice",
input: make([]string, 0),
output: `[]string{
}`,
},
{
name: "pretty print map",
input: make(map[string]string),
output: `map[string]string{
}`,
},
{
name: "pretty print pointer",
input: &strings,
output: `&"dummy"`,
},
{
name: "pretty print value",
input: 3,
output: "3",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
output := debugutil.PrettySprint(test.input)
if output != test.output {
t.Errorf(`unepxected value (actual: "%s", exepected: "%s")`, output, test.output)
}
})
}
}
3 changes: 2 additions & 1 deletion schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package schedule_test

import (
"context"
"github.com/donutloop/toolkit/schedule"
"testing"
"time"

"github.com/donutloop/toolkit/schedule"
)

func TestFIFOSchedule(t *testing.T) {
Expand Down

0 comments on commit 1b892e2

Please sign in to comment.