Skip to content

Conversation

@rhnvrm
Copy link

@rhnvrm rhnvrm commented Oct 31, 2025

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., asStringFloat64Str()).

Changes:

  • Added noExponent bool to fieldTags struct in gen/encoder.go
  • Parse noexponent tag in parseFieldTags()
  • Added new Writer methods in jwriter/writer.go:
    • Float32NoExp(), Float64NoExp()
    • Float32StrNoExp(), Float64StrNoExp()
  • Added encoder maps: primitiveNoExpEncoders and primitiveStringNoExpEncoders
  • Updated genTypeEncoderNoCheck() to check tag combinations and select appropriate encoder
  • Added test cases demonstrating the feature

Usage 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:

  • No tag: out.Float64(value) uses 'g' format
  • noexponent: out.Float64NoExp(value) uses 'f' format
  • string,noexponent: out.Float64StrNoExp(value) uses quoted 'f' format

Benefits

  • Flexibility: Mix scientific and non-scientific notation in the same struct
  • Composable: Works with other tags like omitempty, string
  • Pattern-compliant: Follows existing asStringFloat64Str() pattern
  • Zero breaking changes: Opt-in, defaults unchanged

Closes #425

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
@rhnvrm rhnvrm marked this pull request as ready for review October 31, 2025 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Configurable Float Formatting

1 participant