From 406f831be8514c6b38f2027dfe5c88072800771e Mon Sep 17 00:00:00 2001 From: Komu Wairagu Date: Tue, 21 Feb 2023 17:53:08 +0300 Subject: [PATCH] issues/25: zero value pointers should not panic (#37) What: - Zero value pointers should not panic Why: - Fixes: https://github.com/komuw/kama/issues/25 --- CHANGELOG.md | 1 + README.md | 8 ++++---- kama_test.go | 6 ++++++ vars.go | 8 ++++++++ vars_test.go | 12 ++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af1435a..9efb703 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Most recent version is listed first. # v0.0.7 - Update to Go 1.20: https://github.com/komuw/kama/pull/34 - Better formatting for zero-length slices and maps: https://github.com/komuw/kama/pull/36 +- Zero value pointers should not panic: https://github.com/komuw/kama/pull/37 # v0.0.6 - Update dependencies: https://github.com/komuw/kama/pull/32 diff --git a/README.md b/README.md index 346ab41..5604ecc 100644 --- a/README.md +++ b/README.md @@ -166,13 +166,13 @@ SNIPPET: &Request{ Body: io.ReadCloser nil, GetBody: func() (io.ReadCloser, error), ContentLength: int64(0), - TransferEncoding: []string{}, + TransferEncoding: []string{(nil)}, Close: false, Host: "example.com", - Form: url.Values{}, - PostForm: url.Values{}, + Form: url.Values{(nil)}, + PostForm: url.Values{(nil)}, MultipartForm: *multipart.Form(nil), - Trailer: http.Header{}, + Trailer: http.Header{(nil)}, RemoteAddr: "", RequestURI: "", TLS: *tls.ConnectionState(nil), diff --git a/kama_test.go b/kama_test.go index 6730efa..e0af309 100644 --- a/kama_test.go +++ b/kama_test.go @@ -124,12 +124,18 @@ func (h myHandler) ServeHTTP(http.ResponseWriter, *http.Request) { func TestStdlibTypes(t *testing.T) { t.Parallel() + var req http.Request + var reqPtr *http.Request + tt := []interface{}{ errors.New, reflect.Value{}, http.Handle, http.HandleFunc, http.Handler(myHandler{}), + req, + &req, + reqPtr, } for _, v := range tt { diff --git a/vars.go b/vars.go index 52659c0..d511737 100644 --- a/vars.go +++ b/vars.go @@ -32,6 +32,14 @@ func newVari(i interface{}) vari { Val: dump(valueOfi, hideZeroValues, indentLevel), } } + if iType.Kind() == reflect.Pointer && valueOfi.IsNil() && valueOfi.IsZero() { + return vari{ + Name: "unknown", + Kind: reflect.Ptr, + Signature: []string{fmt.Sprintf("%v", iType)}, + Val: dump(valueOfi, hideZeroValues, indentLevel), + } + } typeKind := getKind(i) typeName := getName(i) diff --git a/vars_test.go b/vars_test.go index dac71a0..2de0c28 100644 --- a/vars_test.go +++ b/vars_test.go @@ -113,6 +113,8 @@ type SomeStructWIthSlice struct { MyAwesome []int } +var zeroValuePointer *http.Request + func TestBasicVariables(t *testing.T) { t.Parallel() @@ -153,6 +155,16 @@ func TestBasicVariables(t *testing.T) { }`, }, }, + { + tName: "zero value pointer", + variable: zeroValuePointer, + expected: vari{ + Name: "unknown", + Kind: reflect.Ptr, + Signature: []string{"*http.Request"}, + Val: `*http.Request(nil)`, + }, + }, { tName: "function", variable: ThisFunction,