-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from donutloop/feat/debugutil
Debugutil: Added pretty sprint func for any type
- Loading branch information
Showing
5 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{ | ||
//} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters