Skip to content

Commit

Permalink
WIP [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
danilvpetrov committed Dec 29, 2019
1 parent ddeefd5 commit ba37971
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
5 changes: 0 additions & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
//
// TODO(jmalloc): sort numerically-keyed maps numerically
func (vis *visitor) visitMap(w io.Writer, v Value) {
if vis.enter(w, v) {
return
}
defer vis.leave(v)

if v.IsAmbiguousType() {
must.WriteString(w, v.TypeName())
}
Expand Down
5 changes: 0 additions & 5 deletions ptr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (

// visitPtr formats values with a kind of reflect.Ptr.
func (vis *visitor) visitPtr(w io.Writer, v Value) {
if vis.enter(w, v) {
return
}
defer vis.leave(v)

if v.IsAmbiguousType() {
must.WriteByte(w, '*')
}
Expand Down
5 changes: 0 additions & 5 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@ import (

// visitSlice formats values with a kind of reflect.Slice.
func (vis *visitor) visitSlice(w io.Writer, v Value) {
if vis.enter(w, v) {
return
}
defer vis.leave(v)

vis.visitArray(w, v)
}
13 changes: 13 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ func (v *Value) IsAnonymousType() bool {
func (v *Value) IsAmbiguousType() bool {
return v.IsAmbiguousDynamicType || v.IsAmbiguousStaticType
}

// CanNil reports if the Value can be nil as a zero value. When this method
// returns true, it is safe to call IsNil() on the value without causing
// panicking.
func (v *Value) CanNil() bool {
switch v.DynamicType.Kind() {
case reflect.Chan, reflect.Func, reflect.Interface,
reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
return true
default:
return false
}
}
17 changes: 16 additions & 1 deletion visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (vis *visitor) visit(w io.Writer, v Value) {
return
}

if vis.enter(w, v) {
return
}
defer vis.leave(v)

for _, f := range vis.filters {
if n := must.Must(f(w, v)); n > 0 {
return
Expand Down Expand Up @@ -79,6 +84,16 @@ func (vis *visitor) visit(w io.Writer, v Value) {
// It returns true if the value is nil, or recursion has occurred, indicating
// that the value should not be rendered.
func (vis *visitor) enter(w io.Writer, v Value) bool {
if !v.CanNil() {
return false
}

// If the value is an empty interface, return false as there is a specific
// case for rendering empty interfaces.
if v.DynamicType.Kind() == reflect.Interface && v.Value.IsNil() {
return false
}

marker := "nil"

if !v.Value.IsNil() {
Expand Down Expand Up @@ -113,7 +128,7 @@ func (vis *visitor) enter(w io.Writer, v Value) bool {
//
// It must be called after enter(v) returns true.
func (vis *visitor) leave(v Value) {
if !v.Value.IsNil() {
if v.CanNil() && !v.Value.IsNil() {
delete(vis.recursionSet, v.Value.Pointer())
}
}

0 comments on commit ba37971

Please sign in to comment.