gin icon indicating copy to clipboard operation
gin copied to clipboard

feat(binding): add CustomDecimal type for parsing decimal numbers wit…

Open aydinomer00 opened this issue 11 months ago • 1 comments

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 CustomDecimal type that embeds decimal.Decimal
  • Implemented the BindUnmarshaler interface 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

aydinomer00 avatar Jan 03 '25 09:01 aydinomer00

Is there any update or feedback regarding this PR? I’d be happy to make any improvements or adjustments if needed. Thank you!

aydinomer00 avatar Jan 04 '25 17:01 aydinomer00