Add noexponent struct tag for field-level float formatting #432
+133
−3
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.
Summary
This PR implements field-level control of float formatting via the
json:"field,noexponent"struct tag, as discussed in #425.Instead of relying on global CLI flags, this allows developers to suppress exponential notation on a per-field basis using the
'f'format instead of the default'g'format.Implementation
Follows the existing pattern where different struct tags select different Writer methods (e.g.,
asString→Float64Str()).Changes:
noExponent booltofieldTagsstruct ingen/encoder.gonoexponenttag inparseFieldTags()jwriter/writer.go:Float32NoExp(),Float64NoExp()Float32StrNoExp(),Float64StrNoExp()primitiveNoExpEncodersandprimitiveStringNoExpEncodersgenTypeEncoderNoCheck()to check tag combinations and select appropriate encoderUsage Example
```go
type PriceData struct {
Scientific float64 `json:"sci"` // 1e8 (default 'g' format)
NoExp float64 `json:"price,noexponent"` // 100000000 ('f' format)
QuotedNoExp float64 `json:"str,string,noexponent"` // "100000000" (quoted + 'f')
}
```
Generated Code
The generator produces appropriate Writer method calls:
out.Float64(value)uses'g'formatnoexponent:out.Float64NoExp(value)uses'f'formatstring,noexponent:out.Float64StrNoExp(value)uses quoted'f'formatBenefits
omitempty,stringasString→Float64Str()patternCloses #425