swap from standalone attrs call to slog.LogValuer interface

This commit is contained in:
Jay
2026-05-09 11:36:33 -04:00
parent bd05712982
commit ba0e0ac925
3 changed files with 26 additions and 37 deletions
+7 -13
View File
@@ -17,6 +17,7 @@ type Component interface {
Module() string
Path() []string
PathString() string
LogValue() slog.Value
}
type component struct {
@@ -27,6 +28,12 @@ type component struct {
func (c component) Module() string { return c.module }
func (c component) Path() []string { return slices.Clone(c.path) }
func (c component) PathString() string { return strings.Join(c.path, ".") }
func (c component) LogValue() slog.Value {
return slog.GroupValue(
slog.String("module", c.module),
slog.String("path", c.PathString()),
)
}
func insert(ctx context.Context, module string, name string, path []string) context.Context {
return context.WithValue(ctx, storageKey, component{
@@ -117,16 +124,3 @@ func GetFields(ctx context.Context) (map[string]string, bool) {
"path": c.PathString(),
}, true
}
// Attrs returns the slog projection of GetFields.
func Attrs(ctx context.Context) ([]slog.Attr, bool) {
fields, ok := GetFields(ctx)
if !ok {
return nil, false
}
return []slog.Attr{
slog.String("module", fields["module"]),
slog.String("path", fields["path"]),
}, true
}