Skip to content

Commit

Permalink
simplify de-duplication code
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pohly committed Nov 21, 2024
1 parent 5f92409 commit ab1b250
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 34 deletions.
28 changes: 4 additions & 24 deletions internal/serialize/keyvalues.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,15 @@ type Formatter struct {

type AnyToStringFunc func(v interface{}) string

// MergeKVsInto is a variant of MergeKVs which directly formats the key/value
// pairs into a buffer.
func (f Formatter) MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
f.formatKVs(b, first, second)
}

func MergeAndFormatKVs(b *bytes.Buffer, first, second []interface{}) {
Formatter{}.MergeAndFormatKVs(b, first, second)
}

const missingValue = "(MISSING)"

// KVListFormat serializes all key/value pairs into the provided buffer.
// A space gets inserted before the first pair and between each pair.
func (f Formatter) KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
f.formatKVs(b, keysAndValues)
}

func KVListFormat(b *bytes.Buffer, keysAndValues ...interface{}) {
Formatter{}.KVListFormat(b, keysAndValues...)
}

func KVFormat(b *bytes.Buffer, k, v interface{}) {
Formatter{}.KVFormat(b, k, v)
func FormatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
Formatter{}.FormatKVs(b, kvs...)
}

// formatKVs formats all key/value pairs such that the output contains no
// FormatKVs formats all key/value pairs such that the output contains no
// duplicates ("last one wins").
func (f Formatter) formatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
func (f Formatter) FormatKVs(b *bytes.Buffer, kvs ...[]interface{}) {
// De-duplication is done by optimistically formatting all key value
// pairs and then cutting out the output of those key/value pairs which
// got overwritten later.
Expand Down
2 changes: 1 addition & 1 deletion internal/serialize/keyvalues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ No whitespace.`,

for _, d := range testKVList {
b := &bytes.Buffer{}
serialize.KVListFormat(b, d.keysValues...)
serialize.FormatKVs(b, d.keysValues)
if b.String() != d.want {
t.Errorf("KVListFormat error:\n got:\n\t%s\nwant:\t%s", b.String(), d.want)
}
Expand Down
5 changes: 3 additions & 2 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,11 @@ func (l *loggingT) printS(err error, s severity.Severity, depth int, msg string,
b := buffer.GetBuffer()
b.Write(qMsg)

var errKV []interface{}
if err != nil {
serialize.KVListFormat(&b.Buffer, "err", err)
errKV = []interface{}{"err", err}
}
serialize.KVListFormat(&b.Buffer, keysAndValues...)
serialize.FormatKVs(&b.Buffer, errKV, keysAndValues)
l.printDepth(s, nil, nil, depth+1, &b.Buffer)
// Make the buffer available for reuse.
buffer.PutBuffer(b)
Expand Down
5 changes: 3 additions & 2 deletions klogr_slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ func slogOutput(file string, line int, now time.Time, err error, s severity.Seve
b := buffer.GetBuffer()
b.Write(qMsg)

var errKV []interface{}
if err != nil {
serialize.KVListFormat(&b.Buffer, "err", err)
errKV = []interface{}{"err", err}
}
serialize.KVListFormat(&b.Buffer, kvList...)
serialize.FormatKVs(&b.Buffer, errKV, kvList)

// See print + header.
buf := logging.formatHeader(s, file, line, now)
Expand Down
7 changes: 4 additions & 3 deletions ktesting/testinglogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (l tlogger) Info(level int, msg string, kvList ...interface{}) {

l.shared.t.Helper()
buf := buffer.GetBuffer()
l.shared.formatter.MergeAndFormatKVs(&buf.Buffer, l.values, kvList)
l.shared.formatter.FormatKVs(&buf.Buffer, l.values, kvList)
l.log(LogInfo, msg, level, buf, nil, kvList)
}

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

l.shared.t.Helper()
buf := buffer.GetBuffer()
var errKV []interface{}
if err != nil {
l.shared.formatter.KVFormat(&buf.Buffer, "err", err)
errKV = []interface{}{"err", err}
}
l.shared.formatter.MergeAndFormatKVs(&buf.Buffer, l.values, kvList)
l.shared.formatter.FormatKVs(&buf.Buffer, errKV, l.values, kvList)
l.log(LogError, msg, 0, buf, err, kvList)
}

Expand Down
5 changes: 3 additions & 2 deletions textlogger/textlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ func (l *tlogger) printWithInfos(file string, line int, now time.Time, err error

b.Write(qMsg)

var errKV []interface{}
if err != nil {
serialize.KVFormat(&b.Buffer, "err", err)
errKV = []interface{}{"err", err}
}
serialize.MergeAndFormatKVs(&b.Buffer, l.values, kvList)
serialize.FormatKVs(&b.Buffer, errKV, l.values, kvList)
if b.Len() == 0 || b.Bytes()[b.Len()-1] != '\n' {
b.WriteByte('\n')
}
Expand Down

0 comments on commit ab1b250

Please sign in to comment.