Add immutable namespace views
This commit is contained in:
40
namespaces.go
Normal file
40
namespaces.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user