Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle FloatFormat and DigitSeparator when formatting slices/maps #430

Open
gavv opened this issue Oct 7, 2023 · 2 comments · May be fixed by #434
Open

Handle FloatFormat and DigitSeparator when formatting slices/maps #430

gavv opened this issue Oct 7, 2023 · 2 comments · May be fixed by #434
Assignees
Labels
feature New feature or request help wanted Contributions are welcome

Comments

@gavv
Copy link
Owner

gavv commented Oct 7, 2023

In DefaultFormatter.formatValue, when value is a number, we format it accordingly to FloatFormat and DigitSeparator settings (see formatFloatValue and reformatNumber funcs).

However, if value is slice or map, and it has numbers inside, settings are ignored for those numbers. We need to fix it.

Slices and maps are formatted using one of the two ways:

  • json.MarshalIndent
  • litter.Sdump

We need to ensure that MarshalIndent handles FloatFormat, and Sdump handles both FloatFormat and DigitSeparator. (MarshalIndent can't handle DigitSeparator because it would be invalid JSON).

To achieve this, we need to do the following:

  • for MarshalIndent:
    • recursively inspect value using reflect
    • find all numbers (integers, floats, big.Float, json.Number)
    • replace each number with a wrapper type that holds the number (e.g. in big.Float) and implements json.Marshaler interface; its MarshalJSON method should format number according to FloatFormat setting
  • for Sdump:
    • do the same, but instead of json.Marshaler, implement litter.Dumper interface; its LitterDump method should format number according to FloatFormat and DigitSeparator settings

We also need to adjust "colorjson" func in formatter.go. By default, it will reformat numbers using strconv.FormatFloat. We can try to prevent it by using json.Decoder instead of json.Unmarshal to parse json, and using Decoder.UseNumber() method. It will tell decoder to store numbers as json.Number, which colorjson should format as-is.

@gavv gavv added feature New feature or request help wanted Contributions are welcome labels Oct 7, 2023
@gavv gavv changed the title Handle FloatFormat and DigitSeparator when formatting slices/maps/structs Handle FloatFormat and DigitSeparator when formatting slices/maps Oct 7, 2023
@nhAnik
Copy link
Contributor

nhAnik commented Oct 10, 2023

Hi @gavv , can I give it a try?

@gavv
Copy link
Owner Author

gavv commented Oct 10, 2023

Sure, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Contributions are welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants