gin
gin copied to clipboard
feat(binding): add CustomDecimal type for parsing decimal numbers wit…
Description
This commit adds support for parsing decimal numbers that start with a dot (e.g. ".1") in query parameters and form data. It implements the BindUnmarshaler interface to handle this special case.
Changes
- Added
CustomDecimaltype that embedsdecimal.Decimal - Implemented the
BindUnmarshalerinterface to parse decimal values with leading dots - Added comprehensive tests for validation
- Provided an example usage under
examples/custom-decimal
Example Usage
type QueryParams struct {
Amount binding.CustomDecimal `form:"amount"`
}
// With this change:
// GET /amount?amount=.1 -> parses as "0.1"
// GET /amount?amount=1.23 -> parses as "1.23"
// Invalid formats return appropriate errors
Testing
Added unit tests covering various decimal formats (leading dot, valid decimals, invalid strings, etc.)
All tests are passing
Manually tested with the example application and verified
Fixes #4089
Checklist
I have performed a self-review of my code
I have added tests for my changes
My changes generate no new warnings
I have added an example demonstrating the functionality
Is there any update or feedback regarding this PR? I’d be happy to make any improvements or adjustments if needed. Thank you!