3 Commits

Author SHA1 Message Date
jay f51a11ba00 remove error returns from component getter functions 2026-05-11 17:34:33 -04:00
jay ad04a920fd add c2p 2026-05-10 09:44:58 -04:00
jay 431f06b9f6 fix formatting 2026-05-09 11:58:08 -04:00
4 changed files with 35 additions and 34 deletions
+5 -4
View File
@@ -3,6 +3,7 @@
Component identity propagation for layered Go libraries. Component identity propagation for layered Go libraries.
Source: https://git.wisehodl.dev/jay/go-mana-component Source: https://git.wisehodl.dev/jay/go-mana-component
Mirror: https://github.com/wisehodl/go-mana-component Mirror: https://github.com/wisehodl/go-mana-component
## What this library does ## What this library does
@@ -47,7 +48,7 @@ logger to carry `module` and `path` automatically.
func NewPool(ctx context.Context, id string, handler slog.Handler) (*Pool, error) { func NewPool(ctx context.Context, id string, handler slog.Handler) (*Pool, error) {
ctx = component.MustNew(ctx, "honeybee", "outbound_pool") ctx = component.MustNew(ctx, "honeybee", "outbound_pool")
c, _ := component.Get(ctx) c := component.FromContext(ctx)
logger := slog.New(handler).With(slog.Any("component", c), slog.String("pool_id", id)) logger := slog.New(handler).With(slog.Any("component", c), slog.String("pool_id", id))
return &Pool{ctx: ctx, logger: logger}, nil return &Pool{ctx: ctx, logger: logger}, nil
@@ -63,7 +64,7 @@ path. No parent identifiers need to be passed as arguments.
func NewWorker(ctx context.Context, id string, handler slog.Handler) (*Worker, error) { func NewWorker(ctx context.Context, id string, handler slog.Handler) (*Worker, error) {
ctx = component.MustExtend(ctx, "outbound_worker") ctx = component.MustExtend(ctx, "outbound_worker")
c, _ := component.Get(ctx) c := component.FromContext(ctx)
logger := slog.New(handler).With(slog.Any("component", c), slog.Any("peer_id", id)) logger := slog.New(handler).With(slog.Any("component", c), slog.Any("peer_id", id))
return &Worker{ctx: ctx, logger: logger}, nil return &Worker{ctx: ctx, logger: logger}, nil
@@ -84,8 +85,8 @@ component is a programming error that should halt execution immediately.
**Generic Output**: **Generic Output**:
`GetFields` provides the component fields as a `map[string]string` for non-slog `FieldsFromContext` provides the component fields as a `map[string]string` for non-slog
consumers such as OpenTelemetry or Prometheus. consumers such as OpenTelemetry or Prometheus. It returns `nil` if no component is present.
## Testing ## Testing
Executable
+1
View File
@@ -0,0 +1 @@
code2prompt -c -e go.sum -e bump.sh -e c2p
+17 -15
View File
@@ -42,23 +42,25 @@ func insert(ctx context.Context, module string, name string, path []string) cont
}) })
} }
// Get retrieves the current component from the context; returns false if none is present. // FromContext retrieves the current component from the context; returns nil if none is present.
func Get(ctx context.Context) (Component, bool) { func FromContext(ctx context.Context) Component {
t, ok := ctx.Value(storageKey).(component) c, ok := ctx.Value(storageKey).(component)
return t, ok
}
// GetFields returns the component as a string map with keys "module" and "path".
func GetFields(ctx context.Context) (map[string]string, bool) {
c, ok := Get(ctx)
if !ok { if !ok {
return nil, false return nil
}
return c
} }
// FieldsFromContext returns the component as a string map with keys "module" and "path"; returns nil if none is present.
func FieldsFromContext(ctx context.Context) map[string]string {
c := FromContext(ctx)
if c == nil {
return nil
}
return map[string]string{ return map[string]string{
"module": c.Module(), "module": c.Module(),
"path": c.PathString(), "path": c.PathString(),
}, true }
} }
// New sets a new component on the context, resetting any existing component. // New sets a new component on the context, resetting any existing component.
@@ -81,8 +83,8 @@ func Extend(ctx context.Context, name string) (context.Context, error) {
return nil, fmt.Errorf("context is nil") return nil, fmt.Errorf("context is nil")
} }
c, ok := Get(ctx) c := FromContext(ctx)
if !ok { if c == nil {
return nil, fmt.Errorf("missing parent component") return nil, fmt.Errorf("missing parent component")
} }
@@ -113,8 +115,8 @@ func MustExtend(ctx context.Context, name string) context.Context {
panic("context is nil") panic("context is nil")
} }
c, ok := Get(ctx) c := FromContext(ctx)
if !ok { if c == nil {
panic("missing parent component") panic("missing parent component")
} }
+12 -15
View File
@@ -22,8 +22,8 @@ func TestNew(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }
c, ok := Get(ctx) c := FromContext(ctx)
if !ok { if c == nil {
t.Fatal("expected component in context") t.Fatal("expected component in context")
} }
if c.Module() != "mymodule" { if c.Module() != "mymodule" {
@@ -58,8 +58,8 @@ func TestExtend(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }
c, ok := Get(ctx) c := FromContext(ctx)
if !ok { if c == nil {
t.Fatal("expected component in context") t.Fatal("expected component in context")
} }
if c.Module() != "mymodule" { if c.Module() != "mymodule" {
@@ -87,28 +87,25 @@ func TestExtend(t *testing.T) {
}) })
} }
func TestGet(t *testing.T) { func TestFromContext(t *testing.T) {
_, ok := Get(context.Background()) if FromContext(context.Background()) != nil {
if ok {
t.Fatal("expected no component on bare context") t.Fatal("expected no component on bare context")
} }
ctx := MustNew(context.Background(), "mymodule", "mycomponent") ctx := MustNew(context.Background(), "mymodule", "mycomponent")
_, ok = Get(ctx) if FromContext(ctx) == nil {
if !ok {
t.Fatal("expected component in context") t.Fatal("expected component in context")
} }
} }
func TestGetFields(t *testing.T) { func TestFieldsFromContext(t *testing.T) {
_, ok := GetFields(context.Background()) if FieldsFromContext(context.Background()) != nil {
if ok {
t.Fatal("expected no fields on bare context") t.Fatal("expected no fields on bare context")
} }
ctx := MustNew(context.Background(), "mymodule", "mycomponent") ctx := MustNew(context.Background(), "mymodule", "mycomponent")
fields, ok := GetFields(ctx) fields := FieldsFromContext(ctx)
if !ok { if fields == nil {
t.Fatal("expected fields in context") t.Fatal("expected fields in context")
} }
if fields["module"] != "mymodule" { if fields["module"] != "mymodule" {
@@ -121,7 +118,7 @@ func TestGetFields(t *testing.T) {
func TestLogValue(t *testing.T) { func TestLogValue(t *testing.T) {
ctx := MustNew(context.Background(), "mymodule", "mycomponent") ctx := MustNew(context.Background(), "mymodule", "mycomponent")
c, _ := Get(ctx) c := FromContext(ctx)
v := c.LogValue() v := c.LogValue()
attrs := v.Group() attrs := v.Group()
if len(attrs) != 2 { if len(attrs) != 2 {