Skip to content

Commit

Permalink
issues/57: add config option for how many slices/maps to display (#58)
Browse files Browse the repository at this point in the history
- Fixes: #57
- Fixes: #27
  • Loading branch information
komuw authored Jan 31, 2024
1 parent ba2bf2f commit f3bd2f2
Show file tree
Hide file tree
Showing 26 changed files with 30,290 additions and 312 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
Most recent version is listed first.


# v0.0.14
- Add config option for how many slices/maps to display: https://github.com/komuw/kama/pull/58

# v0.0.13
- Update Go version: https://github.com/komuw/kama/pull/56

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ req.Header.Set("Content-Type", "application/octet-stream")
req.AddCookie(&http.Cookie{Name: "hello", Value: "world"})

kama.Dirp(req)
// kama.Dirp(req, kama.Config{MaxLength: 14}) // pass in an optional config value.
```
that will print:
```bash
Expand Down
14 changes: 8 additions & 6 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,18 @@ func dump(val reflect.Value, hideZeroValues bool, indentLevel int) string {

func dumpString(v reflect.Value) string {
// dumps strings
maxL := 100

adder := 1 // this is a custom string type.
if strings.HasPrefix(fmt.Sprintf("%#v", v), `"`) {
adder = 2 // the `+2` is important so that the final quote `"` at end of string is not cut off
}
numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(maxL))) + adder
if numEntries > 0 && (fmt.Sprintf("%#v", v)[0] == 34) { // 34 is the char("")
adder = 2
}
newLineCount := strings.Count(fmt.Sprintf("%s", v), "\n")

constraint := int(math.Min(float64(numEntries), float64(cfg.MaxLength+50))) + adder + newLineCount

s := fmt.Sprintf("%#v", v)[:constraint]

Expand Down Expand Up @@ -218,9 +222,8 @@ func dumpSlice(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// 1. https://github.com/sanity-io/litter/pull/43
// 2. https://github.com/komuw/kama/pull/28

maxL := 20
numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(maxL)))
constraint := int(math.Min(float64(numEntries), float64(cfg.MaxLength)))
typeName := v.Type().String()

newline := "\n"
Expand Down Expand Up @@ -260,9 +263,8 @@ func dumpMap(v reflect.Value, hideZeroValues bool, indentLevel int) string {
// 1. https://github.com/sanity-io/litter/pull/43
// 2. https://github.com/komuw/kama/pull/28

maxL := 20
numEntries := v.Len()
constraint := int(math.Min(float64(numEntries), float64(maxL)))
constraint := int(math.Min(float64(numEntries), float64(cfg.MaxLength)))
typeName := v.Type().String()

newline := "\n"
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module github.com/komuw/kama
go 1.21

require (
golang.org/x/sys v0.11.0
golang.org/x/tools v0.12.0
golang.org/x/sys v0.16.0
golang.org/x/tools v0.17.0
)

require (
github.com/google/go-cmp v0.5.9 // indirect
golang.org/x/mod v0.12.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/mod v0.14.0 // indirect
)

require (
github.com/pkg/errors v0.9.1 // test
go.akshayshah.org/attest v1.0.2 // test
go.uber.org/goleak v1.2.1 // test
go.uber.org/goleak v1.3.0 // test
)
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -10,15 +10,15 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
go.akshayshah.org/attest v1.0.2 h1:qOv9PXCG2mwnph3g0I3yZj0rLAwLyUITs8nhxP+wS44=
go.akshayshah.org/attest v1.0.2/go.mod h1:PnWzcW5j9dkyGwTlBmUsYpPnHG0AUPrs1RQ+HrldWO0=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
33 changes: 30 additions & 3 deletions kama.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ import (
"fmt"
"reflect"
"strings"
"sync"
)

var (
cfg = Config{MaxLength: 14} //nolint:gochecknoglobals
onceCfg *sync.Once //nolint:gochecknoglobals
)

// Config controls how printing is going to be done.
type Config struct {
// MaxLength is the length of slices/maps/strings that is going to be printed.
MaxLength int
}

// Dirp prints (to stdout) exported information of types, variables, packages, modules, imports
// It also pretty prints data structures.
//
Expand All @@ -27,12 +39,27 @@ import (
// kama.Dirp(&http.Request{})
// kama.Dirp("github.com/pkg/errors")
// kama.Dirp(http.Request{})
func Dirp(i interface{}) {
fmt.Println(Dir(i))
// kama.Dirp(http.Request{}, Config{999})
func Dirp(i interface{}, c ...Config) {
fmt.Println(Dir(i, c...))
}

// Dir returns exported information of types, variables, packages, modules, imports
func Dir(i interface{}) string {
func Dir(i interface{}, c ...Config) string {
if len(c) > 0 {
onceCfg.Do(func() {
cfg = c[0]
if cfg.MaxLength < 1 {
cfg.MaxLength = 1
}
if cfg.MaxLength > 10_000 {
// the upper limit of a slice is some significant fraction of the address space of a process.
// https://github.com/golang/go/issues/38673#issuecomment-643885108
cfg.MaxLength = 10_000
}
})
}

iType := reflect.TypeOf(i)
if iType == nil {
res := newVari(i)
Expand Down
8 changes: 1 addition & 7 deletions testdata/e2e_test/map_in_a_struct_is_not_compacted.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ SNIPPET: someStructWithMap{
int(1009): "1009",
int(101): "101",
int(1010): "1010",
int(1011): "1011",
int(1012): "1012",
int(1013): "1013",
int(1014): "1014",
int(1015): "1015",
int(1016): "1016",
...<9980 more redacted>..
...<9986 more redacted>..
},
}
]
8 changes: 1 addition & 7 deletions testdata/e2e_test/map_on_its_own_is_not_compacted.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ SNIPPET: map[int]string{
int(1009): "1009",
int(101): "101",
int(1010): "1010",
int(1011): "1011",
int(1012): "1012",
int(1013): "1013",
int(1014): "1014",
int(1015): "1015",
int(1016): "1016",
...<9980 more redacted>..
...<9986 more redacted>..
}
]
38 changes: 4 additions & 34 deletions testdata/e2e_test/pointer_to_struct_of_varying_field_types.txt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,14 @@ SNIPPET: &SomeStruct{
Request{
Method: "13",
},
Request{
Method: "14",
},
Request{
Method: "15",
},
Request{
Method: "16",
},
Request{
Method: "17",
},
Request{
Method: "18",
},
Request{
Method: "19",
},
...<80 more redacted>..
...<86 more redacted>..
},
OneHttpRequest: Request{
Method: "Hello",
},
EmptyString: "",
SmallString: "What up?",
LargeString: "AT last the sleepy atmosphere was stirred—and vigorously: the murder trial came on in the court. It ...<3404 more redacted>..,
LargeString: "AT last the sleepy atmosphere was stirred—and vigorously: the murder trial came on in the ...<3414 more redacted>..,
DistinctType: kama_test.Distance(9131),
SomeNilError: error(nil),
SomeConcreteError: error(Houston something bad happened),
Expand All @@ -125,13 +107,7 @@ SNIPPET: &SomeStruct{
int(11),
int(12),
int(13),
int(14),
int(15),
int(16),
int(17),
int(18),
int(19),
...<9980 more redacted>..
...<9986 more redacted>..
},
LargeMap: map[int]string{
int(0): "0",
Expand All @@ -150,13 +126,7 @@ SNIPPET: &SomeStruct{
int(1009): "1009",
int(101): "101",
int(1010): "1010",
int(1011): "1011",
int(1012): "1012",
int(1013): "1013",
int(1014): "1014",
int(1015): "1015",
int(1016): "1016",
...<9980 more redacted>..
...<9986 more redacted>..
},
UndirectedChan: chan int (len=122, cap=10000),
DirectedChan: chan<- bool (len=1, cap=13),
Expand Down
8 changes: 1 addition & 7 deletions testdata/e2e_test/slice_in_a_struct_is_not_compacted.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,7 @@ SNIPPET: someStructWithSlice{
int(11),
int(12),
int(13),
int(14),
int(15),
int(16),
int(17),
int(18),
int(19),
...<9980 more redacted>..
...<9986 more redacted>..
},
}
]
Loading

0 comments on commit f3bd2f2

Please sign in to comment.