51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package profile
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
|
|
"gitea.maximumdirect.net/eric/promptkit/internal/jsonvalue"
|
|
)
|
|
|
|
// NewMemoryRepository constructs an immutable repository from profile
|
|
// definitions supplied by an internal caller.
|
|
func NewMemoryRepository(definitions []domain.ExecutionProfile) (Repository, error) {
|
|
repository := &memoryRepository{profiles: make(map[string]domain.ExecutionProfile, len(definitions))}
|
|
for _, definition := range definitions {
|
|
extraParams, err := jsonvalue.CopyMap(definition.ExtraParams)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
definition.ExtraParams = extraParams
|
|
if err := NormalizeAndValidateDefinition(&definition); err != nil {
|
|
return nil, err
|
|
}
|
|
if _, exists := repository.profiles[definition.ID]; exists {
|
|
return nil, fmt.Errorf("duplicate profile id %q", definition.ID)
|
|
}
|
|
repository.profiles[definition.ID] = definition
|
|
}
|
|
return repository, nil
|
|
}
|
|
|
|
type memoryRepository struct {
|
|
profiles map[string]domain.ExecutionProfile
|
|
}
|
|
|
|
func (r *memoryRepository) GetProfile(_ context.Context, id string) (*domain.ExecutionProfile, error) {
|
|
if r == nil {
|
|
return nil, ErrProfileNotFound
|
|
}
|
|
definition, found := r.profiles[id]
|
|
if !found {
|
|
return nil, ErrProfileNotFound
|
|
}
|
|
extraParams, err := jsonvalue.CopyMap(definition.ExtraParams)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("copy memory profile %q: %w", definition.ID, err)
|
|
}
|
|
definition.ExtraParams = extraParams
|
|
return &definition, nil
|
|
}
|