Skip to content

Commit

Permalink
fix: protect slog.Group, fix Append
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Jan 27, 2024
1 parent 6dc5129 commit 1010209
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions examples/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func TestSlogging(t *testing.T) {

func NestedParam(value string) logcore.SlogParam {
return func(c *logcore.SlogGroup) {
c.Append(slog.Group("nested", logcore.AttrsToAny(logcore.TurnMapToAttrs(map[string]any{
c.Append(logcore.Group("nested", logcore.TurnMapToAttrs(map[string]any{
"smth": "abc",
"number": 123,
}))...))
})...))
}
}

Expand All @@ -50,7 +50,7 @@ type Smth struct {
func NestedStructParam(value string) logcore.SlogParam {
return func(c *logcore.SlogGroup) {
c.Append(
slog.Group("nested", logcore.AttrsToAny(logcore.TurnStructToAttrs(Smth{Value1: "123", Number1: 4}))...),
logcore.Group("nested", logcore.TurnStructToAttrs(Smth{Value1: "123", Number1: 4})...),
slog.Int("not_nested", 345),
)
}
Expand Down
4 changes: 2 additions & 2 deletions logcore/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (s SlogGroup) Render() []SlogAttr {
return s.slogs
}

func (s SlogGroup) Append(params ...slog.Attr) {
func (s *SlogGroup) Append(params ...slog.Attr) {
for _, param := range params {
s.slogs = append(s.slogs, param)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ func Struct(value any) SlogParam {
func NestedStruct(key string, value any) SlogParam {
return func(c *SlogGroup) {
attrs := TurnMapToAttrs(StructToMap(value))
c.Append(slog.Group(key, AttrsToAny(attrs)...))
c.Append(Group(key, attrs...))
}
}

Expand Down
10 changes: 5 additions & 5 deletions logcore/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ func CompL[T any, V any](objs []T, lambda func(x T) V) []V {
return results
}

func AttrsToAny(attrs []slog.Attr) []any {
return CompL(attrs, func(x slog.Attr) any { return any(x) })
}

func logGroupFiles() slog.Attr {
return slog.Group("files",
"file3", GetCallingFile(3),
Expand Down Expand Up @@ -61,7 +57,7 @@ func TurnMapToAttrs(params map[string]any) []slog.Attr {
case time.Time:
anies = append(anies, slog.Time(key, v))
case map[string]any:
anies = append(anies, slog.Group(key, AttrsToAny(TurnMapToAttrs(v))...))
anies = append(anies, Group(key, TurnMapToAttrs(v)...))
default:
anies = append(anies, slog.String(key, fmt.Sprintf("%v", v)))
}
Expand All @@ -73,3 +69,7 @@ func TurnMapToAttrs(params map[string]any) []slog.Attr {
func TurnStructToAttrs(somestruct any) []slog.Attr {
return TurnMapToAttrs(StructToMap(somestruct))
}

func Group(name string, attrs ...slog.Attr) slog.Attr {
return slog.Group(name, CompL(attrs, func(x slog.Attr) SlogAttr { return SlogAttr(x) })...)
}

0 comments on commit 1010209

Please sign in to comment.