Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/34: More compact array/map representation #43

Closed
wants to merge 11 commits into from
22 changes: 22 additions & 0 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ func (s *dumpState) dumpType(v reflect.Value) {
}

func (s *dumpState) dumpSlice(v reflect.Value) {
elmType := v.Type().Elem()
switch elmType.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.String:
// Where the elements in a slice/array are primitive types; dump in a compact manner
original := s.config.Compact
s.config.Compact = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should never mutate the configuration, which might be shared across multiple concurrent goroutines, and might be a constant at the caller's end. Let's add the flag to dumpState directly.

defer func() {
s.config.Compact = original
}()
}

s.dumpType(v)
numEntries := v.Len()
if numEntries == 0 {
Expand Down Expand Up @@ -178,6 +189,17 @@ func (s *dumpState) dumpStruct(v reflect.Value) {
}

func (s *dumpState) dumpMap(v reflect.Value) {
elmType := v.Type().Elem()
switch elmType.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.String:
// Where the elements(not keys) in a map are primitive types; dump in a compact manner
original := s.config.Compact
s.config.Compact = true
defer func() {
s.config.Compact = original
}()
}

if v.IsNil() {
s.dumpType(v)
s.writeString("(nil)")
Expand Down
24 changes: 24 additions & 0 deletions dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,30 @@ func TestSdump_maps(t *testing.T) {
map[int]*BlankStruct{
2: &BlankStruct{},
},
map[string]BasicStruct{
"one": BasicStruct{Public: 1, private: 1},
"two": BasicStruct{Public: 2, private: 2},
"three": BasicStruct{Public: 3, private: 3},
"four": BasicStruct{Public: 4, private: 4},
"five": BasicStruct{Public: 5, private: 5},
},
})
}

func TestSdump_slices(t *testing.T) {
runTests(t, "slices_arrays", []interface{}{
[]string{"a", "b", "c", "d", "e", "f"},
[15]int{1, 2, 3, 4, 5, 6, 7, 8, 78_999, 4, 90_000},
[]BasicStruct{
BasicStruct{Public: 1, private: 1},
BasicStruct{Public: 2, private: 2},
BasicStruct{Public: 3, private: 3},
},
[5]BasicStruct{
BasicStruct{Public: 1, private: 1},
BasicStruct{Public: 2, private: 2},
BasicStruct{Public: 3, private: 3},
},
})
}

Expand Down
34 changes: 24 additions & 10 deletions testdata/maps.dump
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
[]interface {}{
map[string]string{
"another string": "indeed",
"hello": "there",
"something": "something something",
},
map[int]string{
1: "one",
2: "two",
3: "three",
},
map[string]string{"another string":"indeed","hello":"there","something":"something something"},
map[int]string{1:"one",2:"two",3:"three"},
map[int]*litter_test.BlankStruct{
2: &litter_test.BlankStruct{},
},
map[string]litter_test.BasicStruct{
"five": litter_test.BasicStruct{
Public: 5,
private: 5,
},
"four": litter_test.BasicStruct{
Public: 4,
private: 4,
},
"one": litter_test.BasicStruct{
Public: 1,
private: 1,
},
"three": litter_test.BasicStruct{
Public: 3,
private: 3,
},
"two": litter_test.BasicStruct{
Public: 2,
private: 2,
},
},
}
5 changes: 1 addition & 4 deletions testdata/multipleArgs_lineBreak.dump
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
[]string{
"x",
"y",
}
[]string{"x","y"}
42
5 changes: 1 addition & 4 deletions testdata/multipleArgs_noSeparator.dump
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
[]string{
"x",
"y",
}42
[]string{"x","y"}42
5 changes: 1 addition & 4 deletions testdata/multipleArgs_separator.dump
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
[]string{
"x",
"y",
}***42
[]string{"x","y"}***42
6 changes: 1 addition & 5 deletions testdata/primitives.dump
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
complex128(-1.2-0.1i),
&10,
"string with \"quote\"",
[]int{
1,
2,
3,
},
[]int{1,2,3},
"hello from interface",
litter_test.BlankStruct{},
&litter_test.BlankStruct{},
Expand Down
40 changes: 40 additions & 0 deletions testdata/slices_arrays.dump
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[]interface {}{
[]string{"a","b","c","d","e","f"},
[15]int{1,2,3,4,5,6,7,8,78999,4,90000,0,0,0,0},
[]litter_test.BasicStruct{
litter_test.BasicStruct{
Public: 1,
private: 1,
},
litter_test.BasicStruct{
Public: 2,
private: 2,
},
litter_test.BasicStruct{
Public: 3,
private: 3,
},
},
[5]litter_test.BasicStruct{
litter_test.BasicStruct{
Public: 1,
private: 1,
},
litter_test.BasicStruct{
Public: 2,
private: 2,
},
litter_test.BasicStruct{
Public: 3,
private: 3,
},
litter_test.BasicStruct{
Public: 0,
private: 0,
},
litter_test.BasicStruct{
Public: 0,
private: 0,
},
},
}