Skip to content

Commit

Permalink
Add Stringers field constructor for array of objects implementing fmt…
Browse files Browse the repository at this point in the history
….Stringer method
  • Loading branch information
saurabh95 committed Aug 22, 2022
1 parent 1e46f5e commit 126f793
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
30 changes: 29 additions & 1 deletion array_go118.go
Expand Up @@ -23,7 +23,11 @@

package zap

import "go.uber.org/zap/zapcore"
import (
"fmt"

"go.uber.org/zap/zapcore"
)

// Objects constructs a field with the given key, holding a list of the
// provided objects that can be marshaled by Zap.
Expand Down Expand Up @@ -122,3 +126,27 @@ func (os objectValues[T, P]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
}
return nil
}

// Stringers constructs a field with the given key, holding a list of the
// output provided by the value's String method
//
// Given an object that implements String on the value receiver, you
// can log a slice of those objects with Objects like so:
//
// type Request struct{ ... }
// func (a Request) String() string
//
// var requests []Request = ...
// logger.Info("sending requests", zap.Stringers("requests", requests))
func Stringers[T fmt.Stringer](key string, values []T) Field {
return Array(key, stringers[T](values))
}

type stringers[T fmt.Stringer] []T

func (ss stringers[T]) MarshalLogArray(arr zapcore.ArrayEncoder) error {
for _, s := range ss {
arr.AppendString(s.String())
}
return nil
}
45 changes: 45 additions & 0 deletions array_go118_test.go
Expand Up @@ -179,3 +179,48 @@ func TestObjectsAndObjectValues_marshalError(t *testing.T) {
})
}
}

type stringerObject struct{
value string
}

func (s stringerObject) String() string {
return s.value
}

func TestStringers(t *testing.T) {
t.Parallel()

tests := []struct {
desc string
give Field
want []any
}{
{
desc: "Stringers",
give: Stringers("", []stringerObject{
{value: "foo"},
{value: "bar"},
{value: "baz"},
}),
want: []any{
"foo",
"bar",
"baz",
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()

tt.give.Key = "k"

enc := zapcore.NewMapObjectEncoder()
tt.give.AddTo(enc)
assert.Equal(t, tt.want, enc.Fields["k"])
})
}
}

0 comments on commit 126f793

Please sign in to comment.