feat(binding): Support custom BindUnmarshaler for binding. #3933
+184
−0
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.
Feature
Add
binding.BindUnmarshaler
interface to support custom unmarshaling (from the suggestion in #2673)Description
This PR is inspired by the information provided on the following page: labstack/echo/v4#BindUnmarshaler and PR-3045.
As ssoroka (the author of PR-3045) mentioned:
Therefore, I would like to open a new pull request to implement this feature. However, I have identified two potential issues with PR-3045:
PR-3045 also introduces support for the
encoding.TextUnmarshaler
interface. I believe there might be compatibility issues with this approach. Instead, I suggest that users implement the new interface binding.BindUnmarshaler to achieve the desired custom unmarshaler functionality.In order to support arrays and slices, PR-3045 uses
vs[0]
to retrieve the first item. This approach may be susceptible to boundary issues. Upon closer examination of the implementation, I found that addressing this complexity might be challenging. As a temporary solution, I have decided not to support this functionality for arrays and slices. If there is a future demand from other users, we can consider implementing it at that time.Usage
Test it with:
curl 'localhost:8088/test?birthday=2000-01-01'
Result
"2000/01/01"
Testing
binding/form_mapping_test.go
gin_test.go
References