add try-extend variant

This commit is contained in:
Jay
2026-05-10 09:59:22 -04:00
parent ad04a920fd
commit 03c783ba53
3 changed files with 29 additions and 1 deletions
+15
View File
@@ -93,6 +93,21 @@ func Extend(ctx context.Context, name string) (context.Context, error) {
return insert(ctx, c.Module(), name, c.Path()), nil
}
// TryExtend returns ctx if no parent component is attached, otherwise
// extends the existing component path.
func TryExtend(ctx context.Context, name string) (context.Context, error) {
if ctx == nil {
return nil, fmt.Errorf("context is nil")
}
c, ok := Get(ctx)
if !ok || name == "" {
return ctx, nil
}
return insert(ctx, c.Module(), name, c.Path()), nil
}
// MustNew is New but panics on error.
func MustNew(ctx context.Context, module string, name string) context.Context {
if ctx == nil {