Skip to content

Commit 662c5da

Browse files
committed
simplify de-duplication code
This is a follow-up which removes several functions which don't provide enough additional value anymore. This wasn't done earlier to keep the previous commits simple.
1 parent 5964d1b commit 662c5da

File tree

6 files changed

+18
-34
lines changed

6 files changed

+18
-34
lines changed

internal/serialize/keyvalues.go

+4-24
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,15 @@ type Formatter struct {
5858

5959
type AnyToStringFunc func(v interface{}) string
6060

61-
// MergeKVsInto is a variant of MergeKVs which directly formats the key/value
62-
// pairs into a buffer.
63-
func (f Formatter) MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
64-
f.formatKVs(b, first, second)
65-
}
66-
67-
func MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
68-
Formatter{}.MergeAndFormatKVs(b, first, second)
69-
}
70-
7161
const missingValue = "(MISSING)"
7262

73-
// KVListFormat serializes all key/value pairs into the provided buffer.
74-
// A space gets inserted before the first pair and between each pair.
75-
func (f Formatter) KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
76-
f.formatKVs(b, keysAndValues)
77-
}
78-
79-
func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
80-
Formatter{}.KVListFormat(b, keysAndValues...)
81-
}
82-
83-
func KVFormat(b *bytes.Buffer, k, v interface{}) {
84-
Formatter{}.KVFormat(b, k, v)
63+
func FormatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
64+
Formatter{}.FormatKVs(b, kvs...)
8565
}
8666

87-
// formatKVs formats all key/value pairs such that the output contains no
67+
// FormatKVs formats all key/value pairs such that the output contains no
8868
// duplicates ("last one wins").
89-
func (f Formatter) formatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
69+
func (f Formatter) FormatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
9070
// De-duplication is done by optimistically formatting all key value
9171
// pairs and then cutting out the output of those key/value pairs which
9272
// got overwritten later.

internal/serialize/keyvalues_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ No whitespace.`,
184184

185185
for _, d := range testKVList {
186186
b := &bytes.Buffer{}
187-
serialize.KVListFormat(b, d.keysValues...)
187+
serialize.FormatKVs(b, d.keysValues)
188188
if b.String() != d.want {
189189
t.Errorf("KVListFormat error:\n got:\n\t%s\nwant:\t%s", b.String(), d.want)
190190
}

klog.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,11 @@ func (l *loggingT) printS(err error, s severity.Severity, depth int, msg string,
819819
b := buffer.GetBuffer()
820820
b.Write(qMsg)
821821

822+
var errKV []interface{}
822823
if err != nil {
823-
serialize.KVListFormat(&b.Buffer, "err", err)
824+
errKV = []interface{}{"err", err}
824825
}
825-
serialize.KVListFormat(&b.Buffer, keysAndValues...)
826+
serialize.FormatKVs(&b.Buffer, errKV, keysAndValues)
826827
l.printDepth(s, nil, nil, depth+1, &b.Buffer)
827828
// Make the buffer available for reuse.
828829
buffer.PutBuffer(b)

klogr_slog.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ func slogOutput(file string, line int, now time.Time, err error, s severity.Seve
6969
b := buffer.GetBuffer()
7070
b.Write(qMsg)
7171

72+
var errKV []interface{}
7273
if err != nil {
73-
serialize.KVListFormat(&b.Buffer, "err", err)
74+
errKV = []interface{}{"err", err}
7475
}
75-
serialize.KVListFormat(&b.Buffer, kvList...)
76+
serialize.FormatKVs(&b.Buffer, errKV, kvList)
7677

7778
// See print + header.
7879
buf := logging.formatHeader(s, file, line, now)

ktesting/testinglogger.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (l tlogger) Info(level int, msg string, kvList ...interface{}) {
302302

303303
l.shared.t.Helper()
304304
buf := buffer.GetBuffer()
305-
l.shared.formatter.MergeAndFormatKVs(&buf.Buffer, l.values, kvList)
305+
l.shared.formatter.FormatKVs(&buf.Buffer, l.values, kvList)
306306
l.log(LogInfo, msg, level, buf, nil, kvList)
307307
}
308308

@@ -320,10 +320,11 @@ func (l tlogger) Error(err error, msg string, kvList ...interface{}) {
320320

321321
l.shared.t.Helper()
322322
buf := buffer.GetBuffer()
323+
var errKV []interface{}
323324
if err != nil {
324-
l.shared.formatter.KVFormat(&buf.Buffer, "err", err)
325+
errKV = []interface{}{"err", err}
325326
}
326-
l.shared.formatter.MergeAndFormatKVs(&buf.Buffer, l.values, kvList)
327+
l.shared.formatter.FormatKVs(&buf.Buffer, errKV, l.values, kvList)
327328
l.log(LogError, msg, 0, buf, err, kvList)
328329
}
329330

textlogger/textlogger.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ func (l *tlogger) printWithInfos(file string, line int, now time.Time, err error
132132

133133
b.Write(qMsg)
134134

135+
var errKV []interface{}
135136
if err != nil {
136-
serialize.KVFormat(&b.Buffer, "err", err)
137+
errKV = []interface{}{"err", err}
137138
}
138-
serialize.MergeAndFormatKVs(&b.Buffer, l.values, kvList)
139+
serialize.FormatKVs(&b.Buffer, errKV, l.values, kvList)
139140
if b.Len() == 0 || b.Bytes()[b.Len()-1] != '\n' {
140141
b.WriteByte('\n')
141142
}

0 commit comments

Comments
 (0)