-
Notifications
You must be signed in to change notification settings - Fork 612
Added variadic StrsV, ObjectsV, and StringersV #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -78,7 +78,10 @@ func (c Context) Object(key string, obj LogObjectMarshaler) Context { | |||||
| return c | ||||||
| } | ||||||
|
|
||||||
| // Object marshals an object that implement the LogObjectMarshaler interface. | ||||||
| // Objects adds the field key with objs to the logger context as an array of | ||||||
| // objects that implement the LogObjectMarshaler interface. | ||||||
| // | ||||||
| // This is the array version that accepts a slice of LogObjectMarshaler objects. | ||||||
| func (c Context) Objects(key string, objs []LogObjectMarshaler) Context { | ||||||
| e := c.l.scratchEvent() | ||||||
| e.Objects(key, objs) | ||||||
|
|
@@ -87,6 +90,14 @@ func (c Context) Objects(key string, objs []LogObjectMarshaler) Context { | |||||
| return c | ||||||
| } | ||||||
|
|
||||||
| // ObjectsV adds the field key with objs to the logger context as an array of | ||||||
| // objects that implement the LogObjectMarshaler interface. | ||||||
| // | ||||||
| // This is a variadic version that accepts a list of individual LogObjectMarshaler objects. | ||||||
| func (c Context) ObjectsV(key string, objs ...LogObjectMarshaler) Context { | ||||||
| return c.Objects(key, objs) | ||||||
| } | ||||||
|
|
||||||
| // EmbedObject marshals and Embeds an object that implement the LogObjectMarshaler interface. | ||||||
| func (c Context) EmbedObject(obj LogObjectMarshaler) Context { | ||||||
| e := c.l.scratchEvent() | ||||||
|
|
@@ -103,11 +114,20 @@ func (c Context) Str(key, val string) Context { | |||||
| } | ||||||
|
|
||||||
| // Strs adds the field key with val as a string to the logger context. | ||||||
| // | ||||||
| // This is the array version that accepts a slice of string values. | ||||||
| func (c Context) Strs(key string, vals []string) Context { | ||||||
| c.l.context = enc.AppendStrings(enc.AppendKey(c.l.context, key), vals) | ||||||
| return c | ||||||
| } | ||||||
|
|
||||||
| // StrsV adds the field key with vals as a []string to the logger context. | ||||||
| // | ||||||
| // This is a variadic version that accepts a list of individual strings. | ||||||
| func (c Context) StrsV(key string, vals ...string) Context { | ||||||
| return c.Strs(key, vals) | ||||||
| } | ||||||
|
|
||||||
| // Stringer adds the field key with val.String() (or null if val is nil) to the logger context. | ||||||
| func (c Context) Stringer(key string, val fmt.Stringer) Context { | ||||||
| if val != nil { | ||||||
|
|
@@ -119,18 +139,24 @@ func (c Context) Stringer(key string, val fmt.Stringer) Context { | |||||
| return c | ||||||
| } | ||||||
|
|
||||||
| // Stringers adds the field key with vals as an array of strings by calling .String() on each entry | ||||||
| // to the logger context. | ||||||
| // Stringers adds the field key with vals to the logger context where each | ||||||
| // individual val is added by calling val.String(). | ||||||
| // | ||||||
| // This is the array version that accepts a slice of fmt.Stringer values. | ||||||
| func (c Context) Stringers(key string, vals []fmt.Stringer) Context { | ||||||
| if vals != nil { | ||||||
| c.l.context = enc.AppendStringers(enc.AppendKey(c.l.context, key), vals) | ||||||
| return c | ||||||
| } | ||||||
|
|
||||||
| c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), nil) | ||||||
| c.l.context = enc.AppendStringers(enc.AppendKey(c.l.context, key), vals) | ||||||
| return c | ||||||
| } | ||||||
|
|
||||||
| // StringersV adds the field key with vals to the logger context where each | ||||||
|
||||||
| // StringersV adds the field key with vals to the logger context where each | |
| // StringersV adds the field key with vals to the logger context where each |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //go:build go1.18 | ||
| // +build go1.18 | ||
|
|
||
| package zerolog | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| func AsLogObjectMarshalers[T LogObjectMarshaler](objs []T) []LogObjectMarshaler { | ||
| if objs == nil { | ||
| return nil | ||
| } | ||
| s := make([]LogObjectMarshaler, len(objs)) | ||
| for i, v := range objs { | ||
| s[i] = v | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| func AsStringers[T fmt.Stringer](objs []T) []fmt.Stringer { | ||
| if objs == nil { | ||
| return nil | ||
| } | ||
| s := make([]fmt.Stringer, len(objs)) | ||
| for i, v := range objs { | ||
| s[i] = v | ||
| } | ||
| return s | ||
|
Comment on lines
+10
to
+29
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Context.Stringerschanged behavior forvals == nil: it now encodes an empty array ([]) instead ofnull(previously done viaAppendInterface(..., nil)). If this is intentional for consistency with other plural helpers, it should be called out explicitly as a behavior change (it can affect downstream log consumers). If it’s not intended, restore the nil special-case.