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
73b4550a
Commit
73b4550a
authored
May 17, 2015
by
Kyle Lemons
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from rmartinjak/skipzerovals
Allow the shortening of diffs by skipping fields with zero values
parents
808ac284
c13a706a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
85 additions
and
7 deletions
+85
-7
pretty/public.go
pretty/public.go
+16
-6
pretty/public_test.go
pretty/public_test.go
+56
-0
pretty/reflect.go
pretty/reflect.go
+13
-1
No files found.
pretty/public.go
View file @
73b4550a
...
...
@@ -35,13 +35,21 @@ type Config struct {
// Field and value options
IncludeUnexported
bool
// Include unexported fields in output
PrintStringers
bool
// Call String on a fmt.Stringer
SkipZeroFields
bool
// Skip struct fields that have a zero value.
// Output transforms
ShortList
int
// Maximum character length for short lists if nonzero.
}
// DefaultConfig is the default configuration used for all exported non-method functions except Compare.
var
DefaultConfig
=
&
Config
{}
// ConmpareConfig is the default configuration used for Compare.
var
CompareConfig
=
&
Config
{
Diffable
:
true
,
IncludeUnexported
:
true
,
}
func
(
cfg
*
Config
)
fprint
(
buf
*
bytes
.
Buffer
,
vals
...
interface
{})
{
for
i
,
val
:=
range
vals
{
if
i
>
0
{
...
...
@@ -86,16 +94,18 @@ func (cfg *Config) Fprint(w io.Writer, vals ...interface{}) (n int64, err error)
}
// Compare returns a string containing a line-by-line unified diff of the
// values in got and want
. Compare includes unexported fields
.
// values in got and want
, using the default config
.
//
// Each line in the output is prefixed with '+', '-', or ' ' to indicate if it
// should be added to, removed from, or is correct for the "got" value with
// respect to the "want" value.
func
Compare
(
got
,
want
interface
{})
string
{
diffOpt
:=
&
Config
{
Diffable
:
true
,
IncludeUnexported
:
true
,
}
return
CompareConfig
.
Compare
(
got
,
want
)
}
return
diff
.
Diff
(
diffOpt
.
Sprint
(
got
),
diffOpt
.
Sprint
(
want
))
// Compare returns a string containing a line-by-line unified diff of the
// values in got and want according to the cfg.
func
(
cfg
*
Config
)
Compare
(
got
,
want
interface
{})
string
{
cfg
.
Diffable
=
true
return
diff
.
Diff
(
cfg
.
Sprint
(
got
),
cfg
.
Sprint
(
want
))
}
pretty/public_test.go
View file @
73b4550a
...
...
@@ -70,3 +70,59 @@ func TestDiff(t *testing.T) {
}
}
}
func
TestSkipZeroFields
(
t
*
testing
.
T
)
{
type
example
struct
{
Name
string
Species
string
Age
int
Friends
[]
string
}
tests
:=
[]
struct
{
desc
string
got
,
want
interface
{}
diff
string
}{
{
desc
:
"basic struct"
,
got
:
example
{
Name
:
"Zaphd"
,
Species
:
"Betelgeusian"
,
Age
:
42
,
},
want
:
example
{
Name
:
"Zaphod"
,
Species
:
"Betelgeusian"
,
Age
:
42
,
Friends
:
[]
string
{
"Ford Prefect"
,
"Trillian"
,
""
,
},
},
diff
:
` {
- Name: "Zaphd",
+ Name: "Zaphod",
Species: "Betelgeusian",
Age: 42,
+ Friends: [
+ "Ford Prefect",
+ "Trillian",
+ "",
+ ],
}`
,
},
}
cfg
:=
*
CompareConfig
cfg
.
SkipZeroFields
=
true
for
_
,
test
:=
range
tests
{
if
got
,
want
:=
cfg
.
Compare
(
test
.
got
,
test
.
want
),
test
.
diff
;
got
!=
want
{
t
.
Errorf
(
"%s:"
,
test
.
desc
)
t
.
Errorf
(
" got: %q"
,
got
)
t
.
Errorf
(
" want: %q"
,
want
)
}
}
}
pretty/reflect.go
View file @
73b4550a
...
...
@@ -20,6 +20,14 @@ import (
"sort"
)
func
isZeroVal
(
val
reflect
.
Value
)
bool
{
if
!
val
.
CanInterface
()
{
return
false
}
z
:=
reflect
.
Zero
(
val
.
Type
())
.
Interface
()
return
reflect
.
DeepEqual
(
val
.
Interface
(),
z
)
}
func
(
c
*
Config
)
val2node
(
val
reflect
.
Value
)
node
{
// TODO(kevlar): pointer tracking?
...
...
@@ -63,7 +71,11 @@ func (c *Config) val2node(val reflect.Value) node {
if
!
c
.
IncludeUnexported
&&
sf
.
PkgPath
!=
""
{
continue
}
n
=
append
(
n
,
keyval
{
sf
.
Name
,
c
.
val2node
(
val
.
Field
(
i
))})
field
:=
val
.
Field
(
i
)
if
c
.
SkipZeroFields
&&
isZeroVal
(
field
)
{
continue
}
n
=
append
(
n
,
keyval
{
sf
.
Name
,
c
.
val2node
(
field
)})
}
return
n
case
reflect
.
Bool
:
...
...
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