🔥 v3: update Ctx.Format to match Express's res.format#2766
Merged
ReneWerner87 merged 4 commits intogofiber:v3-betafrom Jan 4, 2024
Merged
🔥 v3: update Ctx.Format to match Express's res.format#2766ReneWerner87 merged 4 commits intogofiber:v3-betafrom
ReneWerner87 merged 4 commits intogofiber:v3-betafrom
Conversation
0d4d1c0 to
14e2cda
Compare
While the existing Ctx.Format provides a concise convenience method for basic content negotiation on simple structures, res.format allows developers to set their own custom handlers for each content type. The existing Ctx.Format is renamed to Ctx.AutoFormat.
63433fb to
aa86933
Compare
Member
|
Thanks @nickajacks1 quick glance on mobile looks good. I will review tomorrow. |
efectn
reviewed
Dec 18, 2023
efectn
reviewed
Dec 18, 2023
efectn
reviewed
Dec 18, 2023
sixcolors
reviewed
Dec 18, 2023
sixcolors
approved these changes
Dec 18, 2023
Member
|
thanks really cool improvement |
- Rename Fmt to ResFmt - Add comments in several places - Return errors instead of panicking in Format - Add 'Accept' to the Vary header in Format to match res.format
a72a783 to
2b0cd34
Compare
gaby
reviewed
Dec 23, 2023
gaby
reviewed
Dec 23, 2023
Member
gaby
left a comment
There was a problem hiding this comment.
Not sure if I missed it, but there's no test using a json object?
Member
Author
Do you mean for Format or AutoFormat? For Format, there's not a lot of significance on specifically which content types are tested since you'll be calling For AutoFormat there's this: c.Request().Header.Set(HeaderAccept, MIMEApplicationJSON)
err = c.AutoFormat("Hello, World!")
require.NoError(t, err)
require.Equal(t, `"Hello, World!"`, string(c.Response().Body()))But maybe it would be nice to add something that takes advantage of struct tags to get better use-case coverage. I can add that tomorrow. |
3 tasks
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
While the existing Ctx.Format provides a concise convenience method for basic content negotiation on simple structures, res.format allows developers to set their own custom handlers for each content type.
Docs: moved the existing Ctx.Format docs to a section called AutoFormat and added docs for the new Ctx.Format.
I'm taking suggestions for better names than
Fmtfor the mediaType-handler struct...was just worried about making it overly long since people will need to type it over and over.Benchmarks
Performs ever so slightly better than AutoFormat (formerly known as Format)
Related to #2745
Migration
To make their code compatible with Fiber v3, users need only replace their uses of
c.Formattoc.AutoFormat.To make full use of the new
c.Format, users should note where in their application they are performing manual content negotiation and consider ifc.Formatwill simplify their code.Changes Introduced
Type of Change
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/directory for Fiber's documentation.