Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
godebug
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
godebug
Commits
135288e3
Commit
135288e3
authored
Mar 30, 2016
by
Kyle Lemons
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for TextMarshalers
parent
21cb3784
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
5 deletions
+28
-5
pretty/public.go
pretty/public.go
+1
-0
pretty/reflect.go
pretty/reflect.go
+10
-4
pretty/reflect_test.go
pretty/reflect_test.go
+17
-1
No files found.
pretty/public.go
View file @
135288e3
...
...
@@ -35,6 +35,7 @@ type Config struct {
// Field and value options
IncludeUnexported
bool
// Include unexported fields in output
PrintStringers
bool
// Call String on a fmt.Stringer
NoTextMarshalers
bool
// Don't automatically call MarshalText
SkipZeroFields
bool
// Skip struct fields that have a zero value.
// Output transforms
...
...
pretty/reflect.go
View file @
135288e3
...
...
@@ -15,6 +15,7 @@
package
pretty
import
(
"encoding"
"fmt"
"reflect"
"sort"
...
...
@@ -31,10 +32,15 @@ func isZeroVal(val reflect.Value) bool {
func
(
c
*
Config
)
val2node
(
val
reflect
.
Value
)
node
{
// TODO(kevlar): pointer tracking?
if
c
.
PrintStringers
&&
val
.
CanInterface
()
{
stringer
,
ok
:=
val
.
Interface
()
.
(
fmt
.
Stringer
)
if
ok
{
return
stringVal
(
stringer
.
String
())
if
val
.
CanInterface
()
{
v
:=
val
.
Interface
()
if
s
,
ok
:=
v
.
(
fmt
.
Stringer
);
ok
&&
c
.
PrintStringers
{
return
stringVal
(
s
.
String
())
}
if
t
,
ok
:=
v
.
(
encoding
.
TextMarshaler
);
ok
&&
!
c
.
NoTextMarshalers
{
if
raw
,
err
:=
t
.
MarshalText
();
err
==
nil
{
// if NOT an error
return
stringVal
(
string
(
raw
))
}
}
}
...
...
pretty/reflect_test.go
View file @
135288e3
...
...
@@ -15,6 +15,7 @@
package
pretty
import
(
"net"
"reflect"
"testing"
"time"
...
...
@@ -78,6 +79,11 @@ func TestVal2nodeDefault(t *testing.T) {
3
,
rawVal
(
"3"
),
},
{
"TextMarshaler"
,
net
.
ParseIP
(
"dead:beef::1"
),
stringVal
(
"dead:beef::1"
),
},
}
for
_
,
test
:=
range
tests
{
...
...
@@ -120,7 +126,17 @@ func TestVal2node(t *testing.T) {
struct
{
Date
time
.
Time
}{
time
.
Unix
(
1234567890
,
0
)
.
UTC
()},
DefaultConfig
,
keyvals
{
{
"Date"
,
keyvals
{}},
// empty struct, it has unexported fields
{
"Date"
,
stringVal
(
"2009-02-13T23:31:30Z"
)},
},
},
{
"time w/o TextMarshalers"
,
struct
{
Date
time
.
Time
}{
time
.
Unix
(
1234567890
,
0
)
.
UTC
()},
&
Config
{
NoTextMarshalers
:
true
,
},
keyvals
{
{
"Date"
,
keyvals
{}},
},
},
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment