Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WithTemplates(nil) function #400

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Fix WithTemplates(nil) function #400

wants to merge 3 commits into from

Conversation

sdil
Copy link
Contributor

@sdil sdil commented Feb 12, 2025

Summary

Currently using WithTemplates(nil) gives me error like below because it cannot reference the template.Template object correctly

 panic: runtime error: invalid memory address or nil pointer dereference

 -> html/template.(*Template).lookupAndEscapeTemplate
 ->   /opt/homebrew/Cellar/go/1.23.6/libexec/src/html/template/template.go:146

    html/template.(*Template).ExecuteTemplate
      /opt/homebrew/Cellar/go/1.23.6/libexec/src/html/template/template.go:135
    github.com/go-fuego/fuego.StdRenderer.Render
      /Users/fadhil/go/pkg/mod/github.com/go-fuego/fuego@v0.17.0/html.go:95
    github.com/go-fuego/fuego.init.func4
      /Users/fadhil/go/pkg/mod/github.com/go-fuego/fuego@v0.17.0/serialization.go:258
    github.com/go-fuego/fuego.Send
      /Users/fadhil/go/pkg/mod/github.com/go-fuego/fuego@v0.17.0/serialization.go:90
    github.com/go-fuego/fuego.netHttpContext[...].Serialize
      /Users/fadhil/go/pkg/mod/github.com/go-fuego/fuego@v0.17.0/ctx.go:268
    github.com/go-fuego/fuego.Flow[...]
      /Users/fadhil/go/pkg/mod/github.com/go-fuego/fuego@v0.17.0/serve.go:142
    github.com/go-fuego/fuego.registerFuegoController[...].func1
      /Users/fadhil/go/pkg/mod/github.com/go-fuego/fuego@v0.17.0/serve.go:80
    net/http.HandlerFunc.ServeHTTP

This diff should fix the error and the error shouldn't be raised when the default directory ./templates exists.

@EwenQuim
Copy link
Member

Thanks! Maintainers never use WithTemplates, so there might be several edge cases not identified.

Can you add a test with require.Panics to ensure the behaviour you coded will remain in the future please?

@EwenQuim
Copy link
Member

Also please that your tests pass by running make before sending the PR.

Copy link
Member

@EwenQuim EwenQuim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests

@sdil
Copy link
Contributor Author

sdil commented Feb 12, 2025

Can you add a test with require.Panics to ensure the behaviour you coded will remain in the future please?

@EwenQuim sorry im not sure how to do this. Can you guide me please?

@EwenQuim
Copy link
Member

  1. Write a test
func TestWithTemplateNilShouldPanic(...
  fuego.NewServer(
    fuego.WithTemplates(nil)
  )
}

This should raise an error when running make

  1. Make the test OK
func TestWithTemplateNilShouldPanic(...
require.Panics(func() {
  fuego.NewServer(
    fuego.WithTemplates(nil)
  )
}}

@dylanhitt
Copy link
Collaborator

Hey @sdil an exact example can be found here for this: https://github.com/go-fuego/fuego/blob/main/engine_test.go#L43. You can place it in this test here: https://github.com/go-fuego/fuego/blob/main/server_test.go#L178

😄

@EwenQuim EwenQuim added the maintainance / not prio Non user-critical change that improves the codebase label Feb 26, 2025
@dylanhitt
Copy link
Collaborator

dylanhitt commented Mar 27, 2025

So I looked into this some. I'm not really understanding the reasoning for this approach? What is the need to pass WithTemplate(nil)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintainance / not prio Non user-critical change that improves the codebase
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants