Skip to content

Commit

Permalink
issues/69: Display stacktrace when dumping variables (#73)
Browse files Browse the repository at this point in the history
- Fixes: #69
- There's going to be follow up work in #75 to regenerate the test examples.
  • Loading branch information
komuw authored Mar 1, 2024
1 parent f6222de commit af7087e
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Most recent version is listed first.
# v0.0.20
- Support older Go versions: https://github.com/komuw/kama/pull/71
- Stackp can now write to any io.writer: https://github.com/komuw/kama/pull/74
- Display stacktrace when dumping variables: https://github.com/komuw/kama/pull/73

# v0.0.19
- Update docs: https://github.com/komuw/kama/commit/3dfdef76fbdd55a8ebed2d01dfb1cbb7c1cf2fc2
Expand Down
31 changes: 31 additions & 0 deletions kama_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,34 @@ func TestDiff(t *testing.T) {
})
}
}

func TestDirWithStack(t *testing.T) {
t.Parallel()

req, _ := http.NewRequest("GET", "https://example.com", nil)
req.Header.Set("Content-Type", "application/octet-stream")
req.AddCookie(&http.Cookie{Name: "hello", Value: "world"})

tt := []struct {
tName string
item interface{}
}{
{
tName: "http request with stack",
item: req,
},
}

for _, v := range tt {
v := v

t.Run(v.tName, func(t *testing.T) {
t.Parallel()

res := Dir(v.item)

path := getDataPath(t, "kama_test.go", v.tName)
dealWithTestData(t, path, res)
})
}
}
96 changes: 96 additions & 0 deletions testdata/kama_test/http_request_with_stack.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@

[
NAME: net/http.Request
KIND: struct
SIGNATURE: [*http.Request http.Request]
FIELDS: [
Method string
URL *url.URL
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
Body io.ReadCloser
GetBody func() (io.ReadCloser, error)
ContentLength int64
TransferEncoding []string
Close bool
Host string
Form url.Values
PostForm url.Values
MultipartForm *multipart.Form
Trailer http.Header
RemoteAddr string
RequestURI string
TLS *tls.ConnectionState
Cancel <-chan struct {}
Response *http.Response
]
METHODS: [
AddCookie func(*http.Request, *http.Cookie)
BasicAuth func(*http.Request) (string, string, bool)
Clone func(*http.Request, context.Context) *http.Request
Context func(*http.Request) context.Context
Cookie func(*http.Request, string) (*http.Cookie, error)
Cookies func(*http.Request) []*http.Cookie
FormFile func(*http.Request, string) (multipart.File, *multipart.FileHeader, error)
FormValue func(*http.Request, string) string
MultipartReader func(*http.Request) (*multipart.Reader, error)
ParseForm func(*http.Request) error
ParseMultipartForm func(*http.Request, int64) error
PathValue func(*http.Request, string) string
PostFormValue func(*http.Request, string) string
ProtoAtLeast func(*http.Request, int, int) bool
Referer func(*http.Request) string
SetBasicAuth func(*http.Request, string, string)
SetPathValue func(*http.Request, string, string)
UserAgent func(*http.Request) string
WithContext func(*http.Request, context.Context) *http.Request
Write func(*http.Request, io.Writer) error
WriteProxy func(*http.Request, io.Writer) error
]
STACK_TRACE: [
LEGEND:
compiler: blue
thirdParty: yellow
yours: red

 /home/komu/mystuff/kama/kama.go:129 github.com/komuw/kama.Dir
 /home/komu/mystuff/kama/kama_test.go:315 github.com/komuw/kama.TestDirWithStack.func1
 /usr/local/go/src/testing/testing.go:1689 testing.tRunner
 /usr/local/go/src/runtime/asm_amd64.s:1695 runtime.goexit
]
SNIPPET: &Request{
Method: "GET",
URL: &URL{
Scheme: "https",
Host: "example.com",
},
Proto: "HTTP/1.1",
ProtoMajor: int(1),
ProtoMinor: int(1),
Header: http.Header{
"Content-Type": []string{
"application/octet-stream",
},
"Cookie": []string{
"hello=world",
},
},
Body: io.ReadCloser nil,
GetBody: func() (io.ReadCloser, error),
ContentLength: int64(0),
TransferEncoding: []string{(nil)},
Close: false,
Host: "example.com",
Form: url.Values{(nil)},
PostForm: url.Values{(nil)},
MultipartForm: *multipart.Form(nil),
Trailer: http.Header{(nil)},
RemoteAddr: "",
RequestURI: "",
TLS: *tls.ConnectionState(nil),
Cancel: <-chan struct {} (len=0, cap=0),
Response: *http.Response(nil),
}
]
7 changes: 7 additions & 0 deletions vars.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kama

import (
"bytes"
"fmt"
"reflect"
"runtime"
Expand Down Expand Up @@ -72,6 +73,9 @@ func (v vari) String() string {
return fm
}

w := &bytes.Buffer{}
stackp(w)

return fmt.Sprintf(
`
[
Expand All @@ -80,6 +84,8 @@ KIND: %v
SIGNATURE: %v
FIELDS: %v
METHODS: %v
STACK_TRACE: [
%v]
SNIPPET: %s
]
`,
Expand All @@ -88,6 +94,7 @@ SNIPPET: %s
v.Signature,
nLf(v.Fields),
nLf(v.Methods),
strings.TrimRight(w.String(), "\n"),
v.Val,
)
}
Expand Down

0 comments on commit af7087e

Please sign in to comment.