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

Multi return #137

Merged
merged 6 commits into from
Jul 22, 2024
Merged

Multi return #137

merged 6 commits into from
Jul 22, 2024

Conversation

EwenQuim
Copy link
Member

@EwenQuim EwenQuim commented Jul 2, 2024

Spec

Currently, return type is either JSON or XML for the whole app. This needs to be improved.

The route /recipes should send HTML with a list of recipes if the Accept Header is "text/html", or JSON if the Accept Header is "application/json", or XML if the Accept Header is "application/xml". This allows to build a traditional website in the same time as building an API.

This change is of course backward-compatible and existing controller will not change current behaviour. Performance should also not be degraded.

Example

A controller might look like this (see multi_return_test.go for more examples).

fuego.Get(s, "/data", func(c ContextNoBody) (DataOrTemplate[MyType], error) {
	entity := MyType{Name: "Ewen"}

	return DataOrTemplate[MyType]{
		Data: entity, // Used when asking for JSON or XML
		Template: MyTemplate(entity) // std template, Templ or Gomponent rendered when asking for HTML
	}, nil
})

multi_return.go Outdated Show resolved Hide resolved
@EwenQuim EwenQuim force-pushed the multi-return branch 3 times, most recently from 97643be to 5fcf04f Compare July 9, 2024 15:37
@EwenQuim EwenQuim force-pushed the multi-return branch 6 times, most recently from ff90e44 to 6575e90 Compare July 16, 2024 11:20
@EwenQuim EwenQuim changed the title [Draft] Multi return Multi return Jul 16, 2024
@EwenQuim
Copy link
Member Author

This is a lot cleaner and I think it is ready to merge. Examples of the new features in _test.go files.

Another thing is missing : when using this new DataOrTemplate structure, the generated OpenAPI spec is a bit buggy. But I'll fix this after reviewing PR #134, since it is very deeply linked. What do you think about this @dylanhitt ?

@EwenQuim EwenQuim marked this pull request as ready for review July 16, 2024 11:30
@dylanhitt
Copy link
Collaborator

Will look at it at the end of the week. 😄

@EwenQuim
Copy link
Member Author

EwenQuim commented Jul 16, 2024

Ready for review ✅

The generated OpenAPI schema should be correct.

When using DataOrTemplate[MyType], OpenAPI spec will only see MyType as it is the type return by JSON/XML APIs, the template is ignored.

This allow to work on classic "view" (from MVC pattern) routes and create an API at the same time. I was playing with this feature the last few weeks and it is very helpful.

@dylanhitt
Copy link
Collaborator

Will be looking at this Sunday night. Day job got in the way this week

Copy link
Collaborator

@dylanhitt dylanhitt left a comment

Choose a reason for hiding this comment

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

This is super clean and very clear. Just a few comments to address.

serialization.go Outdated Show resolved Hide resolved
serialization.go Show resolved Hide resolved
serialization.go Outdated Show resolved Hide resolved
@EwenQuim EwenQuim merged commit 30bb170 into main Jul 22, 2024
4 checks passed
@EwenQuim EwenQuim deleted the multi-return branch July 23, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants