Skip to content

Commit

Permalink
issues/25: zero value pointers should not panic (#37)
Browse files Browse the repository at this point in the history
What:
- Zero value pointers should not panic

Why:
- Fixes: #25
  • Loading branch information
komuw authored Feb 21, 2023
1 parent 0d615f2 commit 406f831
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 6 additions & 0 deletions kama_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ type SomeStructWIthSlice struct {
MyAwesome []int
}

var zeroValuePointer *http.Request

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

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 406f831

Please sign in to comment.