From: Joe Tsai Date: Thu, 23 Mar 2023 07:29:08 +0000 (-0700) Subject: log/slog: use Infinity instead of Inf X-Git-Tag: go1.21rc1~1161 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=16544b83f8b4ef36fb9f93af7adb4b7135bdcc2d;p=gostls13.git log/slog: use Infinity instead of Inf JSON is derived from Javascript, so we should use Javascript-inspired literals instead of ones more common to Go. In Javascript, infinity is declared as Infinity rather than Inf. Change-Id: I6c81353d0c677640f3f11961a37d792408ac03fc Reviewed-on: https://go-review.googlesource.com/c/go/+/478758 Run-TryBot: Joseph Tsai Auto-Submit: Joseph Tsai Reviewed-by: Jonathan Amsterdam Reviewed-by: Heschi Kreinick TryBot-Result: Gopher Robot --- diff --git a/src/log/slog/json_handler.go b/src/log/slog/json_handler.go index 90ecc6a3a4..96545d58d6 100644 --- a/src/log/slog/json_handler.go +++ b/src/log/slog/json_handler.go @@ -79,7 +79,7 @@ func (h *JSONHandler) WithGroup(name string) Handler { // Values are formatted as with encoding/json.Marshal, with the following // exceptions: // - Floating-point NaNs and infinities are formatted as one of the strings -// "NaN", "+Inf" or "-Inf". +// "NaN", "Infinity" or "-Infinity". // - Levels are formatted as with Level.String. // - HTML characters are not escaped. // @@ -113,9 +113,9 @@ func appendJSONValue(s *handleState, v Value) error { // json.Marshal fails on special floats, so handle them here. switch { case math.IsInf(f, 1): - s.buf.WriteString(`"+Inf"`) + s.buf.WriteString(`"Infinity"`) case math.IsInf(f, -1): - s.buf.WriteString(`"-Inf"`) + s.buf.WriteString(`"-Infinity"`) case math.IsNaN(f): s.buf.WriteString(`"NaN"`) default: diff --git a/src/log/slog/json_handler_test.go b/src/log/slog/json_handler_test.go index 55e05d58e3..0a38969f46 100644 --- a/src/log/slog/json_handler_test.go +++ b/src/log/slog/json_handler_test.go @@ -111,8 +111,8 @@ func TestJSONAppendAttrValueSpecial(t *testing.T) { want string }{ {math.NaN(), `"NaN"`}, - {math.Inf(+1), `"+Inf"`}, - {math.Inf(-1), `"-Inf"`}, + {math.Inf(+1), `"Infinity"`}, + {math.Inf(-1), `"-Infinity"`}, {LevelWarn, `"WARN"`}, } { got := jsonValueString(t, AnyValue(test.value))