Skip to content

Commit

Permalink
feat: recursive FormatByType+FormatByKind+FormatByKey
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Sep 9, 2024
1 parent 7805e61 commit c5a6e5c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
60 changes: 57 additions & 3 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ func FormatByType[T any](formatter func(T) slog.Value) Formatter {
return formatter(v), true
}

if value.Kind() == slog.KindGroup {
updated := false
attrs := []slog.Attr{}

for _, attr := range value.Group() {
if v, ok := value.Any().(T); ok {
attrs = append(attrs, slog.Attr{Key: attr.Key, Value: formatter(v)})
updated = true
} else {
attrs = append(attrs, attr)
}
}

if updated {
return slog.GroupValue(attrs...), true
}
}

return value, false
}
}
Expand All @@ -42,6 +60,24 @@ func FormatByKind(kind slog.Kind, formatter func(slog.Value) slog.Value) Formatt
return formatter(value), true
}

if value.Kind() == slog.KindGroup {
updated := false
attrs := []slog.Attr{}

for _, attr := range value.Group() {
if attr.Value.Kind() == kind {
attrs = append(attrs, slog.Attr{Key: attr.Key, Value: formatter(attr.Value)})
updated = true
} else {
attrs = append(attrs, attr)
}
}

if updated {
return slog.GroupValue(attrs...), true
}
}

return value, false
}
}
Expand All @@ -51,11 +87,29 @@ func FormatByKey(key string, formatter func(slog.Value) slog.Value) Formatter {
return func(_ []string, attr slog.Attr) (slog.Value, bool) {
value := attr.Value

if attr.Key != key {
return value, false
if attr.Key == key {
return formatter(value), true
}

return formatter(value), true
if value.Kind() == slog.KindGroup {
updated := false
attrs := []slog.Attr{}

for _, attr := range value.Group() {
if attr.Key == key {
attrs = append(attrs, slog.Attr{Key: attr.Key, Value: formatter(attr.Value)})
updated = true
} else {
attrs = append(attrs, attr)
}
}

if updated {
return slog.GroupValue(attrs...), true
}
}

return value, false
}
}

Expand Down
18 changes: 14 additions & 4 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func TestFormatByKind(t *testing.T) {

is.Len(attrs, 2)
is.Equal(attrs["key"], slog.StringValue("value"))
is.Equal(attrs["duration"], slog.StringValue("1s"))
is.Equal(attrs["subgroup"].Kind(), slog.KindGroup)
is.Equal(attrs["subgroup"].Group()[0].Key, "duration")
is.Equal(attrs["subgroup"].Group()[0].Value, slog.StringValue("1s"))

atomic.AddInt32(&checked, 1)
return nil
Expand All @@ -48,7 +50,10 @@ func TestFormatByKind(t *testing.T) {

logger.Info("hello world",
slog.String("key", "value"),
slog.Duration("duration", 1*time.Second))
slog.Group("subgroup",
slog.Duration("duration", 1*time.Second),
),
)

is.Equal(int32(1), atomic.LoadInt32(&checked))
}
Expand Down Expand Up @@ -79,7 +84,9 @@ func TestFormatByKey(t *testing.T) {

is.Len(attrs, 2)
is.Equal(attrs["key"], slog.StringValue("value"))
is.Equal(attrs["duration"], slog.StringValue("1s"))
is.Equal(attrs["subgroup"].Kind(), slog.KindGroup)
is.Equal(attrs["subgroup"].Group()[0].Key, "duration")
is.Equal(attrs["subgroup"].Group()[0].Value, slog.StringValue("1s"))

atomic.AddInt32(&checked, 1)
return nil
Expand All @@ -90,7 +97,10 @@ func TestFormatByKey(t *testing.T) {

logger.Info("hello world",
slog.String("key", "value"),
slog.Duration("duration", 1*time.Second))
slog.Group("subgroup",
slog.Duration("duration", 1*time.Second),
),
)

is.Equal(int32(1), atomic.LoadInt32(&checked))
}

0 comments on commit c5a6e5c

Please sign in to comment.