FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
go-stack/data/stringify.go at master · subchen/go-stack · GitHub
subchen
/
go-stack
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
6
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
go-stack
/
data
/
stringify.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
77 lines (65 loc) · 1.43 KB
Breadcrumbs
go-stack
/
data
/
stringify.go
Copy path
File metadata and controls
77 lines (65 loc) · 1.43 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
data
import
(
"bytes"
"fmt"
"io"
"reflect"
)
// Stringify attempts to create a reasonable string representation of types.
// It does things like resolve pointers to their values and omits struct fields with nil values.
func
Stringify
(
message
interface
{})
string
{
var
buf
bytes.
Buffer
v
:=
reflect
.
ValueOf
(
message
)
stringifyValue
(
&
buf
,
v
)
return
buf
.
String
()
}
// stringifyValue was heavily inspired by the goprotobuf library.
func
stringifyValue
(
w
io.
Writer
,
val
reflect.
Value
) {
if
val
.
Kind
()
==
reflect
.
Ptr
&&
val
.
IsNil
() {
w
.
Write
([]
byte
(
"<nil>"
))
return
}
v
:=
reflect
.
Indirect
(
val
)
switch
v
.
Kind
() {
case
reflect
.
String
:
fmt
.
Fprintf
(
w
,
`"%s"`
,
v
)
case
reflect
.
Slice
:
w
.
Write
([]
byte
{
'['
})
for
i
:=
0
;
i
<
v
.
Len
();
i
++
{
if
i
>
0
{
w
.
Write
([]
byte
{
' '
})
}
stringifyValue
(
w
,
v
.
Index
(
i
))
}
w
.
Write
([]
byte
{
']'
})
return
case
reflect
.
Struct
:
if
v
.
Type
().
Name
()
!=
""
{
w
.
Write
([]
byte
(
v
.
Type
().
String
()))
}
w
.
Write
([]
byte
{
'{'
})
var
sep
bool
for
i
:=
0
;
i
<
v
.
NumField
();
i
++
{
fv
:=
v
.
Field
(
i
)
if
fv
.
Kind
()
==
reflect
.
Ptr
&&
fv
.
IsNil
() {
continue
}
if
fv
.
Kind
()
==
reflect
.
Slice
&&
fv
.
IsNil
() {
continue
}
if
sep
{
w
.
Write
([]
byte
(
", "
))
}
else
{
sep
=
true
}
w
.
Write
([]
byte
(
v
.
Type
().
Field
(
i
).
Name
))
w
.
Write
([]
byte
{
':'
})
stringifyValue
(
w
,
fv
)
}
w
.
Write
([]
byte
{
'}'
})
default
:
if
v
.
CanInterface
() {
fmt
.
Fprint
(
w
,
v
.
Interface
())
}
}
}
Back
|
FazBrowse Home
|
New Git URL