Skip to content

Commit

Permalink
log/slog: use Infinity instead of Inf
Browse files Browse the repository at this point in the history
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 <joetsai@digital-static.net>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
dsnet authored and eric committed Sep 7, 2023
1 parent 42bd096 commit cd187aa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/log/slog/json_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/log/slog/json_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit cd187aa

Please sign in to comment.