-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
85 lines (73 loc) · 1.93 KB
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package subtest
import (
"encoding/json"
"fmt"
"reflect"
"strings"
)
var fmtCfg = struct {
f func(v ...interface{}) string
indent string
}{indent: " "}
// SetTypeFormatter replaces the type formatter used by the package. This
// function is not thread-safe, and should be called as part of initialization
// only. E.g. in a test package init function.
func SetTypeFormatter(f func(...interface{}) string) {
fmtCfg.f = f
}
// SetIndent sets a string to use in package error and type formatting. The
// default is four spaces, as that's what's used by the Go test-runner. This
// function is not thread-safe, and should be called as part of initialization
// only. E.g. in a test package init function.
func SetIndent(s string) {
fmtCfg.indent = s
}
func formatIndentedType(v interface{}) string {
switch vt := v.(type) {
case nil:
return "untyped nil"
case error:
return fmt.Sprintf("%T\n%s", v, indentString(vt.Error()))
default:
return fmt.Sprintf("%T\n%s", v, indentString(FormatType(v)))
}
}
// FormatType formats a type using the configured type formatter for the
// package.
func FormatType(v interface{}) string {
f := fmtCfg.f
if f == nil {
return defaultTypeFormatter(v)
}
return f(v)
}
func defaultTypeFormatter(v interface{}) string {
switch vt := v.(type) {
case nil:
return "nil"
case error:
return vt.Error()
case []byte, json.RawMessage:
return fmt.Sprintf("`%s`", v)
case string, fmt.Stringer:
return quoteString(fmt.Sprintf("%s", v))
}
rv := reflect.ValueOf(v)
switch {
case rv.Kind() == reflect.Ptr && rv.IsNil():
return "nil"
case rv.Kind() == reflect.Ptr:
return fmt.Sprintf("%+v", rv.Elem().Interface())
}
return fmt.Sprintf("%+v", v)
}
func quoteString(s string) string {
if strings.Contains(s, "\n") {
return "`" + s + "`"
}
return fmt.Sprintf("%q", s)
}
func indentString(s string) string {
sIndent := fmtCfg.indent
return sIndent + strings.ReplaceAll(s, "\n", "\n"+sIndent)
}