Skip to content

Commit

Permalink
fix: new array syntax (#20)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored May 9, 2022
1 parent a6677aa commit 21625fb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
32 changes: 32 additions & 0 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,38 @@ __Type__: TS
assert.Equal(t, expected, out.String())
}

func TestDumpStruct_Array_New_Array_Notation(t *testing.T) {
a := TS{
A: 0,
B: "here",
C: []T{
{23, "foo bar", Tbis{"lol", "lol"}},
{24, "fee bor", Tbis{"lel", "lel"}},
},
D: []bool{true, false},
}

out := &bytes.Buffer{}
dumper := dump.NewEncoder(out)
dumper.ArrayJSONNotation = true
err := dumper.Fdump(a)
assert.NoError(t, err)
expected := `TS.A: 0
TS.B: here
TS.C[0].A: 23
TS.C[0].B: foo bar
TS.C[0].C.Cbis: lol
TS.C[0].C.Cter: lol
TS.C[1].A: 24
TS.C[1].B: fee bor
TS.C[1].C.Cbis: lel
TS.C[1].C.Cter: lel
TS.D[0]: true
TS.D[1]: false
`
assert.Equal(t, expected, out.String())
}

func TestToMap(t *testing.T) {
type T struct {
A int
Expand Down
9 changes: 6 additions & 3 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,17 @@ func (e *Encoder) fDumpArray(w map[string]interface{}, i interface{}, roots []st
for i := 0; i < v.Len(); i++ {
var l string
var croots []string
var skey = fmt.Sprintf("[%d]", i)
if len(roots) > 0 {
l = roots[len(roots)-1:][0]
if !e.ArrayJSONNotation {
skey = fmt.Sprintf("%s%d", l, i)
croots = append(roots, fmt.Sprintf("%s%d", l, i))
} else {
var t = make([]string, len(roots)-1)
copy(t, roots[0:len(roots)-1])
croots = append(t, fmt.Sprintf("%s[%d]", l, i))
}
croots = append(roots, skey)
} else {
var skey = fmt.Sprintf("[%d]", i)
if !e.ArrayJSONNotation {
skey = fmt.Sprintf("%s%d", l, i)
}
Expand Down

0 comments on commit 21625fb

Please sign in to comment.