Add noexponent struct tag for field-level float formatting#432
Open
rhnvrm wants to merge 1 commit intomailru:masterfrom
Open
Add noexponent struct tag for field-level float formatting#432rhnvrm wants to merge 1 commit intomailru:masterfrom
rhnvrm wants to merge 1 commit intomailru:masterfrom
Conversation
Implements field level control of float formatting via json:"field,noexponent" struct tag to suppress exponential notation using format 'f' instead of 'g'. This allows mixing scientific and non-scientific notation in the same struct, providing more flexibility than global CLI flags. Implementation: - Add noExponent bool to fieldTags struct (gen/encoder.go) - Parse noexponent tag in parseFieldTags - Add Float32NoExp, Float64NoExp, Float32StrNoExp, Float64StrNoExp methods to jwriter.Writer - Add primitiveNoExpEncoders and primitiveStringNoExpEncoders encoder maps - Update genTypeEncoderNoCheck to check tag combinations and use appropriate encoder - Add test cases demonstrating the feature Generated code calls appropriate Writer methods: - Default: out.Float64() uses 'g' format - With noexponent: out.Float64NoExp() uses 'f' format - With string,noexponent: out.Float64StrNoExp() uses quoted 'f' format Follows existing pattern of asString tag → Float64Str() method. Related to issue mailru#425
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.
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