Skip to content

Commit

Permalink
refactor: remove some uneeded type checks on pointers to interface
Browse files Browse the repository at this point in the history
In InferAcceptHeaderFromType we're simply check if the type
implements the interface. It's not needed (nor correct) to check
if the type is a pointer to interface as interfaces are already
references
  • Loading branch information
dylanhitt committed Feb 26, 2025
1 parent 8c69b3f commit 846131b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
10 changes: 0 additions & 10 deletions serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,21 +349,11 @@ func InferAcceptHeaderFromType(ans any) string {
return "text/html"
}

_, ok = any(&ans).(*CtxRenderer)
if ok {
return "text/html"
}

_, ok = ans.(Renderer)
if ok {
return "text/html"
}

_, ok = any(&ans).(*Renderer)
if ok {
return "text/html"
}

return "application/json"
}

Expand Down
16 changes: 16 additions & 0 deletions serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,10 +493,26 @@ func TestInferAcceptHeaderFromType(t *testing.T) {
require.Equal(t, "text/html", accept)
})

t.Run("can infer that reference type is a template (implements Renderer)", func(t *testing.T) {
accept := InferAcceptHeaderFromType(&templateMock{})
require.Equal(t, "text/html", accept)
})

t.Run("can infer that type is a template (implements CtxRenderer)", func(t *testing.T) {
accept := InferAcceptHeaderFromType(MockCtxRenderer{})
require.Equal(t, "text/html", accept)
})

t.Run("can infer that reference type is a template (implements CtxRenderer)", func(t *testing.T) {
accept := InferAcceptHeaderFromType(&MockCtxRenderer{})
require.Equal(t, "text/html", accept)
})

t.Run("can infer string reference", func(t *testing.T) {
s := "hello"
accept := InferAcceptHeaderFromType(&s)
require.Equal(t, "text/plain", accept)
})
}

func TestInferAcceptHeader(t *testing.T) {
Expand Down

0 comments on commit 846131b

Please sign in to comment.