refactor: slices.Sorted over map keys and one shared IsNil check
This commit is contained in:
1 parent
20d6ec93db
commit
f2f07fcb9b
18 files changed
+49
-175
No files matched your search
@@ -8,13 +8,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
protocol "github.com/samcharles93/archie-core/internal/tools/mcp"
|
||||
@@ -106,7 +106,7 @@ func (p *Provider) Start(ctx context.Context) error {
|
||||
if p.name == "" || p.segment == "" {
|
||||
return errors.New("MCP tool provider name is required")
|
||||
}
|
||||
if isNilTransport(p.transport) {
|
||||
if lifecycle.IsNil(p.transport) {
|
||||
return errors.New("MCP tool provider transport is nil")
|
||||
}
|
||||
if err := p.Manifest().Validate(); err != nil {
|
||||
@@ -198,7 +198,7 @@ func (p *Provider) Stop(ctx context.Context) error {
|
||||
return fmt.Errorf("remove MCP media dir %q: %w", mediaDir, err)
|
||||
}
|
||||
}
|
||||
if isNilTransport(p.transport) {
|
||||
if lifecycle.IsNil(p.transport) {
|
||||
return nil
|
||||
}
|
||||
if err := p.transport.Stop(ctx); err != nil {
|
||||
@@ -428,16 +428,3 @@ func isASCIILetter(char rune) bool {
|
||||
func isASCIIDigit(char rune) bool {
|
||||
return char >= '0' && char <= '9'
|
||||
}
|
||||
|
||||
func isNilTransport(transport LifecycleTransport) bool {
|
||||
if transport == nil {
|
||||
return true
|
||||
}
|
||||
value := reflect.ValueOf(transport)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return value.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,12 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"maps"
|
||||
"slices"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/lifecycle"
|
||||
"github.com/samcharles93/archie-core/internal/plugin"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
)
|
||||
@@ -100,7 +101,7 @@ func (r *Registry) Register(engine Engine) error {
|
||||
// prepareRegistration validates an engine and builds its registration, the
|
||||
// validation Register, Add and Replace all share.
|
||||
func prepareRegistration(engine Engine, optional bool) (registration, error) {
|
||||
if isNilEngine(engine) {
|
||||
if lifecycle.IsNil(engine) {
|
||||
return registration{}, errors.New("tool provider is nil")
|
||||
}
|
||||
manifest, err := safeManifest(engine)
|
||||
@@ -390,11 +391,7 @@ func cloneRegistrations(source map[string]registration) map[string]registration
|
||||
}
|
||||
|
||||
func dependencyOrder(providers map[string]registration) ([]string, error) {
|
||||
ids := make([]string, 0, len(providers))
|
||||
for id := range providers {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
slices.Sort(ids)
|
||||
ids := slices.Sorted(maps.Keys(providers))
|
||||
|
||||
indegree := make(map[string]int, len(providers))
|
||||
dependents := make(map[string][]string, len(providers))
|
||||
@@ -486,19 +483,6 @@ func cloneManifest(manifest plugin.Manifest) plugin.Manifest {
|
||||
return clone
|
||||
}
|
||||
|
||||
func isNilEngine(engine Engine) bool {
|
||||
if engine == nil {
|
||||
return true
|
||||
}
|
||||
value := reflect.ValueOf(engine)
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
|
||||
return value.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func safeManifest(engine Engine) (manifest plugin.Manifest, err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
)
|
||||
|
||||
@@ -30,12 +31,7 @@ func (r *Registry) Has(id string) bool {
|
||||
func (r *Registry) RegisteredIDs() []string {
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
ids := make([]string, 0, len(r.providers))
|
||||
for id := range r.providers {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
slices.Sort(ids)
|
||||
return ids
|
||||
return slices.Sorted(maps.Keys(r.providers))
|
||||
}
|
||||
|
||||
// RunningIDs returns the manifest IDs of the running providers, in start
|
||||
|
||||
Reference in new issue
Block a user