Skip to content

Commit

Permalink
ensure there are no duplicate keys in "WithValues" func
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeyMosin committed Jan 14, 2022
1 parent 4482034 commit 829d96b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
43 changes: 30 additions & 13 deletions sdlogr.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func NewWithOptions(opts Options) logr.Logger {
depth: opts.Depth + 1,
logCallerInfo: opts.LogCallerInfo,
out: opts.Out,
valuesMap: make(map[string]interface{}, 1),
}
return logr.New(&l)
}
Expand All @@ -79,7 +80,8 @@ type sdLogr struct {
level int
depth int
prefix string
values string
valuesMap map[string]interface{}
valuesStr string
out io.Writer
logCallerInfo bool
}
Expand Down Expand Up @@ -119,8 +121,8 @@ func (l *sdLogr) Info(_ int, msg string, kvList ...interface{}) {
hasData = true
}

if l.values != "" {
buf.WriteString(l.values)
if l.valuesStr != "" {
buf.WriteString(l.valuesStr)
hasData = true
}

Expand Down Expand Up @@ -160,8 +162,8 @@ func (l *sdLogr) Error(err error, msg string, kvList ...interface{}) {
buf.WriteString(msg + ", ")
}

if l.values != "" {
buf.WriteString(l.values)
if l.valuesStr != "" {
buf.WriteString(l.valuesStr)
}

kvLen := len(kvList)
Expand All @@ -184,18 +186,33 @@ func (l sdLogr) WithName(name string) logr.LogSink {
func (l sdLogr) WithValues(kvList ...interface{}) logr.LogSink {
// because we have data before the values string...
n := len(kvList)
for i := 0; i < n; {
v := kvList[i]
msIndex := i & 1
// the last kv must always have the ", " separator msIndex = 1
i++
if n == i {
msIndex = 1
if n == 0 {
return &l
}

if (n & 1) != 0 {
// for some reason a value is missing
// let's add an empty string
kvList = append(kvList, emptyStringPlaceholder)
n++
}

for i := 0; i < n; i++ {
k := kvList[i]
if k == "" {
k = emptyStringPlaceholder
}
i++
l.valuesMap[fmt.Sprintf("%v", deref(k))] = kvList[i]
}

// rebuild the string
l.valuesStr = ""
for k, v := range l.valuesMap {
if v == "" {
v = emptyStringPlaceholder
}
l.values += fmt.Sprintf("%v", deref(v)) + mergeSeparators[msIndex]
l.valuesStr += k + ": " + fmt.Sprintf("%v", deref(v)) + ", "
}
return &l
}
Expand Down
22 changes: 22 additions & 0 deletions sdlogr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@ func TestLevel(t *testing.T) {

}

func TestWithValues(t *testing.T) {
log := sdlogr.New()
log = log.WithValues("vv", "vvk")
log.Info("msg1")
log = log.WithValues("vv", "vvk")
log.Info("msg2")
log = log.WithValues("vv1", "vvk")
log.Info("msg3")
}

func TestOldNew(t *testing.T) {
log := sdlogr.New()
log.Info("msg1")

logNew := log.WithValues("vv", "vvk").WithName("new")
logNew.Info("logNew msg1")

err := errors.New("test_err")
log.Error(err, "msg2")
logNew.Error(err, "msg2")
}

func TestCallDepth(t *testing.T) {
log := sdlogr.New().WithCallDepth(0)
log.Info("cd 0")
Expand Down

0 comments on commit 829d96b

Please sign in to comment.