gavv / httpexpect

End-to-end HTTP and REST API testing for Go.
https://pkg.go.dev/github.com/gavv/httpexpect/v2
MIT License
2.53k stars 237 forks source link

Handle FloatFormat and DigitSeparator when formatting slices/maps #430

Open gavv opened 12 months ago

gavv commented 12 months ago

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:

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:

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.

nhAnik commented 12 months ago

Hi @gavv , can I give it a try?

gavv commented 12 months ago

Sure, thanks!