Skip to content

Commit

Permalink
fix: check empty key (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and fsamin committed Jan 12, 2018
1 parent b3a2116 commit c11d795
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ func (e *Encoder) Sdump(i interface{}) (string, error) {
func (e *Encoder) fdumpInterface(w map[string]interface{}, i interface{}, roots []string) error {
f := valueFromInterface(i)
if !validAndNotEmpty(f) {
if len(roots) == 0 {
return nil
}
k := fmt.Sprintf("%s", strings.Join(sliceFormat(roots, e.Formatters), "."))
w[k] = ""
return nil
Expand Down
18 changes: 18 additions & 0 deletions test/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,24 @@ value: bar
assert.Equal(t, expected, out.String())
}

func TestMapEmptyInterface(t *testing.T) {
myMap := make(map[string]interface{})
myMap[""] = "empty"

result, err := dump.ToStringMap(myMap)
t.Log(dump.Sdump(myMap))
assert.NoError(t, err)
assert.Equal(t, 2, len(result))

expected := `__len__: 0
__type__: Map
`
out := &bytes.Buffer{}
err = dump.Fdump(out, myMap, dump.WithDefaultLowerCaseFormatter())
assert.NoError(t, err)
assert.Equal(t, expected, out.String())
}

func TestFromJSON(t *testing.T) {
js := []byte(`{
"blabla": "lol log",
Expand Down

0 comments on commit c11d795

Please sign in to comment.