From 439e138de2eb2fe90450be257f8ac7c98c09f259 Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 15:48:30 +0300 Subject: [PATCH 01/11] when dumping arrays and slices; always dump them in a compact form --- dump.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dump.go b/dump.go index 3dfbb9d..34efb29 100644 --- a/dump.go +++ b/dump.go @@ -106,6 +106,12 @@ func (s *dumpState) dumpType(v reflect.Value) { } func (s *dumpState) dumpSlice(v reflect.Value) { + original := s.config.Compact + s.config.Compact = true + defer func() { + s.config.Compact = original + }() + s.dumpType(v) numEntries := v.Len() if numEntries == 0 { From db5f0247892b0a8ca5ca012f7514f2b0c748292b Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 15:48:50 +0300 Subject: [PATCH 02/11] add a temporary test --- dump_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/dump_test.go b/dump_test.go index c80ced6..047ca03 100644 --- a/dump_test.go +++ b/dump_test.go @@ -4,15 +4,15 @@ import ( "fmt" "io" "io/ioutil" + "net/http" "os" "os/exec" "reflect" "testing" + "github.com/sanity-io/litter" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - - "github.com/sanity-io/litter" ) func Function(arg1 string, arg2 int) (string, error) { @@ -314,3 +314,46 @@ func messageFromMsgAndArgs(msgAndArgs ...interface{}) string { } return "" } + +func Test_dumpState_dumpSlice(t *testing.T) { + + t.Run("hello", func(t *testing.T) { + + mySlice := []int{} + for i := 0; i < 10; i++ { + mySlice = append(mySlice, i) + } + litter.Dump("mySlice: ", mySlice) + + fmt.Println() + + myArray := [15]int{} + for i := 0; i < 10; i++ { + myArray[i] = i + } + litter.Dump("myArray: ", myArray) + + fmt.Println() + + myMap := map[int]string{} + for i := 0; i < 10; i++ { + myMap[i] = fmt.Sprint(i) + } + litter.Dump("myMap: ", myMap) + + fmt.Println() + fmt.Println() + type Table struct { + Nails [15]int + } + type House struct { + Name string + Chairs []int + HTTP http.Request + T Table + } + house := House{Name: "KICC", Chairs: mySlice, HTTP: http.Request{Method: "POST"}, T: Table{Nails: myArray}} + litter.Dump("house: ", house) + + }) +} From 5f73c429cbff685e03537610c9de8ff4f7f4db86 Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 16:56:30 +0300 Subject: [PATCH 03/11] use a map in TestSdump_primitives instead of a []slice This is because slices are now compacted and as such are not easier on the eyes when looking at testdata/primitives.dump --- dump_test.go | 117 +++++++++++++-------------------------- testdata/primitives.dump | 74 ++++++++++++------------- 2 files changed, 72 insertions(+), 119 deletions(-) diff --git a/dump_test.go b/dump_test.go index 047ca03..6ac18c4 100644 --- a/dump_test.go +++ b/dump_test.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "io/ioutil" - "net/http" "os" "os/exec" "reflect" @@ -57,42 +56,43 @@ func TestSdump_primitives(t *testing.T) { sends := make(chan<- int64, 1) receives := make(<-chan uint64) - runTests(t, "primitives", []interface{}{ - false, - true, - 7, - int8(10), - int16(10), - int32(10), - int64(10), - uint8(10), - uint16(10), - uint32(10), - uint64(10), - uint(10), - float32(12.3), - float64(12.3), - complex64(12 + 10.5i), - complex128(-1.2 - 0.1i), - (func(v int) *int { return &v })(10), - "string with \"quote\"", - []int{1, 2, 3}, - interface{}("hello from interface"), - BlankStruct{}, - &BlankStruct{}, - BasicStruct{1, 2}, - IntAlias(10), - (func(v IntAlias) *IntAlias { return &v })(10), - Function, - func(arg string) (bool, error) { return false, nil }, - nil, - interface{}(nil), - CustomMap{}, - CustomMap(nil), - messages, - sends, - receives, - }) + runTests(t, "primitives", + map[string]interface{}{ + "1": false, + "2": true, + "3": 7, + "4": int8(10), + "5": int16(10), + "6": int32(10), + "7": int64(10), + "8": uint8(10), + "9": uint16(10), + "10": uint32(10), + "11": uint64(10), + "12": uint(10), + "13": float32(12.3), + "14": float64(12.3), + "15": complex64(12 + 10.5i), + "16": complex128(-1.2 - 0.1i), + "17": (func(v int) *int { return &v })(10), + "18": "string with \"quote\"", + "19": []int{1, 2, 3}, + "20": interface{}("hello from interface"), + "21": BlankStruct{}, + "22": &BlankStruct{}, + "23": BasicStruct{1, 2}, + "24": IntAlias(10), + "25": (func(v IntAlias) *IntAlias { return &v })(10), + "26": Function, + "27": func(arg string) (bool, error) { return false, nil }, + "28": nil, + "29": interface{}(nil), + "30": CustomMap{}, + "31": CustomMap(nil), + "32": messages, + "33": sends, + "34": receives, + }) } func TestSdump_customDumper(t *testing.T) { @@ -314,46 +314,3 @@ func messageFromMsgAndArgs(msgAndArgs ...interface{}) string { } return "" } - -func Test_dumpState_dumpSlice(t *testing.T) { - - t.Run("hello", func(t *testing.T) { - - mySlice := []int{} - for i := 0; i < 10; i++ { - mySlice = append(mySlice, i) - } - litter.Dump("mySlice: ", mySlice) - - fmt.Println() - - myArray := [15]int{} - for i := 0; i < 10; i++ { - myArray[i] = i - } - litter.Dump("myArray: ", myArray) - - fmt.Println() - - myMap := map[int]string{} - for i := 0; i < 10; i++ { - myMap[i] = fmt.Sprint(i) - } - litter.Dump("myMap: ", myMap) - - fmt.Println() - fmt.Println() - type Table struct { - Nails [15]int - } - type House struct { - Name string - Chairs []int - HTTP http.Request - T Table - } - house := House{Name: "KICC", Chairs: mySlice, HTTP: http.Request{Method: "POST"}, T: Table{Nails: myArray}} - litter.Dump("house: ", house) - - }) -} diff --git a/testdata/primitives.dump b/testdata/primitives.dump index b7195ad..0ca2c7c 100644 --- a/testdata/primitives.dump +++ b/testdata/primitives.dump @@ -1,43 +1,39 @@ -[]interface {}{ - false, - true, - 7, - 10, - 10, - 10, - 10, - 10, - 10, - 10, - 10, - 10, - 12.3, - 12.3, - complex64(12+10.5i), - complex128(-1.2-0.1i), - &10, - "string with \"quote\"", - []int{ - 1, - 2, - 3, - }, - "hello from interface", - litter_test.BlankStruct{}, - &litter_test.BlankStruct{}, - litter_test.BasicStruct{ +map[string]interface {}{ + "1": false, + "10": 10, + "11": 10, + "12": 10, + "13": 12.3, + "14": 12.3, + "15": complex64(12+10.5i), + "16": complex128(-1.2-0.1i), + "17": &10, + "18": "string with \"quote\"", + "19": []int{1,2,3}, + "2": true, + "20": "hello from interface", + "21": litter_test.BlankStruct{}, + "22": &litter_test.BlankStruct{}, + "23": litter_test.BasicStruct{ Public: 1, private: 2, }, - 10, - &10, - litter_test.Function, - func(string) (bool, error), - nil, - nil, - litter_test.CustomMap{}, - litter_test.CustomMap(nil), - chan string, - chan<- int64, - <-chan uint64, + "24": 10, + "25": &10, + "26": litter_test.Function, + "27": func(string) (bool, error), + "28": nil, + "29": nil, + "3": 7, + "30": litter_test.CustomMap{}, + "31": litter_test.CustomMap(nil), + "32": chan string, + "33": chan<- int64, + "34": <-chan uint64, + "4": 10, + "5": 10, + "6": 10, + "7": 10, + "8": 10, + "9": 10, } \ No newline at end of file From e882c5c1900656e39e33dc8ddcf2b7b8878cb2c2 Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:12:36 +0300 Subject: [PATCH 04/11] only compat arrays and slices of primitive types --- dump.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/dump.go b/dump.go index 34efb29..2c61ac2 100644 --- a/dump.go +++ b/dump.go @@ -106,11 +106,16 @@ func (s *dumpState) dumpType(v reflect.Value) { } func (s *dumpState) dumpSlice(v reflect.Value) { - original := s.config.Compact - s.config.Compact = true - defer func() { - s.config.Compact = original - }() + 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: + // For primites types, dump them in a compact manner + original := s.config.Compact + s.config.Compact = true + defer func() { + s.config.Compact = original + }() + } s.dumpType(v) numEntries := v.Len() @@ -184,6 +189,10 @@ func (s *dumpState) dumpStruct(v reflect.Value) { } func (s *dumpState) dumpMap(v reflect.Value) { + // TODO: inspec maps underlying keys and values. + // If values(not keys) are the primitive types; then dump them in compact manner. + // see: `dumpSlice` + if v.IsNil() { s.dumpType(v) s.writeString("(nil)") From bc44f23abb866ac1686959df1615b100715e7261 Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:13:12 +0300 Subject: [PATCH 05/11] undo changes to tests --- dump_test.go | 76 ++++++++++++++++++++-------------------- testdata/primitives.dump | 74 ++++++++++++++++++++------------------ 2 files changed, 77 insertions(+), 73 deletions(-) diff --git a/dump_test.go b/dump_test.go index 6ac18c4..c80ced6 100644 --- a/dump_test.go +++ b/dump_test.go @@ -9,9 +9,10 @@ import ( "reflect" "testing" - "github.com/sanity-io/litter" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/sanity-io/litter" ) func Function(arg1 string, arg2 int) (string, error) { @@ -56,43 +57,42 @@ func TestSdump_primitives(t *testing.T) { sends := make(chan<- int64, 1) receives := make(<-chan uint64) - runTests(t, "primitives", - map[string]interface{}{ - "1": false, - "2": true, - "3": 7, - "4": int8(10), - "5": int16(10), - "6": int32(10), - "7": int64(10), - "8": uint8(10), - "9": uint16(10), - "10": uint32(10), - "11": uint64(10), - "12": uint(10), - "13": float32(12.3), - "14": float64(12.3), - "15": complex64(12 + 10.5i), - "16": complex128(-1.2 - 0.1i), - "17": (func(v int) *int { return &v })(10), - "18": "string with \"quote\"", - "19": []int{1, 2, 3}, - "20": interface{}("hello from interface"), - "21": BlankStruct{}, - "22": &BlankStruct{}, - "23": BasicStruct{1, 2}, - "24": IntAlias(10), - "25": (func(v IntAlias) *IntAlias { return &v })(10), - "26": Function, - "27": func(arg string) (bool, error) { return false, nil }, - "28": nil, - "29": interface{}(nil), - "30": CustomMap{}, - "31": CustomMap(nil), - "32": messages, - "33": sends, - "34": receives, - }) + runTests(t, "primitives", []interface{}{ + false, + true, + 7, + int8(10), + int16(10), + int32(10), + int64(10), + uint8(10), + uint16(10), + uint32(10), + uint64(10), + uint(10), + float32(12.3), + float64(12.3), + complex64(12 + 10.5i), + complex128(-1.2 - 0.1i), + (func(v int) *int { return &v })(10), + "string with \"quote\"", + []int{1, 2, 3}, + interface{}("hello from interface"), + BlankStruct{}, + &BlankStruct{}, + BasicStruct{1, 2}, + IntAlias(10), + (func(v IntAlias) *IntAlias { return &v })(10), + Function, + func(arg string) (bool, error) { return false, nil }, + nil, + interface{}(nil), + CustomMap{}, + CustomMap(nil), + messages, + sends, + receives, + }) } func TestSdump_customDumper(t *testing.T) { diff --git a/testdata/primitives.dump b/testdata/primitives.dump index 0ca2c7c..b7195ad 100644 --- a/testdata/primitives.dump +++ b/testdata/primitives.dump @@ -1,39 +1,43 @@ -map[string]interface {}{ - "1": false, - "10": 10, - "11": 10, - "12": 10, - "13": 12.3, - "14": 12.3, - "15": complex64(12+10.5i), - "16": complex128(-1.2-0.1i), - "17": &10, - "18": "string with \"quote\"", - "19": []int{1,2,3}, - "2": true, - "20": "hello from interface", - "21": litter_test.BlankStruct{}, - "22": &litter_test.BlankStruct{}, - "23": litter_test.BasicStruct{ +[]interface {}{ + false, + true, + 7, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 10, + 12.3, + 12.3, + complex64(12+10.5i), + complex128(-1.2-0.1i), + &10, + "string with \"quote\"", + []int{ + 1, + 2, + 3, + }, + "hello from interface", + litter_test.BlankStruct{}, + &litter_test.BlankStruct{}, + litter_test.BasicStruct{ Public: 1, private: 2, }, - "24": 10, - "25": &10, - "26": litter_test.Function, - "27": func(string) (bool, error), - "28": nil, - "29": nil, - "3": 7, - "30": litter_test.CustomMap{}, - "31": litter_test.CustomMap(nil), - "32": chan string, - "33": chan<- int64, - "34": <-chan uint64, - "4": 10, - "5": 10, - "6": 10, - "7": 10, - "8": 10, - "9": 10, + 10, + &10, + litter_test.Function, + func(string) (bool, error), + nil, + nil, + litter_test.CustomMap{}, + litter_test.CustomMap(nil), + chan string, + chan<- int64, + <-chan uint64, } \ No newline at end of file From 288fce3f69b116efd9df1f9138dfc7ae89e951cf Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:14:48 +0300 Subject: [PATCH 06/11] update TestSdump_primitives --- testdata/primitives.dump | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/testdata/primitives.dump b/testdata/primitives.dump index b7195ad..a4f61c5 100644 --- a/testdata/primitives.dump +++ b/testdata/primitives.dump @@ -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{}, From 53cb262ae7497074010ce36162ac07e034075da0 Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:18:16 +0300 Subject: [PATCH 07/11] fix all tests --- testdata/multipleArgs_lineBreak.dump | 5 +---- testdata/multipleArgs_noSeparator.dump | 5 +---- testdata/multipleArgs_separator.dump | 5 +---- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/testdata/multipleArgs_lineBreak.dump b/testdata/multipleArgs_lineBreak.dump index 8e7e06f..0037182 100644 --- a/testdata/multipleArgs_lineBreak.dump +++ b/testdata/multipleArgs_lineBreak.dump @@ -1,5 +1,2 @@ -[]string{ - "x", - "y", -} +[]string{"x","y"} 42 \ No newline at end of file diff --git a/testdata/multipleArgs_noSeparator.dump b/testdata/multipleArgs_noSeparator.dump index 3d9a1aa..119b23a 100644 --- a/testdata/multipleArgs_noSeparator.dump +++ b/testdata/multipleArgs_noSeparator.dump @@ -1,4 +1 @@ -[]string{ - "x", - "y", -}42 \ No newline at end of file +[]string{"x","y"}42 \ No newline at end of file diff --git a/testdata/multipleArgs_separator.dump b/testdata/multipleArgs_separator.dump index 27e5a3a..f5a40e1 100644 --- a/testdata/multipleArgs_separator.dump +++ b/testdata/multipleArgs_separator.dump @@ -1,4 +1 @@ -[]string{ - "x", - "y", -}***42 \ No newline at end of file +[]string{"x","y"}***42 \ No newline at end of file From b4460aaefe3d04c6d6517d24847f106bb92838b0 Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:31:37 +0300 Subject: [PATCH 08/11] where the elements(not keys) of a map are primitive types; dump in a compact manner --- dump.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dump.go b/dump.go index 2c61ac2..039c682 100644 --- a/dump.go +++ b/dump.go @@ -109,7 +109,7 @@ 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: - // For primites types, dump them in a compact manner + // Where the elements in a slice/array are primitive types; dump in a compact manner original := s.config.Compact s.config.Compact = true defer func() { @@ -189,9 +189,16 @@ func (s *dumpState) dumpStruct(v reflect.Value) { } func (s *dumpState) dumpMap(v reflect.Value) { - // TODO: inspec maps underlying keys and values. - // If values(not keys) are the primitive types; then dump them in compact manner. - // see: `dumpSlice` + 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) From c6e50e5896881cb81db1c4e7b4d6bd3bcc17735a Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:32:51 +0300 Subject: [PATCH 09/11] fix tests --- testdata/maps.dump | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/testdata/maps.dump b/testdata/maps.dump index 61fd468..2428843 100644 --- a/testdata/maps.dump +++ b/testdata/maps.dump @@ -1,14 +1,6 @@ []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{}, }, From 1649d4cae3572e4cd27f53df83258e898c682c5f Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:36:29 +0300 Subject: [PATCH 10/11] add tests showing that maps with nonPrimitive elements are not compacted --- dump_test.go | 7 +++++++ testdata/maps.dump | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/dump_test.go b/dump_test.go index c80ced6..66444cd 100644 --- a/dump_test.go +++ b/dump_test.go @@ -238,6 +238,13 @@ 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}, + }, }) } diff --git a/testdata/maps.dump b/testdata/maps.dump index 2428843..1b18741 100644 --- a/testdata/maps.dump +++ b/testdata/maps.dump @@ -4,4 +4,26 @@ 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, + }, + }, } \ No newline at end of file From 42bf6d5283d22aa874fb4644d51182598ceda78a Mon Sep 17 00:00:00 2001 From: komuW Date: Thu, 20 May 2021 17:42:30 +0300 Subject: [PATCH 11/11] add tests showing that slices with nonPrimitive elements are not compacted --- dump_test.go | 17 ++++++++++++++++ testdata/slices_arrays.dump | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 testdata/slices_arrays.dump diff --git a/dump_test.go b/dump_test.go index 66444cd..dbdb149 100644 --- a/dump_test.go +++ b/dump_test.go @@ -248,6 +248,23 @@ func TestSdump_maps(t *testing.T) { }) } +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}, + }, + }) +} + var standardCfg = litter.Options{} func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...interface{}) { diff --git a/testdata/slices_arrays.dump b/testdata/slices_arrays.dump new file mode 100644 index 0000000..7117156 --- /dev/null +++ b/testdata/slices_arrays.dump @@ -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, + }, + }, +} \ No newline at end of file