41 lines
1.4 KiB
Go
41 lines
1.4 KiB
Go
package promptkit
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
)
|
|
|
|
// Namespaces selects default resource namespaces for caller-supplied IDs.
|
|
// Empty fields leave that resource type unscoped.
|
|
type Namespaces struct {
|
|
Prompt string
|
|
Profile string
|
|
Backend string
|
|
}
|
|
|
|
// WithNamespaces returns an engine view that qualifies slashless prompt and
|
|
// profile selections with the supplied namespaces. Qualified selections pass
|
|
// through unchanged. It replaces all namespace defaults on the new view and
|
|
// leaves the receiver unchanged; resource existence is checked when selected.
|
|
// An empty field clears that resource's scope. Invalid namespace syntax or a
|
|
// nil engine returns an error matching ErrInvalidConfig.
|
|
func (e *Engine) WithNamespaces(namespaces Namespaces) (*Engine, error) {
|
|
if e == nil || e.internal == nil {
|
|
return nil, fmt.Errorf("%w: engine is nil", ErrInvalidConfig)
|
|
}
|
|
for _, namespace := range []struct {
|
|
kind string
|
|
name string
|
|
}{
|
|
{kind: "prompt", name: namespaces.Prompt},
|
|
{kind: "profile", name: namespaces.Profile},
|
|
{kind: "backend", name: namespaces.Backend},
|
|
} {
|
|
if namespace.name != "" && !domain.IsValidResourceNamespace(namespace.name) {
|
|
return nil, fmt.Errorf("%w: invalid %s namespace %q", ErrInvalidConfig, namespace.kind, namespace.name)
|
|
}
|
|
}
|
|
return &Engine{internal: e.internal.WithNamespaces(namespaces.Prompt, namespaces.Profile, namespaces.Backend)}, nil
|
|
}
|