Skip to content

Commit 034384c

Browse files
committed
address review feedback
Signed-off-by: Callum Styan <[email protected]>
1 parent 76ecec2 commit 034384c

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

internal/entryhuman/entry.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ func bracketedLevel(l slog.Level) string {
109109
}
110110
}
111111

112+
func writeSignedInt(w io.Writer, n int64) (bool, error) {
113+
var a [20]byte
114+
_, err := w.Write(strconv.AppendInt(a[:0], n, 10))
115+
return true, err
116+
}
117+
118+
func writeUnsignedInt(w io.Writer, n uint64) (bool, error) {
119+
var a [20]byte
120+
_, err := w.Write(strconv.AppendUint(a[:0], n, 10))
121+
return true, err
122+
}
123+
112124
// Optimization to avoid allocation of heap allocations/temporary strings via formatValue when dealing with primitive types.
113125
// It returns (handled, error). When handled is false, the caller should fall back to formatValue.
114126
func writeValueFast(w io.Writer, v interface{}) (bool, error) {
@@ -126,47 +138,27 @@ func writeValueFast(w io.Writer, v interface{}) (bool, error) {
126138

127139
// signed ints
128140
case int:
129-
var a [20]byte
130-
_, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10))
131-
return true, err
141+
return writeSignedInt(w, int64(x))
132142
case int8:
133-
var a [20]byte
134-
_, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10))
135-
return true, err
143+
return writeSignedInt(w, int64(x))
136144
case int16:
137-
var a [20]byte
138-
_, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10))
139-
return true, err
145+
return writeSignedInt(w, int64(x))
140146
case int32:
141-
var a [20]byte
142-
_, err := w.Write(strconv.AppendInt(a[:0], int64(x), 10))
143-
return true, err
147+
return writeSignedInt(w, int64(x))
144148
case int64:
145-
var a [20]byte
146-
_, err := w.Write(strconv.AppendInt(a[:0], x, 10))
147-
return true, err
149+
return writeSignedInt(w, x)
148150

149151
// unsigned ints
150152
case uint:
151-
var a [20]byte
152-
_, err := w.Write(strconv.AppendUint(a[:0], uint64(x), 10))
153-
return true, err
153+
return writeUnsignedInt(w, uint64(x))
154154
case uint8:
155-
var a [20]byte
156-
_, err := w.Write(strconv.AppendUint(a[:0], uint64(x), 10))
157-
return true, err
155+
return writeUnsignedInt(w, uint64(x))
158156
case uint16:
159-
var a [20]byte
160-
_, err := w.Write(strconv.AppendUint(a[:0], uint64(x), 10))
161-
return true, err
157+
return writeUnsignedInt(w, uint64(x))
162158
case uint32:
163-
var a [20]byte
164-
_, err := w.Write(strconv.AppendUint(a[:0], uint64(x), 10))
165-
return true, err
159+
return writeUnsignedInt(w, uint64(x))
166160
case uint64:
167-
var a [20]byte
168-
_, err := w.Write(strconv.AppendUint(a[:0], x, 10))
169-
return true, err
161+
return writeUnsignedInt(w, x)
170162

171163
// floats: prefer 'g' to keep output bounded (matches fmt default)
172164
case float32:

internal/entryhuman/entry_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"flag"
77
"fmt"
88
"io"
9+
"math"
910
"os"
1011
"testing"
1112
"time"
@@ -175,9 +176,11 @@ func TestEntry(t *testing.T) {
175176
Fields: slog.M(
176177
slog.F("zero_int", 0),
177178
slog.F("neg_int", -999),
178-
slog.F("max_int64", int64(9223372036854775807)),
179-
slog.F("min_int64", int64(-9223372036854775808)),
180-
slog.F("max_uint64", uint64(18446744073709551615)),
179+
slog.F("max_int64", math.MaxInt64),
180+
slog.F("min_int64", math.MinInt64),
181+
// math.MaxUint64 is an untyped constant, and by default the compiler will assume a number
182+
// value is an int, so we need an explicit cast to uint64 here.
183+
slog.F("max_uint64", uint64(math.MaxUint64)),
181184
slog.F("zero_float", 0.0),
182185
slog.F("neg_float", -123.456),
183186
),

0 commit comments

Comments
 (0)