Skip to content

Commit 1b892e2

Browse files
authored
Merge pull request #30 from donutloop/feat/debugutil
Debugutil: Added pretty sprint func for any type
2 parents 9e1642a + 3dd07f7 commit 1b892e2

File tree

5 files changed

+156
-1
lines changed

5 files changed

+156
-1
lines changed

debugutil/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Usage
2+
3+
PrettyPrint generates a human readable representation of the value v.
4+
5+
## Example
6+
```go
7+
package main
8+
9+
import (
10+
"github.com/donutloop/toolkit/debugutil"
11+
"log"
12+
)
13+
14+
func main() {
15+
debugutil.PrettyPrint([]string{})
16+
}
17+
```

debugutil/doc_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package debugutil_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/donutloop/toolkit/debugutil"
7+
)
8+
9+
func ExamplePrettySprint() {
10+
11+
str := debugutil.PrettySprint([]string{})
12+
fmt.Println(str)
13+
// Output: []string{
14+
//}
15+
}

debugutil/prettysprint.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2017 The toolkit Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT License
3+
// license that can be found in the LICENSE file.
4+
5+
package debugutil
6+
7+
import (
8+
"fmt"
9+
"reflect"
10+
"strings"
11+
)
12+
13+
const (
14+
bracketOpen string = "{\n"
15+
bracketClose string = "}"
16+
pointerSign string = "&"
17+
nilSign string = "nil"
18+
)
19+
20+
// PrettyPrint generates a human readable representation of the value v.
21+
func PrettySprint(v interface{}) string {
22+
value := reflect.ValueOf(v)
23+
switch value.Kind() {
24+
case reflect.Struct:
25+
str := fullName(value.Type()) + bracketOpen
26+
for i := 0; i < value.NumField(); i++ {
27+
l := string(value.Type().Field(i).Name[0])
28+
if strings.ToUpper(l) == l {
29+
str += fmt.Sprintf("%s: %s,\n", value.Type().Field(i).Name, PrettySprint(value.Field(i).Interface()))
30+
}
31+
}
32+
str += bracketClose
33+
return str
34+
case reflect.Map:
35+
str := "map[" + fullName(value.Type().Key()) + "]" + fullName(value.Type().Elem()) + bracketOpen
36+
for _, k := range value.MapKeys() {
37+
str += fmt.Sprintf(`"%s":%s,\n`, k.String(), PrettySprint(value.MapIndex(k).Interface()))
38+
}
39+
str += bracketClose
40+
return str
41+
case reflect.Ptr:
42+
if e := value.Elem(); e.IsValid() {
43+
return fmt.Sprintf("%s%s", pointerSign, PrettySprint(e.Interface()))
44+
}
45+
return nilSign
46+
case reflect.Slice:
47+
str := "[]" + fullName(value.Type().Elem()) + bracketOpen
48+
for i := 0; i < value.Len(); i++ {
49+
str += fmt.Sprintf("%s,\n", PrettySprint(value.Index(i).Interface()))
50+
}
51+
str += bracketClose
52+
return str
53+
default:
54+
return fmt.Sprintf("%#v", v)
55+
}
56+
}
57+
58+
func pkgName(t reflect.Type) string {
59+
pkg := t.PkgPath()
60+
c := strings.Split(pkg, "/")
61+
return c[len(c)-1]
62+
}
63+
64+
func fullName(t reflect.Type) string {
65+
if pkg := pkgName(t); pkg != "" {
66+
return pkg + "." + t.Name()
67+
}
68+
return t.Name()
69+
}

debugutil/prettysprint_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2017 The toolkit Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT License
3+
// license that can be found in the LICENSE file.
4+
5+
package debugutil_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/donutloop/toolkit/debugutil"
11+
)
12+
13+
func Test(t *testing.T) {
14+
15+
strings := "dummy"
16+
17+
tests := []struct {
18+
name string
19+
input interface{}
20+
output string
21+
}{
22+
{
23+
name: "pretty print slice",
24+
input: make([]string, 0),
25+
output: `[]string{
26+
}`,
27+
},
28+
{
29+
name: "pretty print map",
30+
input: make(map[string]string),
31+
output: `map[string]string{
32+
}`,
33+
},
34+
{
35+
name: "pretty print pointer",
36+
input: &strings,
37+
output: `&"dummy"`,
38+
},
39+
{
40+
name: "pretty print value",
41+
input: 3,
42+
output: "3",
43+
},
44+
}
45+
for _, test := range tests {
46+
t.Run(test.name, func(t *testing.T) {
47+
output := debugutil.PrettySprint(test.input)
48+
if output != test.output {
49+
t.Errorf(`unepxected value (actual: "%s", exepected: "%s")`, output, test.output)
50+
}
51+
})
52+
}
53+
}

schedule/schedule_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ package schedule_test
66

77
import (
88
"context"
9-
"github.com/donutloop/toolkit/schedule"
109
"testing"
1110
"time"
11+
12+
"github.com/donutloop/toolkit/schedule"
1213
)
1314

1415
func TestFIFOSchedule(t *testing.T) {

0 commit comments

Comments
 (0)