From 3dd07f7f7f47d035a4b401bd53f062e9477d2b76 Mon Sep 17 00:00:00 2001 From: Marcel Edmund Franke Date: Wed, 20 Dec 2017 14:51:15 +0100 Subject: [PATCH] Debugutil: Added pretty sprint func for any type --- debugutil/README.md | 17 +++++++++ debugutil/doc_test.go | 15 ++++++++ debugutil/prettysprint.go | 69 ++++++++++++++++++++++++++++++++++ debugutil/prettysprint_test.go | 53 ++++++++++++++++++++++++++ schedule/schedule_test.go | 3 +- 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 debugutil/README.md create mode 100644 debugutil/doc_test.go create mode 100644 debugutil/prettysprint.go create mode 100644 debugutil/prettysprint_test.go diff --git a/debugutil/README.md b/debugutil/README.md new file mode 100644 index 0000000..5612afc --- /dev/null +++ b/debugutil/README.md @@ -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{}) +} +``` \ No newline at end of file diff --git a/debugutil/doc_test.go b/debugutil/doc_test.go new file mode 100644 index 0000000..2253af5 --- /dev/null +++ b/debugutil/doc_test.go @@ -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{ + //} +} diff --git a/debugutil/prettysprint.go b/debugutil/prettysprint.go new file mode 100644 index 0000000..98ae203 --- /dev/null +++ b/debugutil/prettysprint.go @@ -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() +} diff --git a/debugutil/prettysprint_test.go b/debugutil/prettysprint_test.go new file mode 100644 index 0000000..e270c1d --- /dev/null +++ b/debugutil/prettysprint_test.go @@ -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) + } + }) + } +} diff --git a/schedule/schedule_test.go b/schedule/schedule_test.go index ea1d4e4..e155083 100644 --- a/schedule/schedule_test.go +++ b/schedule/schedule_test.go @@ -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) {